https://github.com/thecodingmachine/phpstan-strict-rules
A set of additional rules for PHPStan based on best practices followed at TheCodingMachine
https://github.com/thecodingmachine/phpstan-strict-rules
Last synced: 6 months ago
JSON representation
A set of additional rules for PHPStan based on best practices followed at TheCodingMachine
- Host: GitHub
- URL: https://github.com/thecodingmachine/phpstan-strict-rules
- Owner: thecodingmachine
- Created: 2017-06-21T14:50:35.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-12-01T08:53:23.000Z (almost 4 years ago)
- Last Synced: 2025-05-10T13:05:54.254Z (6 months ago)
- Language: PHP
- Size: 105 KB
- Stars: 288
- Watchers: 18
- Forks: 25
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://packagist.org/packages/thecodingmachine/phpstan-strict-rules)
[](https://packagist.org/packages/thecodingmachine/phpstan-strict-rules)
[](https://packagist.org/packages/thecodingmachine/phpstan-strict-rules)
[](https://packagist.org/packages/thecodingmachine/phpstan-strict-rules)
[](https://scrutinizer-ci.com/g/thecodingmachine/phpstan-strict-rules/?branch=master)
[](https://travis-ci.org/thecodingmachine/phpstan-strict-rules)
[](https://coveralls.io/github/thecodingmachine/phpstan-strict-rules?branch=master)
TheCodingMachine's additional rules for PHPStan
===============================================
This package contains a set of rules to be added to the [wonderful PHPStan static analyzer](https://phpstan.org/).
Those rules come from [TheCodingMachine best practices](http://bestpractices.thecodingmachine.com/).
They are more "strict" than the default PHPStan rules and some may be controversial. We use those at TheCodingMachine, have found them to help us in our daily work, and ask anyone working with us to follow them.
## Rules list
### Exception related rules
- You should not throw the "Exception" base class directly [but throw a sub-class instead](http://bestpractices.thecodingmachine.com/php/error_handling.html#subtyping-exceptions).
- You should not have empty catch statements
- When throwing an exception inside a catch block, [you should pass the catched exception as the "previous" exception](http://bestpractices.thecodingmachine.com/php/error_handling.html#wrapping-an-exception-do-not-lose-the-previous-exception)
- If you catch a `Throwable`, an `Exception` or a `RuntimeException`, you must rethrow the exception.
### Superglobal related rules
- The use of [`$_GET`, `$_POST`, `$_FILES`, `$_COOKIE`, `$_SESSION`, `$_REQUEST` is forbidden](http://bestpractices.thecodingmachine.com/php/organize_your_code.html#stop-using-superglobals-).
You should instead use your framework's request/session object.
- Superglobal usage is still tolerated at the root scope (because it is typically used once in `index.php` to initialize
PSR-7 request object)
### Condition related rules
- Switch statements should always check for unexpected values by [implementing a default case (and throwing an exception)](http://bestpractices.thecodingmachine.com/php/defensive_programming.html#always-check-for-unexpected-values)
### Work-in-progress
// Never use public properties
// Never use globals
## Installation
We assume that [PHPStan](https://phpstan.org/) is already installed in your project.
To use this extension, require it in [Composer](https://getcomposer.org/):
```bash
composer require --dev thecodingmachine/phpstan-strict-rules
```
If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!
Manual installation
If you don't want to use `phpstan/extension-installer`, include phpstan-strict-rules.neon in your project's PHPStan config:
```yml
includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
```