orrison / meliorstan
Provides PHPStan rules for improved code quality by detecting code smells and possible issues. In addition to enforcing particular naming and code style conventions to reduce bike-shedding.
Requires
- php: ^8.3
- phpstan/phpstan: ^2.1
Requires (Dev)
- brianium/paratest: ^7.10
- friendsofphp/php-cs-fixer: ^3.76
- phpunit/phpunit: ^12.2
This package is auto-updated.
Last update: 2026-04-25 05:19:40 UTC
README
MeliorStan
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.
Table of Contents
Features
- 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
Quick Start
Installation
composer require --dev orrison/meliorstan
Basic Usage
Add to your phpstan.neon configuration:
includes: - vendor/orrison/meliorstan/config/extension.neon rules: - Orrison\MeliorStan\Rules\PascalCaseClassName\PascalCaseClassNameRule - Orrison\MeliorStan\Rules\CamelCaseMethodName\CamelCaseMethodNameRule
Configuration
Customize 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
Available Rules
Naming Conventions
| 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 |
| 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 |
Code Quality
| 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 |
Control Flow
| Rule | Description | Target |
|---|---|---|
| ElseExpression | Discourages else expressions |
Control Flow |
| IfStatementAssignment | Detects assignments inside if and elseif conditions |
Control Flow |
Design
| 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 |
| ExcessivePublicCount | Detects classes with an excessive public API surface (public methods + properties) | Classes, Interfaces, Traits, Enums |
| CyclomaticComplexity | Detects methods with high cyclomatic complexity | Methods, 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 |
Already Covered by PHPStan
These PHPMD rules are intentionally not reimplemented because PHPStan already provides equivalent (or better) detection out of the box. The linked documentation explains how to enable and configure them.
| PHPMD Rule | PHPStan Equivalent | Enable At |
|---|---|---|
| DuplicatedArrayKey | PHPStan\Rules\Arrays\DuplicateKeysInLiteralArraysRule |
Level 0 (default) |
| UnusedPrivateMethod | PHPStan\Rules\DeadCode\UnusedPrivateMethodRule |
Rule level 4 |
| UnusedPrivateField | PHPStan\Rules\DeadCode\UnusedPrivatePropertyRule |
Rule level 4 |
| UndefinedVariable | PHPStan\Rules\Variables\DefinedVariableRule |
Level 0 (always undefined) / Level 1 (possibly undefined) |
Configuration
Each rule supports extensive configuration options. Refer to individual rule documentation for detailed configuration parameters.
Global Configuration Structure
parameters: meliorstan: rule_name: option1: value1 option2: value2
Example: Comprehensive Setup
includes: - 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"]
Inspiration
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 may offer additional or renamed customization options and are adapted for PHPStan's architecture and modern PHP practices.
Contributing
We welcome contributions! Please see our Contributing Guide for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.