|
| 1 | +# ocReaper naming convention analyzer |
| 2 | + |
| 3 | +[](https://packagist.org/packages/ocreaper/naming-convention-analyzer) |
| 4 | +[](https://packagist.org/packages/ocreaper/naming-convention-analyzer) |
| 5 | + |
| 6 | +ocReaper naming convention analyzer for [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) provides sniffs that ensure proper variable and function naming. |
| 7 | + |
| 8 | +The ensured convention is identical to [kettanaito/naming-cheatsheet](https://github.com/kettanaito/naming-cheatsheet). |
| 9 | + |
| 10 | +## Table of contents |
| 11 | + |
| 12 | +1. [Sniffs included in this standard](#sniffs-included-in-this-standard) |
| 13 | +2. [Installation](#installation) |
| 14 | +3. [How to run the sniffs](#how-to-run-the-sniffs) |
| 15 | + - [Choose which sniffs to run](#choose-which-sniffs-to-run) |
| 16 | + - [Using all sniffs from the standard](#using-all-sniffs-from-the-standard) |
| 17 | +4. [Fixing errors automatically](#fixing-errors-automatically) |
| 18 | +5. [Suppressing sniffs locally](#suppressing-sniffs-locally) |
| 19 | +6. [Contributing](#contributing) |
| 20 | + |
| 21 | +## Sniffs included in this standard |
| 22 | + |
| 23 | +🔧 = [Automatic errors fixing](#fixing-errors-automatically) |
| 24 | + |
| 25 | +#### NamingConventionAnalyzer.Functions.ActionPrefix |
| 26 | + |
| 27 | +Ensures that a function must start with: |
| 28 | + |
| 29 | +- compose |
| 30 | +- create |
| 31 | +- delete |
| 32 | +- fetch |
| 33 | +- get |
| 34 | +- handle |
| 35 | +- remove |
| 36 | +- reset |
| 37 | +- set |
| 38 | + |
| 39 | +#### NamingConventionAnalyzer.Functions.BoolPrefix |
| 40 | + |
| 41 | +Ensures that functions that returns boolean value must start with: |
| 42 | + |
| 43 | +- has |
| 44 | +- is |
| 45 | +- should |
| 46 | + |
| 47 | +#### NamingConventionAnalyzer.Functions.ContextDuplication |
| 48 | + |
| 49 | +Ensures that a function does not duplicate the name of the class it belongs to. |
| 50 | + |
| 51 | +#### NamingConventionAnalyzer.Functions.SingularPlural |
| 52 | + |
| 53 | +Ensures that the name of those variables that are containing non-associative arrays are in plural. |
| 54 | + |
| 55 | +## Installation |
| 56 | + |
| 57 | +The recommended way to install ocReaper naming convention analyzer is [through Composer](http://getcomposer.org). |
| 58 | + |
| 59 | +```JSON |
| 60 | +{ |
| 61 | + "require-dev": { |
| 62 | + "ocreaper/naming-convention-analyzer": "*" |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | +## How to run the sniffs |
| 67 | + |
| 68 | +You can choose one of two ways to run only selected sniffs from the standard on your codebase: |
| 69 | + |
| 70 | +### Choose which sniffs to run |
| 71 | + |
| 72 | +The recommended way is to write your own ruleset.xml by referencing only the selected sniffs. This is a sample ruleset.xml: |
| 73 | + |
| 74 | +```xml |
| 75 | +<?xml version="1.0"?> |
| 76 | +<ruleset name="AcmeProject"> |
| 77 | + <config name="installed_paths" value="../../ocreaper/naming-convention-analyzer"/><!-- relative path from PHPCS source location --> |
| 78 | + <rule ref="NamingConventionAnalyzer.Functions.ContextDuplication"/> |
| 79 | + <!-- other sniffs to include --> |
| 80 | +</ruleset> |
| 81 | +``` |
| 82 | + |
| 83 | +Then run the `phpcs` executable the usual way: |
| 84 | + |
| 85 | +``` |
| 86 | +vendor/bin/phpcs --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests |
| 87 | +``` |
| 88 | + |
| 89 | +### Exclude sniffs you don't want to run |
| 90 | + |
| 91 | +You can also mention ocReaper naming convention analyzer in your project's `ruleset.xml` and exclude only some sniffs: |
| 92 | + |
| 93 | +```xml |
| 94 | +<?xml version="1.0"?> |
| 95 | +<ruleset name="AcmeProject"> |
| 96 | + <rule ref="vendor/ocreaper/naming-convention-analyzer/NamingConventionAnalyzer/ruleset.xml"><!-- relative path to your ruleset.xml --> |
| 97 | + <!-- sniffs to exclude --> |
| 98 | + </rule> |
| 99 | +</ruleset> |
| 100 | +``` |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +## Fixing errors automatically |
| 105 | + |
| 106 | +Sniffs in this standard marked by the 🔧 symbol support [automatic fixing of coding standard violations](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically). To fix your code automatically, run phpcbf instead of phpcs: |
| 107 | + |
| 108 | +``` |
| 109 | +vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests |
| 110 | +``` |
| 111 | + |
| 112 | +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. |
| 113 | + |
| 114 | +## Contributing |
| 115 | + |
| 116 | +To make this repository work on your machine, clone it and run these two commands in the root directory of the repository: |
| 117 | + |
| 118 | +``` |
| 119 | +composer install |
| 120 | +``` |
0 commit comments