This extension is a work in progress. Use at your own risk.
This PHPStan extension adds custom rules to encourage and validate elegant object design principles in your PHP code.
It's based on Yegor Bugayenko's books :
- Elegant Objects (Volume 1)
- Elegant Objects (Volume 2)
composer require --dev atournayre/phpstan-elegant-objectInclude the extension in your phpstan.neon file:
# Include the extension config if you want extra strict rules
includes:
- vendor/atournayre/phpstan-elegant-object/phpstan.neonIt is recommended to configure each rule separately.
See configuration for a list of available rules.
To create a new rule:
- Create a class in the
src/Analyzer/folder that implementsAtournayre\PHPStan\ElegantObject\Analyzer\RuleAnalyzer - Define the node type to analyze with
getNodeType() - Override
shouldSkipAnalysis()if necessary - Implement your analysis logic in
analyze() - Create a class in the
src/Rules/folder that extendsAtournayre\PHPStan\ElegantObject\Rules\ComposableRule(see existing rules for examples) - Register your rule in
phpstan.neon
To run the tests:
vendor/bin/phpunitFrom the book:
- Never use -er names (chapter 1.1)
- Keep constructors code-free (chapter 1.3)
- Always use interface (chapter 2.3)
- Don't use public constant (chapter 2.5)
- Be immutable (chapter 2.6)
- Keep interfaces short (chapter 2.9)
- Expose fewer than 5 public methods (chapter 3.1)
- Don't use static methods (chapter 3.2)
- Never accept null arguments (chapter 3.3)
- Never use getters and setters (chapter 3.5)
- Don't use new outside of secondary constructors (chapter 3.6)
- Avoid type introspection and casting (chapter 3.7)
- Never return Null (chapter 4.1)
- Throw only checked exceptions (chapter 4.2)
- Be either final or abstract (chapter 4.3)
- Private static literals (chapter 5.4)
Other:
- All properties must be private
- Constructor objects only