Discover and update the PHP-specific properties in Administration > General Settings > PHP
Analyze php.ini Files
The PHP analyzer can analyze
php.ini files with some specific rules (if these rules are activated in your quality profile).
php.ini files must be part of the project you are analyzing, meaning the
php.ini files have to be inside the directories listed in
sonar.sources. Rules targeting
php.ini files can be quickly identified through the php-ini tag set on them.
The PHP analyzer parses the source code, creates an Abstract Syntax Tree (AST) and then walks through the entire tree. A coding rule is a visitor that is able to visit nodes from this AST.
As soon as the coding rule visits a node, it can navigate its children and log issues if necessary.
To get started a sample plugin can be found here: php-custom-rules.
Writing a Plugin
Custom rules for PHP can be added by writing a SonarQube Plugin and using PHP analyzer APIs. Here are the step to follow:
Create SonarQube Plugin
- create a standard SonarQube plugin project
- attach this plugin to the SonarQube PHP analyzer through the
- add the dependency to the PHP analyzer.
- add the following line in the sonar-packaging-maven-plugin configuration.
- implement the following extension points:
- RulesDefinition and PHPCustomRuleRepository, which can be implemented by a single class, to declare your custom rules
- declare the RulesDefinition as an extension in the Plugin extension point.
Implement a Rule
- create a class that will hold the implementation of the rule, it should:
- define the rule name, key, tags, etc. with Java annotations.
- declare this class in the
To explore a part of the AST, override a method from the PHPVisitorCheck. For example, if you want to explore "if statement" nodes, override PHPVisitorCheck#visitIfStatement method that will be called each time an ifStatementTree node is encountered in the AST.
When overriding a visit method, you must call the super method in order to allow the visitor to visit the children the node.
To explore a part of the AST, override
PHPSubscriptionCheck#nodesToVisit by returning the list of the
Tree#Kind of node you want to visit. For example, if you want to explore "if statement" nodes the method will return a list containing the element
From the check, issue can be created by calling