Code quality and security for PHP
The SonarQube analyzer for PHP inspects PHP code for bugs, vulnerabilities, and maintainability issues. This repository contains the analyzer implementation and documentation for developers working on it.
To learn more about Sonar’s PHP analysis, visit the Sonar website.
Features
Useful links
More documentation
Have questions or feedback?
To provide feedback (request a feature, report a bug, etc.) use the SonarSource Community Forum. Please do not forget to specify the language (PHP!), plugin version, and SonarQube version.
If you have a question on how to use plugin (and the docs don't help you), we also encourage you to use the community forum.
Contributing
Topic in SonarSource Community Forum
To request a new feature, please create a new thread in SonarSource Community Forum. Even if you plan to implement it yourself and submit it back to the community, please start a new thread first to be sure that we can follow up on it.
Pull Request (PR)
To submit a contribution, create a pull request for this repository. Please make sure that you follow our code style and that all tests are passing.
Custom Rules
If you have an idea for a rule, but you are not sure that everyone needs it you can implement a custom rule available only for you.
Custom Rules API Changes
- 4.0
ExpressionListStatementTree, Tree.Kind#EXPRESSION_LIST_STATEMENT, and VisitorCheck#visitExpressionListStatement(...) were removed. Use EchoTagStatementTree, Tree.Kind#ECHO_TAG_STATEMENT, and VisitorCheck#visitEchoTagStatement(...) instead.
ParameterTree#type() was removed. Use ParameterTree#declaredType() instead.
ReturnTypeClauseTree#type() was removed. Use ReturnTypeClauseTree#declaredType() instead.
ClassPropertyDeclarationTree#typeAnnotation() was removed. Use ClassPropertyDeclarationTree#declaredType() instead.
FunctionCallTree#arguments() was removed. Use FunctionCallTree#callArguments() instead.
AnonymousClassTree#arguments() was removed. Use AnonymousClassTree#callArguments() instead.
ClassDeclarationTree#modifierToken() and EnumDeclarationTree#modifierToken() were removed. Use modifiersToken() for full token access, or isAbstract(), isFinal(), and isReadOnly() for semantic checks. modifiersToken() is not a drop-in replacement: modifierToken() only returned abstract or final, and returned null otherwise, while modifiersToken() returns all modifier tokens, including readonly.
- 3.32 (October 2023)
- Additional
newIssue endpoint added to the CheckContext API interface
- 3.15 (January 2021)
PHPCustomRulesDefinition was removed, it was deprecated since version 2.13 (March 2018)
- Removed dependency on sslr-squid-bridge which is not maintained anymore
- 3.11, support of PHP 8:
ParameterTree#type() is deprecated. Use ParameterTree#declaredType() instead.
ReturnTypeClauseTree#type() is deprecated. Use ReturnTypeClauseTree#declaredType() instead.
ClassPropertyDeclarationTree#typeAnnotation() is deprecated. Use ClassPropertyDeclarationTree#declaredType() instead.
CatchBlockTree#variable() can now return NULL.
FunctionCallTree#arguments() is deprecated. Use FunctionCallTree#callArguments() instead.
AnonymousClassTree#arguments() is deprecated. Use AnonymousClassTree#callArguments() instead.
- New tree:
CallArgumentTree. This tree wraps expressions passed as arguments now.
- New kind of expression:
ThrowExpressionTree.
- New kind of expression:
MatchExpressionTree.
ParameterTree now has a visibility method.
Build & Test
To build and run tests locally follow these instructions.
To configure build dependencies, run the following command:
git submodule update --init -- build-logic/common
To always get the latest version of the build logic during git operations, set the following configuration:
git config submodule.recurse true
Build the Project and Run Unit Tests
To build the plugin and run its unit tests, execute this command from the project's root directory:
Fix code formatting issues
During the Gradle build, a spotless formatting check is executed.
This check can also be triggered manually with ./gradlew spotlessCheck.
It checks if the code is correctly formatted using standard Sonar rules.
If your build failed, you can fix the formatting just by running:
Fix license packaging issues
During the Gradle build, a license packaging check is executed.
This check can also be triggered manually with ./gradlew validateLicenseFiles.
It checks if the license files of third party libraries are correctly packaged to the resource folder according to SonarSource standards.
If your build failed, you can fix the license packaging by running:
Note that this overwrites your current license files in the resources/licenses/THIRD_PARTY_LICENSES folder.
./gradlew generateLicenseResources
Regenerate Dependency Lock Files
Gradle lock files pin dependency versions to ensure reproducible builds. When dependencies change (e.g. after updating versions in build.gradle.kts or gradle/libs.versions.toml), regenerate all lock files with:
./gradlew writeAllLockFiles --write-locks
Integration Tests
To run integration tests, you will need to create a properties file like the one shown below, and set its location in an environment variable named ORCHESTRATOR_CONFIG_URL.
# version of SonarQube server
sonar.runtimeVersion=LATEST_RELEASE
Before running any of the integration tests make sure the submodules are checked out:
git submodule update --init
Plugin Test
The "Plugin Test" is an additional integration test that verifies plugin features such as metric calculation, coverage, etc. To launch it:
./gradlew its:plugin:integrationTest
Ruling Test
The "Ruling Test" is a special integration test that launches the analysis of a large code base, saves the issues created by the plugin in report files, and then compares those results to the set of expected issues (stored as SARIF files). To launch the ruling test:
./gradlew its:ruling:integrationTest
The sources of the ruling projects are in the its/sources submodule under <project-key>. Check out the submodule
before running the integration tests.
This test gives you the opportunity to examine the issues created by each rule and make sure they're what you expect. You can inspect new/lost issues by checking the SonarQube local URL mentioned in the logs at the end of the analysis.
If everything looks good to you, you can copy the files with the actual issues located at
sonar-php/its/ruling/build/actual/<projectKey>/
into the directory with the expected issues
sonar-php/its/ruling/src/test/resources/expected/<projectKey>/
PR-analysis tests retain the SonarQube project key prAnalysis to reuse the base-analysis cache and compare the existing component keys.
Each scan has a unique dump project key under its/ruling/build/actual/: prAnalysis-logs-<scenario>-base,
prAnalysis-logs-<scenario>-pr, prAnalysis-issues-<scenario>-base, or prAnalysis-issues-<scenario>-branch,
where <scenario> is added, changed, or deleted. Only the branch issue dumps should update
its/ruling/src/test/resources/expected/prAnalysis/<scenario>/; base dumps correspond to expected/prAnalysis/base/.
The PR log-test dumps contain PR-only issues and must not replace the full branch expectations.
Rule Descriptions
Update Rule Descriptions
To update all rule descriptions:
Generate New Rule Description
To fetch static files for a rule SXXXX from RSPEC:
./gradlew ruleApiGenerateRule -Prule=SXXXX
Same for a specific RSPEC branch (master by default):
./gradlew ruleApiGenerateRule -Prule=SXXXX -Pbranch=my-branch
License
Copyright 2010-2025 SonarSource.
SonarQube analyzers released after November 29, 2024, including patch fixes for prior versions,
are published under the Sonar Source-Available License Version 1 (SSALv1).
See individual files for details that specify the license applicable to each file.
Files subject to the SSALv1 will be noted in their headers.