{"id":13826651,"url":"https://github.com/php-fig-rectified/psr2r-sniffer","last_synced_at":"2026-01-24T10:06:35.720Z","repository":{"id":2488129,"uuid":"46661120","full_name":"php-fig-rectified/psr2r-sniffer","owner":"php-fig-rectified","description":"A PSR-2-R code sniffer and code-style auto-correction-tool - including many useful additions","archived":false,"fork":false,"pushed_at":"2024-03-30T20:50:27.000Z","size":662,"stargazers_count":32,"open_issues_count":1,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-24T13:20:34.564Z","etag":null,"topics":["code-style","fixer","php","sniffer","tokenizer"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-fig-rectified.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2015-11-22T13:10:50.000Z","updated_at":"2024-03-29T19:49:18.000Z","dependencies_parsed_at":"2023-01-13T11:53:46.830Z","dependency_job_id":"f4b00527-6ee6-4939-818f-3ceaba1f1dca","html_url":"https://github.com/php-fig-rectified/psr2r-sniffer","commit_stats":{"total_commits":310,"total_committers":9,"mean_commits":34.44444444444444,"dds":0.6064516129032258,"last_synced_commit":"1d7c2ede4b476cadc9c60887b38750b43a74e503"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-fig-rectified%2Fpsr2r-sniffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-fig-rectified%2Fpsr2r-sniffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-fig-rectified%2Fpsr2r-sniffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-fig-rectified%2Fpsr2r-sniffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-fig-rectified","download_url":"https://codeload.github.com/php-fig-rectified/psr2r-sniffer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225476383,"owners_count":17480215,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-style","fixer","php","sniffer","tokenizer"],"created_at":"2024-08-04T09:01:41.896Z","updated_at":"2026-01-24T10:06:30.699Z","avatar_url":"https://github.com/php-fig-rectified.png","language":"PHP","funding_links":[],"categories":["\u003ca id=\"7bf0f5839fb2827fdc1b93ae6ac7f53d\"\u003e\u003c/a\u003e工具"],"sub_categories":["\u003ca id=\"32739127f0c38d61b14448c66a797098\"\u003e\u003c/a\u003e嗅探\u0026\u0026Sniff"],"readme":"# PSR-2-R Sniffer\n[![CI](https://github.com/php-fig-rectified/psr2r-sniffer/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/php-fig-rectified/psr2r-sniffer/actions/workflows/ci.yml)\n[![Latest Stable Version](https://poser.pugx.org/fig-r/psr2r-sniffer/v/stable.svg)](https://packagist.org/packages/fig-r/psr2r-sniffer)\n[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/)\n[![License](https://poser.pugx.org/fig-r/psr2r-sniffer/license.svg)](https://packagist.org/packages/fig-r/psr2r-sniffer)\n[![Total Downloads](https://poser.pugx.org/fig-r/psr2r-sniffer/d/total.svg)](https://packagist.org/packages/fig-r/psr2r-sniffer)\n[![Coding Standards](https://img.shields.io/badge/cs-PSR--2--R-yellow.svg)](https://github.com/php-fig-rectified/fig-rectified-standards)\n\nFor details on PSR-2-R see [fig-rectified-standards](https://github.com/php-fig-rectified/fig-rectified-standards).\n\nFor PHP version compatibility see [version map](https://github.com/php-fig-rectified/psr2r-sniffer/wiki).\n\nDocumentation @ [/docs/](docs).\n\nThis branch is for use with **PHP 8.1+** repos.\n\n## Included sniffs\nThe following sniffers are bundles together as `PSR2R` already, but you can\nalso use them standalone/separately in any way you like.\n\n**Currently, PSR2R ships with over 190 sniffs.**\n\n* [List of included sniffs](docs/sniffs.md)\n\nFurthermore, the following ones are recommended to add:\n- PhpCollective.Commenting.DocBlockThrows\n- PhpCollective.Commenting.DocBlockTagGrouping\n- SlevomatCodingStandard.Functions.DisallowEmptyFunction\n- SlevomatCodingStandard.PHP.UselessParentheses\n\nThis library uses [squizlabs/PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer/).\nIt can detect all issues and fix some of them automatically and is ideal for CI integration\n(travis, jenkins, circleci, GA etc).\n\n### Usage\n\n#### How to use for your project\nMost likely you are using composer. As such, put it into the require-dev dependencies:\n```\ncomposer require --dev fig-r/psr2r-sniffer\n```\n\nSet up a `phpcs.xml` in the root of your library.\nDefine here the standard to use as well as the project paths, usually\n```xml\n\u003crule ref=\"vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml\"/\u003e\n\n\u003cfile\u003esrc/\u003c/file\u003e\n\u003cfile\u003etests/\u003c/file\u003e\n```\n\nYou can then use it like this:\n```\n// Sniffs only\nvendor/bin/phpcs {path/file}\n\n// Sniffs and fixes\nvendor/bin/phpcbf {path/file}\n```\nThe path is optional if you want to run it on a specific subset of your defined paths.\nOtherwise, it defaults to the paths defined in your XML file.\n\n### Customizing\nYou can always customize your `phpcs.xml` further, e.g.:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"app\"\u003e\n\n\t\u003carg value=\"nps\"/\u003e\n\n\t\u003cfile\u003esrc/\u003c/file\u003e\n\t\u003cfile\u003etests/\u003c/file\u003e\n\n\t\u003cexclude-pattern\u003e/config/Migrations/\u003c/exclude-pattern\u003e\n\n\t\u003c!-- Add main ruleset --\u003e\n\t\u003crule ref=\"vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml\"/\u003e\n\n\t\u003c!-- Silence certain sniffs/rules --\u003e\n\t\u003crule ref=\"PSR2.Methods.MethodDeclaration.Underscore\"\u003e\n\t\t\u003cseverity\u003e0\u003c/severity\u003e\n\t\u003c/rule\u003e\n\t\u003crule ref=\"PSR2.Classes.PropertyDeclaration.Underscore\"\u003e\n\t\t\u003cseverity\u003e0\u003c/severity\u003e\n\t\u003c/rule\u003e\n\n\t\u003c!-- Define your own additional sniffs here --\u003e\n\n\u003c/ruleset\u003e\n```\n\n\n#### Useful commands\nVerbose output with `-v` is always useful. With `-vv` or even `-vvv` you get even more debug output.\n\nIf you want a list of all sniffs in this standard, use `-e`:\n```\nvendor/bin/phpcs -e\n```\nIt will list them all grouped by their standard name and sniff type.\n\nTo just run a single sniff, use `--sniffs=...` and a comma separated list of sniffs, e.g.:\n```\nvendor/bin/phpcs --sniffs=PSR2R.Files.EndFileNewline\n```\n\nUsually, if you run it over your complete repository, you would want to exclude dirs like `tests/test_files/`:\n```\nvendor/bin/phpcs --ignore=/tests/test_files/\n```\n\n#### Windows usage\nFor Win OS you should be using `\\` as separator:\n```\nvendor\\bin\\phpcs\n```\n\n#### Include it in your IDE via hotkey\nE.g. for PHPStorm:\n* Open Settings -\u003e Tools -\u003e External Tools\n* 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$`.\n* 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$`.\n* Optionally uncheck \"Open console\" if you don't want to see any output here for the fixer.\n* Now set up your hotkeys under Settings -\u003e Keymap (search for cs-sniffer and cs-fixer). E.g. `Control + Comma` for sniffing, and `Control + Dot` for fixing.\n\n##### Hook it into your IDE for live-correction\nYou can also easily make a \"watcher\" for your IDE, so any file you work on, will be auto-corrected when (auto-)saving.\nBut here you should better only whitelist certain sniffs that only add things and don't remove anything.\n\nNote: For PHPStorm, for example, make sure you switch `Show Console` to `never` to not be disturbed by it all the time.\n\n### Writing new sniffs\nYou can contribute by adding new sniffs as per PSR-2-R standard.\n\n#### Tokenizing Tool\nIt really helps to see what the code looks like for the sniffer.\nSo we can parse a PHP file into its tokens using the following tool:\n\n```\nbin/tokenize /path/to/file\n```\n(If you run this from your application, it will have to be run as `vendor/bin/tokenize`)\n\nWith more verbose output:\n```\nbin/tokenize /path/to/file -v\n```\n\nFor a file `MyClass.php` it will create a token file `MyClass.tokens.php` in the same folder.\n\nExample output of a single line of PHP code:\n```php\n...\n    protected static function _optionsToString($options) {\n// T_WHITESPACE T_PROTECTED T_WHITESPACE T_STATIC T_WHITESPACE T_FUNCTION T_WHITESPACE T_STRING T_OPEN_PARENTHESIS T_VARIABLE T_CLOSE_PARENTHESIS T_WHITESPACE T_OPEN_CURLY_BRACKET T_WHITESPACE\n...\n```\nUsing the verbose option:\n```php\n...\n    protected static function _optionsToString($options) {\n// T_WHITESPACE (935) code=379, line=105, column=1, length=1, level=1, conditions={\"9\":358}, content=`\\t`\n// T_PROTECTED (936) code=348, line=105, column=2, length=9, level=1, conditions={\"9\":358}, content=`protected`\n// T_WHITESPACE (937) code=379, line=105, column=11, length=1, level=1, conditions={\"9\":358}, content=` `\n// T_STATIC (938) code=352, line=105, column=12, length=6, level=1, conditions={\"9\":358}, content=`static`\n// T_WHITESPACE (939) code=379, line=105, column=18, length=1, level=1, conditions={\"9\":358}, content=` `\n// T_FUNCTION (940) code=337, line=105, column=19, length=8, parenthesis_opener=943, parenthesis_closer=945, parenthesis_owner=940, scope_condition=940, scope_opener=947, scope_closer=1079, level=1, conditions={\"9\":358}, content=`function`\n// T_WHITESPACE (941) code=379, line=105, column=27, length=1, level=1, conditions={\"9\":358}, content=` `\n// T_STRING (942) code=310, line=105, column=28, length=16, level=1, conditions={\"9\":358}, content=`_optionsToString`\n// T_OPEN_PARENTHESIS (943) code=PHPCS_T_OPEN_PARENTHESIS, line=105, column=44, length=1, parenthesis_opener=943, parenthesis_owner=940, parenthesis_closer=945, level=1, conditions={\"9\":358}, content=`(`\n// T_VARIABLE (944) code=312, line=105, column=45, length=8, nested_parenthesis={\"943\":945}, level=1, conditions={\"9\":358}, content=`$options`\n// T_CLOSE_PARENTHESIS (945) code=PHPCS_T_CLOSE_PARENTHESIS, line=105, column=53, length=1, parenthesis_owner=940, parenthesis_opener=943, parenthesis_closer=945, level=1, conditions={\"9\":358}, content=`)`\n// T_WHITESPACE (946) code=379, line=105, column=54, length=1, level=1, conditions={\"9\":358}, content=` `\n// T_OPEN_CURLY_BRACKET (947) code=PHPCS_T_OPEN_CURLY_BRACKET, line=105, column=55, length=1, bracket_opener=947, bracket_closer=1079, scope_condition=940, scope_opener=947, scope_closer=1079, level=1, conditions={\"9\":358}, content=`{`\n// T_WHITESPACE (948) code=379, line=105, column=56, length=0, level=2, conditions={\"9\":358,\"940\":337}, content=`\\n`\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-fig-rectified%2Fpsr2r-sniffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-fig-rectified%2Fpsr2r-sniffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-fig-rectified%2Fpsr2r-sniffer/lists"}