phauthentic / phpstan-rules
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.12
This package is auto-updated.
Last update: 2025-06-15 18:32:25 UTC
README
Additional rules for PHPStan, mostly focused on Clean Code and architecture conventions.
Usage
composer require phauthentic/phpstan-rules --dev
Rules
See Rules documentation for a list of available rules and configuration examples.
Available Rules:
- Control Structure Nesting Rule
- Too Many Arguments Rule
- Readonly Class Rule
- Dependency Constraints Rule
- Final Class Rule
- Namespace Class Pattern Rule
Using Regex in Rules
A lot of the rules use regex patterns to match things. Many people are not good at writing them but thankfully there is AI today.
If you struggle to write the regex patterns you need, you can use AI tools like ChatGPT to help you generate them. Just describe what you want to match, and it can provide you with a regex pattern that fits your needs. The regex can be tested using online tools like regex101.
Why PHPStan to enforce Architectural Rules?
Because PHPStan is a widely used static analysis tool in the PHP community. It already provides a solid foundation for code quality checks, and adding custom rules allows you to enforce specific coding standards and architectural constraints is just a logical choice. You won't need more 3rd party tools to enforce your architectural constraints.
It is also more or less easy to write your own rules if you need to enforce something specific that is not covered by the existing rules.
Alternative Tools
If you don't like this library, you can also check out other tools. Some of them provide a fluent interface instead of a Regex. If this feels more comfortable for you, you might want to check them out:
- Deptrac - Checks dependencies between namespaces and classes.
- PHP Architecture Tester - A tool to enforce architectural rules in PHP applications.
- PHPArkitect - A tool to enforce architectural rules in PHP applications.
- PHPMD - A tool that scans PHP source code and looks for potential problems.
License
This library is under the MIT license.
Copyright Florian Krämer