Advanced PHPStan Rules for Code Quality and Consistency
Enhance your PHP codebase with intelligent static analysis rules that detect code smells, enforce naming conventions, and promote best practices.
- Code Analysis: Detect code smells and potential issues
- Naming Convention Enforcement: Ensure consistent naming across your codebase
- Highly Configurable: Customize rules to match your project's standards
- Modern PHP Support: Built for PHP 8.3+ with PHPStan 2.1+
- Extensive Documentation: Detailed guides for each rule
- Well Tested: Comprehensive test suite ensuring reliability
Install the package:
composer require --dev orrison/meliorstanMeliorStan does not auto-register any rules. Including vendor/orrison/meliorstan/config/extension.neon only loads the parameter schemas and config services that the rules need. There is no phpstan/extension-installer integration, so every rule you want to run must be listed explicitly under rules: in your PHPStan config. This is intentional: each rule is opt-in so you can pick only the ones that fit your project.
A minimal phpstan.neon enabling and configuring two rules looks like this:
includes:
- vendor/orrison/meliorstan/config/extension.neon
rules:
- Orrison\MeliorStan\Rules\PascalCaseClassName\PascalCaseClassNameRule
- Orrison\MeliorStan\Rules\CamelCaseMethodName\CamelCaseMethodNameRule
parameters:
meliorstan:
pascal_case_class_name:
allow_consecutive_uppercase: false
camel_case_method_name:
allow_consecutive_uppercase: false
allow_underscore_prefix: falseAdd or remove entries under rules: to control which checks run. Each rule's available options, defaults, and behavior are documented on its own page, linked from Available Rules below.
| Rule | Description | Target |
|---|---|---|
| BooleanGetMethodName | Prevents get* methods from returning boolean values |
Methods |
| CamelCaseMethodName | Enforces camelCase for method names | Methods |
| CamelCaseParameterName | Enforces camelCase for parameter names | Parameters |
| CamelCasePropertyName | Enforces camelCase for property names | Properties |
| CamelCaseVariableName | Enforces camelCase for variable names | Variables |
| ConstantNamingConventions | Enforces UPPERCASE for constants | Constants |
| ConstructorWithNameAsEnclosingClass | Prevents methods with same name as their class | Methods |
| LongClassName | Limits class/interface/trait/enum name length | Classes, Interfaces, Traits, Enums |
| PascalCaseClassName | Enforces PascalCase for class names | Classes |
| ShortClassName | Enforces minimum class/interface/trait/enum name length | Classes, Interfaces, Traits, Enums |
| TraitConstantNamingConventions | Enforces UPPERCASE for trait constants | Trait Constants |
| Rule | Description | Target |
|---|---|---|
| BooleanArgumentFlag | Detects boolean parameters in functions and methods that may indicate multiple responsibilities | Methods, Functions, Closures |
| LongVariable | Limits variable name length | Variables |
| MissingClosureParameterTypehint | Requires type hints on closure parameters | Closures |
| MissingImport | Detects types referenced by fully qualified name instead of being imported with a use statement |
Type-reference positions |
| ShortMethodName | Enforces minimum method name length | Methods |
| ShortVariable | Enforces minimum variable name length | Variables |
| ForbidPestPhpOnly | Prevents committed Pest tests from using the only() filter |
Tests |
| Superglobals | Discourages use of PHP superglobals | Superglobal Usage |
| UnusedFormalParameter | Detects function/method/closure parameters that are declared but never used | Function/method/closure parameters |
| UnusedLocalVariable | Detects local variables assigned but never read inside a function/method/closure | Local Variables |
| Rule | Description | Target |
|---|---|---|
| ElseExpression | Discourages else expressions |
Control Flow |
| IfStatementAssignment | Detects assignments inside if and elseif conditions |
Control Flow |
| Rule | Description | Target |
|---|---|---|
| DevelopmentCodeFragment | Detects calls to development/debug functions like var_dump, print_r, etc. | Function Calls |
| EmptyCatchBlock | Detects and reports empty catch blocks in exception handling | Catch Blocks |
| ErrorControlOperator | Detects use of the error control operator (@), which silently suppresses errors instead of handling them properly | Error Suppression |
| ForbidCountInLoopExpressions | Detects usage of count() or sizeof() in loop conditions | Loop Conditions |
| ForbidEvalExpressions | Detects and reports usage of eval expressions | Eval Expressions |
| ForbidExitExpressions | Detects and reports usage of exit and die expressions | Exit Expressions |
| ForbidGotoStatements | Detects and reports usage of goto statements | Goto Statements |
| CouplingBetweenObjects | Detects classes with too many type dependencies | Classes, Interfaces, Traits, Enums |
| DepthOfInheritance | Detects classes with excessively deep inheritance chains | Classes |
| ExcessiveClassLength | Detects classes, interfaces, traits, and enums with excessive line counts | Classes, Interfaces, Traits, Enums |
| ExcessiveMethodLength | Detects methods, functions, and closures with excessive line counts | Methods, Functions, Closures |
| ExcessiveClassComplexity | Detects classes with excessive total cyclomatic complexity (Weighted Method Count) | Classes, Interfaces, Traits, Enums |
| ExcessiveParameterList | Detects functions, methods, closures, and arrow functions with too many parameters | Functions, Methods, Closures, Arrow Functions |
| ExcessivePublicCount | Detects classes with an excessive public API surface (public methods + properties) | Classes, Interfaces, Traits, Enums |
| CyclomaticComplexity | Detects methods, functions, and classes with high cyclomatic complexity | Methods, Functions, Classes |
| NpathComplexity | Detects methods and functions with high NPath complexity (number of acyclic execution paths) | Methods, Functions |
| CognitiveComplexity | Detects methods, functions, and classes with high Cognitive Complexity, the SonarSource understandability metric with a nesting penalty | Methods, Functions, Classes |
| NumberOfChildren | Detects classes with too many direct child classes | Class Hierarchy |
| StaticAccess | Detects static method calls and optionally static property access that create tight coupling | Static Access |
| TooManyFields | Detects classes and traits with an excessive number of instance fields | Classes, Traits |
| TooManyMethods | Detects classes with too many methods | Classes, Interfaces, Traits, Enums |
| TooManyPublicMethods | Detects classes with too many public methods | Classes, Interfaces, Traits, Enums |
We welcome contributions! Please see our Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.
AI assistants (Claude and similar tools) are used variably in the development and maintenance of this project, including for research, planning, drafting documentation, and at times generating or refactoring code. A significant portion of the code is still written by hand, and all code, tests, and documentation are reviewed, edited, and approved by human maintainers and contributors before being committed or released. The maintainers and contributors are the authors of, and are fully accountable for, everything in this repository. If you find a bug or quality issue, treat it as a project issue and report it normally; AI involvement is never an excuse.
- PHPStan: the foundation of modern PHP static analysis that MeliorStan builds on.
- PHPMD - PHP Mess Detector: the original inspiration for many of the code quality rules. MeliorStan is not a direct 1:1 port; overlapping rules have diverged in behavior and configuration. See MeliorStan and PHPMD for more information.