Download the PHP package fig-r/psr2r-sniffer without Composer
On this page you can find all versions of the php package fig-r/psr2r-sniffer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fig-r/psr2r-sniffer
More information about fig-r/psr2r-sniffer
Files in fig-r/psr2r-sniffer
Package psr2r-sniffer
Short Description Code-Sniffer, Auto-Fixer and Tokenizer for PSR2-R
License MIT
Informations about the package psr2r-sniffer
PSR-2-R Sniffer
For details on PSR-2-R see fig-rectified-standards.
For PHP version compatibility see version map.
Documentation @ /docs/.
This branch is for use with PHP 8.1+ repos.
Included sniffs
The following sniffers are bundles together as PSR2R
already, but you can
also use them standalone/separately in any way you like.
Currently, PSR2R ships with over 190 sniffs.
- List of included sniffs
Furthermore, the following ones are recommended to add:
- PhpCollective.Commenting.DocBlockThrows
- PhpCollective.Commenting.DocBlockTagGrouping
- SlevomatCodingStandard.Functions.DisallowEmptyFunction
- SlevomatCodingStandard.PHP.UselessParentheses
This library uses squizlabs/PHP_CodeSniffer. It can detect all issues and fix some of them automatically and is ideal for CI integration (travis, jenkins, circleci, GA etc).
Usage
How to use for your project
Most likely you are using composer. As such, put it into the require-dev dependencies:
Set up a phpcs.xml
in the root of your library.
Define here the standard to use as well as the project paths, usually
You can then use it like this:
The path is optional if you want to run it on a specific subset of your defined paths. Otherwise, it defaults to the paths defined in your XML file.
Customizing
You can always customize your phpcs.xml
further, e.g.:
Useful commands
Verbose output with -v
is always useful. With -vv
or even -vvv
you get even more debug output.
If you want a list of all sniffs in this standard, use -e
:
It will list them all grouped by their standard name and sniff type.
To just run a single sniff, use --sniffs=...
and a comma separated list of sniffs, e.g.:
Usually, if you run it over your complete repository, you would want to exclude dirs like tests/test_files/
:
Windows usage
For Win OS you should be using \
as separator:
Include it in your IDE via hotkey
E.g. for PHPStorm:
- Open Settings -> Tools -> External Tools
- Add a new tool named "cs-sniffer" and set Program to
$ProjectFileDir$/vendor/bin/phpcs
, Parameters to--standard=$ProjectFileDir$/vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml -p $FilePath$
and Working directoy to$ProjectFileDir$
. - Add a new tool named "cs-fixer" and set Program to
$ProjectFileDir$/vendor/bin/phpcbf
, Parameters to--standard=$ProjectFileDir$/vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml -v $FilePath$
and Working directoy to$ProjectFileDir$
. - Optionally uncheck "Open console" if you don't want to see any output here for the fixer.
- Now set up your hotkeys under Settings -> Keymap (search for cs-sniffer and cs-fixer). E.g.
Control + Comma
for sniffing, andControl + Dot
for fixing.
Hook it into your IDE for live-correction
You can also easily make a "watcher" for your IDE, so any file you work on, will be auto-corrected when (auto-)saving. But here you should better only whitelist certain sniffs that only add things and don't remove anything.
Note: For PHPStorm, for example, make sure you switch Show Console
to never
to not be disturbed by it all the time.
Writing new sniffs
You can contribute by adding new sniffs as per PSR-2-R standard.
Tokenizing Tool
It really helps to see what the code looks like for the sniffer. So we can parse a PHP file into its tokens using the following tool:
(If you run this from your application, it will have to be run as vendor/bin/tokenize
)
With more verbose output:
For a file MyClass.php
it will create a token file MyClass.tokens.php
in the same folder.
Example output of a single line of PHP code:
Using the verbose option:
All versions of psr2r-sniffer with dependencies
php-collective/code-sniffer Version ^0.2.6
slevomat/coding-standard Version ^8.3.0
squizlabs/php_codesniffer Version ^3.7.0