garrcomm / code-sniffs
Additional PHP Codesniffer rules made by Garrcomm.
Requires (Dev)
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-11-16 13:06:28 UTC
README
This package contains some additional PHP Codesniffer rules that can be included in your project.
Usage
These sniffs can be added as a composer package;
$ composer require garrcomm/code-sniffs dev-master --dev
After installing the package, add the following line to to your phpcs.xml
file:
<config name="installed_paths" value="vendor/garrcomm/code-sniffs"/>
<rule ref="Garrcomm"/>
Or run PHP Codesniffer with the following parameters:
$ ./vendor/bin/phpcs --standard=vendor/garrcomm/code-sniffs path/to/your/files
But why don't you just write clean code from the start?
Sniffs help clean up legacy code, it's not just restricting new code. I found a few issues in existing legacy code and wrote sniffs to detect (and repair) some common issues.
Code quality
These sniffs are also written in PHP. Those PHP files are scanned with PHPUnit and PHPStan to make sure they work properly. Also, PHP CodeSniffer itself scanned the code so it complies with the PSR-12 standards and are well-documented. Those scans are automated in Bitbucket Pipelines.
Available sniffs
- Garrcomm.Classes.ClassDeclarationInString.Found
Replaces class names as string ($em->getReference('Foo\Bar', 1)
) to::class
constants ($em->getReference(\Foo\Bar::class, 1)
). - Garrcomm.Commenting.DisallowCommentedCode.Found
Errors on code that's commented out (example:// var_dump($foo);
) - Garrcomm.Commenting.DisallowHashComments.Found
Replaces hash comments (# perl style comment
) to regular comments (// regular comment
) - Garrcomm.Namespaces.UnusedUses.Found
Removes unused use statements (for exampleuse Foo\Bar;
whenBar
is not in use in the file)
Tip for Windows Developers
In the bin
folder, a few batch files exist, to make development easier.
If you install Docker Desktop for Windows, you can use bin\composer.bat, bin\phpstan.bat, bin\phpunit.bat, bin\phpcs.bat and bin\phpcbf.bat as shortcuts for Composer, PHP Static Analyser, PHP Unit, CodeSniffer and Code Beautifier without the need of installing PHP and other dependencies on your machine.