|
| 1 | +# PHPStan Xefi Rules |
| 2 | + |
| 3 | +[](https://github.com/xefi/phpstan-xefi-rules) |
| 4 | +[](https://packagist.org/packages/xefi/phpstan-xefi-rules) |
| 5 | + |
| 6 | +> Some custom PHPStan extensions to improve the readability, maintainability and overall quality of your PHP code base. |
| 7 | +
|
| 8 | +Package based on PHPStan. You can find the source package [here](https://phpstan.org/). |
| 9 | + |
| 10 | +### Supported PHP versions |
| 11 | +| PHP version | This package version | |
| 12 | +|-------------|----------------------| |
| 13 | +| 7.4, 8.x | Latest | |
| 14 | + |
| 15 | +## How to use it |
| 16 | + |
| 17 | +**1**: First, you may use [Composer](https://getcomposer.org) to install this package as a development dependency into your project: |
| 18 | + |
| 19 | +```bash |
| 20 | +composer require --dev "xefi/phpstan-xefi-rules" |
| 21 | +``` |
| 22 | + |
| 23 | +**2**: Then, create a `phpstan.neon` or `phpstan.neon.dist` file in the root of your application. It might look like this: |
| 24 | + |
| 25 | +``` |
| 26 | +includes: |
| 27 | + - vendor/xefi/phpstan-xefi-rules/extension.neon |
| 28 | +
|
| 29 | +parameters: |
| 30 | + paths: |
| 31 | + - src/ |
| 32 | +
|
| 33 | + # 0 to 10, level 10 is the highest level |
| 34 | + level: 0 |
| 35 | +``` |
| 36 | + |
| 37 | +The most important part is the `includes` one, that enables the rules of this package. |
| 38 | + |
| 39 | +For all available options, please take a look at the PHPStan documentation: **https://phpstan.org/config-reference** |
| 40 | + |
| 41 | +**3**: Finally, you may start analyzing your code using the phpstan console command: |
| 42 | + |
| 43 | +```bash |
| 44 | +./vendor/bin/phpstan |
| 45 | +``` |
| 46 | + |
| 47 | +## Rules |
| 48 | + |
| 49 | +### Boolean Property Naming Rule |
| 50 | + |
| 51 | +*Identifier : xefi.booleanPropertyNaming* |
| 52 | + |
| 53 | +Ensure that a Boolean is always named with an “is” at the beginning of its name, to clarify the condition it represents. |
| 54 | + |
| 55 | +### Max Line Per Class Rule |
| 56 | + |
| 57 | +*Identifier : xefi.maxLinePerClass* |
| 58 | + |
| 59 | +Guarantee that a class is no more than 100 lines long, to clearly separate roles between classes and encourage clarification. |
| 60 | + |
| 61 | +### Max Line Per Method Rule |
| 62 | + |
| 63 | +*Identifier : xefi.maxLinePerMethod* |
| 64 | + |
| 65 | +Guarantee that a method is no more than 20 lines long, to separate roles between classes and encourage specification. |
| 66 | + |
| 67 | +### No Basic Exception Rule |
| 68 | + |
| 69 | +*Identifier : xefi.noBasicException* |
| 70 | + |
| 71 | +Ensure that no base exceptions are thrown in order to make custom and specific exceptions everywhere for better maintainability. |
| 72 | + |
| 73 | +### No Delete Cascade Rule (Laravel) |
| 74 | + |
| 75 | +*Identifier : xefi.noCascadeDeleteLaravel* |
| 76 | + |
| 77 | +Prevents cascading deletions in SQL to avoid uncontrolled deletions. |
| 78 | +This rule forces you to think about the need for a cascading deletion, and to handle such cases in your code if you need to, which also makes it possible to use the observers and events managed by the framework. |
| 79 | + |
| 80 | +This rule only works for Laravel. |
| 81 | +A Symfony rule is planned. |
| 82 | + |
| 83 | +### No Generic Word Rule |
| 84 | + |
| 85 | +*Identifier : xefi.noGenericWord* |
| 86 | + |
| 87 | +Prevent generic variable names such as `array`, `string`, `str`, `result`, `res`, `data` to encourage developers to use clear and detailed names such as `users`, `filteredUsers`, `usersCount`, `cars`, ... |
| 88 | + |
| 89 | +### No Laravel Observer Rule |
| 90 | + |
| 91 | +*Identifier : xefi.noLaravelObserver* |
| 92 | + |
| 93 | +Forbid the use of Observers in Laravel, as the behavior of observers is sometimes incomprehensible and uncontrollable. |
| 94 | +This rule encourages the use of Events & Listeners, whose behavior is similar but much more explicit and stable. |
| 95 | + |
| 96 | +### No Try Catch Rule |
| 97 | + |
| 98 | +*Identifier : xefi.noTryCatch* |
| 99 | + |
| 100 | +Prevent `try catch` usage to ensure that the user uses PHP exceptions. |
| 101 | +This makes the code more readable and errors are handled by the framework if there is one. |
0 commit comments