Advanced PHPStan Rules for Superior Code Quality
Enhance your PHP codebase with intelligent static analysis rules that detect code smells, enforce naming conventions, and promote best practices.
- β¨ Features
- π Quick Start
- π Available Rules
- π§ Configuration
- π― Inspiration
- π€ Contributing
- π License
- π Acknowledgments
- π Comprehensive 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
composer require --dev orrison/meliorstanAdd to your phpstan.neon configuration:
includes:
- vendor/orrison/meliorstan/config/extension.neon
rules:
- Orrison\MeliorStan\Rules\PascalCaseClassName\PascalCaseClassNameRule
- Orrison\MeliorStan\Rules\CamelCaseMethodName\CamelCaseMethodNameRuleCustomize rule behavior in your phpstan.neon:
parameters:
meliorstan:
pascal_case_class_name:
allow_consecutive_uppercase: false
camel_case_method_name:
allow_consecutive_uppercase: false
allow_underscore_prefix: false| Rule | Description | Target |
|---|---|---|
| BooleanGetMethodName | Prevents get* methods from returning boolean values |
Methods |
| CamelCase Method Name | Enforces camelCase for method names | Methods |
| CamelCase Parameter Name | Enforces camelCase for parameter names | Parameters |
| CamelCase Property Name | Enforces camelCase for property names | Properties |
| CamelCase Variable Name | 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 |
| PascalCase Class Name | 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 |
| 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 |
| Rule | Description | Target |
|---|---|---|
| ElseExpression | Discourages else expressions |
Control Flow |
| Rule | Description | Target |
|---|---|---|
| EmptyCatchBlock | Detects and reports empty catch blocks in exception handling | Catch Blocks |
| 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 |
| NumberOfChildren | Detects classes with too many direct child classes | Class Hierarchy |
Each rule supports extensive configuration options. Refer to individual rule documentation for detailed configuration parameters.
parameters:
meliorstan:
rule_name:
option1: value1
option2: value2includes:
- vendor/orrison/meliorstan/config/extension.neon
rules:
- Orrison\MeliorStan\Rules\PascalCaseClassName\PascalCaseClassNameRule
- Orrison\MeliorStan\Rules\CamelCaseMethodName\CamelCaseMethodNameRule
- Orrison\MeliorStan\Rules\LongClassName\LongClassNameRule
parameters:
meliorstan:
pascal_case_class_name:
allow_consecutive_uppercase: true
camel_case_method_name:
allow_consecutive_uppercase: false
allow_underscore_prefix: false
long_class_name:
maximum: 50
subtract_prefixes: ["Abstract", "Base"]
subtract_suffixes: ["Interface", "Trait"]Originally inspired by PHPMD - PHP Mess Detector, this project provides modern PHPStan equivalents with enhanced configurability and PHP 8+ features.
Note: While inspired by PHPMD, these rules are not exact replicas. They offer additional customization options and are adapted for PHPStan's architecture and modern PHP practices.
We welcome contributions! Please see our Contributing Guide for details.
git clone https://github.com/Orrison/MeliorStan.git
cd MeliorStan
composer install
composer test- Follow the established architecture pattern
- Include comprehensive tests
- Update documentation
- Ensure all checks pass:
composer format && composer analyze && composer test
This project is licensed under the MIT License - see the LICENSE file for details.
- PHPStan - The foundation of modern PHP static analysis
- PHPMD - Original inspiration for code quality rules
- PHP-Parser - AST parsing capabilities
- PHP Community - For continuous improvement of PHP tooling
Made with β€οΈ for the PHP community
π Documentation β’ π Report Issues β’ π‘ Request Features