ocReaper naming convention analyzer for PHP_CodeSniffer provides sniffs that ensure proper variable and function naming.
The ensured convention is identical to kettanaito/naming-cheatsheet.
The project uses tbrianjones/Inflect.php to handle pluralization.
Ensures that a function must start with a verb. In case the function starts with a prefix: the second word will be used in check.
Ensures that functions that returns boolean value must start with:
- has
- is
- should
Ensures that a function does not duplicate the name of the class it belongs to.
Ensures that a variable does not duplicate the name of the function it belongs to.
Ensures that the name of those variables that are containing non-associative arrays are in plural.
Ensures that function names are in camel case.
Ensures that variable names are in camel case.
Ensures that class member names are in camel case.
Ensures that variable names inside a doublequoted string are in camel case.
The recommended way to install ocReaper naming convention analyzer is through Composer.
{
"require-dev": {
"ocreaper/naming-convention-analyzer": "0.4.1"
}
}
You can choose one of two ways to run only selected sniffs from the standard on your codebase:
The recommended way is to write your own ruleset.xml by referencing only the selected sniffs. This is a sample ruleset.xml:
<?xml version="1.0"?>
<ruleset name="AcmeProject">
<config name="installed_paths" value="../../ocreaper/naming-convention-analyzer"/><!-- relative path from PHPCS source location -->
<rule ref="NamingConventionAnalyzer.Functions.ContextDuplication"/>
<!-- other sniffs to include -->
</ruleset>
Then run the phpcs
executable the usual way:
vendor/bin/phpcs --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
You can also mention ocReaper naming convention analyzer in your project's ruleset.xml
and exclude only some sniffs:
<?xml version="1.0"?>
<ruleset name="AcmeProject">
<rule ref="vendor/ocreaper/naming-convention-analyzer/NamingConventionAnalyzer/ruleset.xml"><!-- relative path to your ruleset.xml -->
<!-- sniffs to exclude -->
</rule>
</ruleset>
However it is not a recommended way to use ocReaper naming convention analyzer, because your build can break when moving between minor versions of the standard (which can happen if you use ^
or ~
version constraint in composer.json
). I regularly add new sniffs even in minor versions meaning your code won't most likely comply with new minor versions of the package.
Sniffs in this standard marked by the 🔧 symbol support automatic fixing of coding standard violations. To fix your code automatically, run phpcbf instead of phpcs:
vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
Always remember to back up your code before performing automatic fixes and check the results with your own eyes as the automatic fixer can sometimes produce unwanted results.
To make this repository work on your machine, clone it and run these two commands in the root directory of the repository:
composer install