ergebnis/phpstan-rules
A set of extra PHPStan rules to enforce stricter, opinionated code standards in PHP projects. Install via Composer and include rules.neon (auto via phpstan/extension-installer) to catch issues like named arguments, class design, and more.
This project provides a composer package with rules for phpstan/phpstan.
Run
composer require --dev ergebnis/phpstan-rules
All of the rules provided (and used) by this library are included in rules.neon.
When you are using phpstan/extension-installer, rules.neon will be automatically included.
Otherwise you need to include rules.neon in your phpstan.neon:
includes:
- vendor/ergebnis/phpstan-rules/rules.neon
:bulb: You probably want to use these rules on top of the rules provided by:
This package provides the following rules for use with phpstan/phpstan:
Ergebnis\PHPStan\Rules\CallLikes\NoNamedArgumentRuleErgebnis\PHPStan\Rules\Classes\FinalRuleErgebnis\PHPStan\Rules\Classes\NoExtendsRuleErgebnis\PHPStan\Rules\Classes\PHPUnit\Framework\TestCaseWithSuffixRuleErgebnis\PHPStan\Rules\Closures\NoNullableReturnTypeDeclarationRuleErgebnis\PHPStan\Rules\Closures\NoParameterPassedByReferenceRuleErgebnis\PHPStan\Rules\Closures\NoParameterWithNullableTypeDeclarationRuleErgebnis\PHPStan\Rules\Closures\NoParameterWithNullDefaultValueRuleErgebnis\PHPStan\Rules\Expressions\NoAssignByReferenceRuleErgebnis\PHPStan\Rules\Expressions\NoCompactRuleErgebnis\PHPStan\Rules\Expressions\NoErrorSuppressionRuleErgebnis\PHPStan\Rules\Expressions\NoEvalRuleErgebnis\PHPStan\Rules\Expressions\NoIssetRuleErgebnis\PHPStan\Rules\Files\DeclareStrictTypesRuleErgebnis\PHPStan\Rules\Files\NoPhpstanIgnoreRuleErgebnis\PHPStan\Rules\Functions\NoNullableReturnTypeDeclarationRuleErgebnis\PHPStan\Rules\Functions\NoParameterPassedByReferenceRuleErgebnis\PHPStan\Rules\Functions\NoParameterWithNullableTypeDeclarationRuleErgebnis\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRuleErgebnis\PHPStan\Rules\Functions\NoReturnByReferenceRuleErgebnis\PHPStan\Rules\Methods\FinalInAbstractClassRuleErgebnis\PHPStan\Rules\Methods\InvokeParentHookMethodRuleErgebnis\PHPStan\Rules\Methods\NoConstructorParameterWithDefaultValueRuleErgebnis\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRuleErgebnis\PHPStan\Rules\Methods\NoParameterPassedByReferenceRuleErgebnis\PHPStan\Rules\Methods\NoParameterWithContainerTypeDeclarationRuleErgebnis\PHPStan\Rules\Methods\NoParameterWithNullableTypeDeclarationRuleErgebnis\PHPStan\Rules\Methods\NoParameterWithNullDefaultValueRuleErgebnis\PHPStan\Rules\Methods\NoReturnByReferenceRuleErgebnis\PHPStan\Rules\Methods\PrivateInFinalClassRuleErgebnis\PHPStan\Rules\Statements\NoSwitchRuleCallLikes\NoNamedArgumentRuleThis rule reports an error when an anonymous function, a function, or a method is invoked using a named argument.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noNamedArgument:
enabled: false
Classes\FinalRuleThis rule reports an error when a non-anonymous class is not final.
:bulb: This rule ignores classes that
@Entity, @ORM\Entity, or @ORM\Mapping\Entity annotationsDoctrine\ORM\Mapping\Entity attributeson the class level.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
final:
enabled: false
abstract classesBy default, this rule allows to declare abstract classes.
You can set the allowAbstractClasses parameter to false to disallow abstract classes.
parameters:
ergebnis:
final:
allowAbstractClasses: false
You can set the classesNotRequiredToBeAbstractOrFinal parameter to a list of class names that you want to exclude from inspection.
parameters:
ergebnis:
final:
classesNotRequiredToBeAbstractOrFinal:
- Foo\Bar\NeitherAbstractNorFinal
- Bar\Baz\NeitherAbstractNorFinal
Classes\NoExtendsRuleThis rule reports an error when a class extends another class.
By default, this rule allows the following classes to be extended:
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noExtends:
enabled: false
You can set the classesAllowedToBeExtended parameter to a list of class names that you want to allow to be extended.
parameters:
ergebnis:
noExtends:
classesAllowedToBeExtended:
- Ergebnis\PHPStan\Rules\Test\Integration\AbstractTestCase
- Ergebnis\PHPStan\Rules\Test\Integration\AbstractTestCase
Classes\PHPUnit\Framework\TestCaseWithSuffixRuleThis rule reports an error when a concrete class is a sub-class of PHPUnit\Framework\TestCase but does not have a Test suffix.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
testCaseWithSuffix:
enabled: false
Closures\NoNullableReturnTypeDeclarationRuleThis rule reports an error when a closure uses a nullable return type declaration.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noNullableReturnTypeDeclaration:
enabled: false
Closures\NoParameterPassedByReferenceRuleThis rule reports an error when a closure has a parameter that is passed by reference.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterPassedByReference:
enabled: false
Closures\NoParameterWithNullableTypeDeclarationRuleThis rule reports an error when a closure has a parameter with a nullable type declaration.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterWithNullableTypeDeclaration:
enabled: false
Closures\NoParameterWithNullDefaultValueRuleThis rule reports an error when a closure has a parameter with null as default value.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterWithNullDefaultValue:
enabled: false
Expressions\NoAssignByReferenceRuleThis rule reports an error when a variable is assigned by reference.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noAssignByReference:
enabled: false
Expressions\NoCompactRuleThis rule reports an error when the function compact() is used.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noCompact:
enabled: false
Expressions\NoErrorSuppressionRuleThis rule reports an error when @ is used to suppress errors.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noErrorSuppression:
enabled: false
Expressions\NoEvalRuleThis rule reports an error when the language construct eval() is used.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noEval:
enabled: false
Expressions\NoIssetRuleThis rule reports an error when the language construct isset() is used.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noIsset:
enabled: false
Files\DeclareStrictTypesRuleThis rule reports an error when a non-empty file does not contain a declare(strict_types=1) declaration.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
declareStrictTypes:
enabled: false
Files\NoPhpstanIgnoreRuleThis rule reports an error when a @phpstan-ignore, @phpstan-ignore-line, or @phpstan-ignore-next-line tag is used to ignore errors reported by phpstan/phpstan.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noPhpstanIgnore:
enabled: false
Functions\NoNullableReturnTypeDeclarationRuleThis rule reports an error when a function uses a nullable return type declaration.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noNullableReturnTypeDeclaration:
enabled: false
Functions\NoParameterPassedByReferenceRuleThis rule reports an error when a function has a parameter that is passed by reference.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterPassedByReference:
enabled: false
Functions\NoParameterWithNullableTypeDeclarationRuleThis rule reports an error when a function has a parameter with a nullable type declaration.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterWithNullableTypeDeclaration:
enabled: false
Functions\NoParameterWithNullDefaultValueRuleThis rule reports an error when a function has a parameter with null as default value.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterWithNullDefaultValue:
enabled: false
Functions\NoReturnByReferenceRuleThis rule reports an error when a function returns by reference.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noReturnByReference:
enabled: false
Methods\FinalInAbstractClassRuleThis rule reports an error when a concrete public or protected method in an abstract class is not final.
:bulb: This rule ignores
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
finalInAbstractClass:
enabled: false
Methods\InvokeParentHookMethodRuleThis rule reports an error when a hook method that overrides a hook method in a parent class does not invoke the overridden hook method in the expected order.
By default, this rule requires the following hook methods to be invoked before doing something in the overriding method:
Codeception\PHPUnit\TestCase::_setUp()Codeception\PHPUnit\TestCase::_setUpBeforeClass()Codeception\Test\Unit::_before()Codeception\Test\Unit::_setUp()PHPUnit\Framework\TestCase::assertPreConditions()PHPUnit\Framework\TestCase::setUp()PHPUnit\Framework\TestCase::setUpBeforeClass()By default, this rule requires the following hook methods to be invoked after doing something in the overriding method:
Codeception\PHPUnit\TestCase::_tearDown()Codeception\PHPUnit\TestCase::_tearDownAfterClass()Codeception\Test\Unit::_after()Codeception\Test\Unit::_tearDown()PHPUnit\Framework\TestCase::assertPostConditions()PHPUnit\Framework\TestCase::tearDown()PHPUnit\Framework\TestCase::tearDownAfterClass()You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
invokeParentHookMethod:
enabled: false
You can set the hookMethods parameter to a list of hook methods:
parameters:
ergebnis:
invokeParentHookMethod:
hookMethods:
- className: "Example\Test\Functional\AbstractCest"
methodName: "_before"
hasContent: "yes"
invocation: "first"
className: name of the class that declares the hook methodmethodName: name of the hook methodhasContent: one of "yes", "no", "maybe"invocation: one of "any" (needs to be invoked), "first" (needs to be invoked before all other statements in the overriding hook method, "last" (needs to be invoked after all other statements in the overriding hook method)Methods\NoConstructorParameterWithDefaultValueRuleThis rule reports an error when a constructor declared in
has a default value.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noConstructorParameterWithDefaultValue:
enabled: false
Methods\NoParameterPassedByReferenceRuleThis rule reports an error when a method has a parameter that is passed by reference.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterPassedByReference:
enabled: false
Methods\NoNullableReturnTypeDeclarationRuleThis rule reports an error when a method declared in
uses a nullable return type declaration.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noNullableReturnTypeDeclaration:
enabled: false
Methods\NoParameterWithContainerTypeDeclarationRuleThis rule reports an error when a method has a type declaration for a known dependency injection container or service locator.
By default, this rule disallows the use of type declarations indicating an implementation of
is expected to be injected into a method.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterWithContainerTypeDeclaration:
enabled: false
You can set the interfacesImplementedByContainers parameter to a list of interface names of additional containers and service locators.
parameters:
ergebnis:
noParameterWithContainerTypeDeclaration:
interfacesImplementedByContainers:
- Fancy\DependencyInjection\ContainerInterface
- Other\ServiceLocatorInterface
You can set the methodsAllowedToUseContainerTypeDeclarations parameter to a list of method names that are allowed to use parameters with container type declarations.
parameters:
ergebnis:
noParameterWithContainerTypeDeclaration:
methodsAllowedToUseContainerTypeDeclarations:
- loadExtension
Methods\NoParameterWithNullableTypeDeclarationRuleThis rule reports an error when a method declared in
has a parameter with a nullable type declaration.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterWithNullableTypeDeclaration:
enabled: false
Methods\NoParameterWithNullDefaultValueRuleThis rule reports an error when a method declared in
has a parameter with null as default value.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noParameterWithNullDefaultValue:
enabled: false
Functions\NoReturnByReferenceRuleThis rule reports an error when a method returns by reference.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noReturnByReference:
enabled: false
Methods\PrivateInFinalClassRuleThis rule reports an error when a method in a final class is protected but could be private.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
privateInFinalClass:
enabled: false
Statements\NoSwitchRuleThis rule reports an error when the statement switch() is used.
You can set the enabled parameter to false to disable this rule.
parameters:
ergebnis:
noSwitch:
enabled: false
You can disable all rules using the allRules configuration parameter:
parameters:
ergebnis:
allRules: false
If you have disabled all rules using the allRules configuration parameter, you can re-enable individual rules with their corresponding configuration parameters:
parameters:
ergebnis:
allRules: false
privateInFinalClass:
enabled: true
The maintainers of this project record notable changes to this project in a changelog.
The maintainers of this project suggest following the contribution guide.
The maintainers of this project ask contributors to follow the code of conduct.
The maintainers of this project provide limited support.
You can support the maintenance of this project by sponsoring @ergebnis.
This project supports PHP versions with active and security support.
The maintainers of this project add support for a PHP version following its initial release and drop support for a PHP version when it has reached the end of security support.
This project has a security policy.
This project uses the MIT license.
The method FinalRule::isWhitelistedClass() is inspired by the work on FinalClassFixer and FinalInternalClassFixer, contributed by Dariusz Rumiński, Filippo Tessarotto, and Spacepossum for friendsofphp/php-cs-fixer (originally licensed under MIT).
Follow @localheinz and @ergebnis on Twitter.
How can I help you explore Laravel packages today?