ergebnis / phpstan-rules
Provides additional rules for phpstan/phpstan.
Installs: 2 085 840
Dependents: 115
Suggesters: 3
Security: 0
Stars: 283
Watchers: 6
Forks: 14
Open Issues: 19
Type:phpstan-extension
Requires
- php: ^7.2 || ^8.0
- ext-mbstring: *
- nikic/php-parser: ^4.2.3
- phpstan/phpstan: ^1.0.0
Requires (Dev)
- doctrine/orm: ^2.10.2
- ergebnis/composer-normalize: ^2.15.0
- ergebnis/license: ^1.1.0
- ergebnis/php-cs-fixer-config: ^2.14.0
- ergebnis/test-util: ^1.5.0
- infection/infection: ~0.15.3
- nette/di: ^3.0.11
- phpstan/phpstan-deprecation-rules: ^1.0.0
- phpstan/phpstan-strict-rules: ^1.0.0
- phpunit/phpunit: ^8.5.21
- psalm/plugin-phpunit: ~0.16.1
- psr/container: ^1.0.0
- vimeo/psalm: ^4.12.0
- zendframework/zend-servicemanager: ^2.0.0
- dev-main
- 1.0.0
- 0.15.3
- 0.15.2
- 0.15.1
- 0.15.0
- 0.14.4
- 0.14.3
- 0.14.2
- 0.14.1
- 0.14.0
- 0.13.0
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.1
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-dependabot/composer/phpunit/phpunit-9.6.4
- dev-dependabot/composer/phpstan/phpstan-strict-rules-1.5.0
- dev-dependabot/github_actions/shivammathur/setup-php-2.24.0
- dev-dependabot/composer/doctrine/orm-2.14.1
- dev-dependabot/composer/nette/di-3.1.1
- dev-dependabot/github_actions/actions/stale-7
- dev-dependabot/composer/ergebnis/composer-normalize-2.28.3
- dev-dependabot/composer/psalm/plugin-phpunit-0.18.4
- dev-dependabot/composer/ergebnis/license-2.0.0
- dev-dependabot/composer/vimeo/psalm-4.30.0
- dev-dependabot/composer/infection/infection-0.26.6
- dev-dependabot/composer/ergebnis/test-util-1.6.0
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-4.16.0
- dev-fix/php7.4
- dev-fix/final-in-abstract-class
This package is auto-updated.
Last update: 2023-03-14 12:03:44 UTC
README
Provides additional rules for phpstan/phpstan
.
Installation
Run
composer require --dev ergebnis/phpstan-rules
Usage
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
💡 You probably want to use these rules on top of the rules provided by:
Rules
This package provides the following rules for use with phpstan/phpstan
:
Ergebnis\PHPStan\Rules\Classes\FinalRule
Ergebnis\PHPStan\Rules\Classes\NoExtendsRule
Ergebnis\PHPStan\Rules\Classes\PHPUnit\Framework\TestCaseWithSuffixRule
Ergebnis\PHPStan\Rules\Closures\NoNullableReturnTypeDeclarationRule
Ergebnis\PHPStan\Rules\Closures\NoParameterWithNullableTypeDeclarationRule
Ergebnis\PHPStan\Rules\Closures\NoParameterWithNullDefaultValueRule
Ergebnis\PHPStan\Rules\Expressions\NoCompactRule
Ergebnis\PHPStan\Rules\Expressions\NoEmptyRule
Ergebnis\PHPStan\Rules\Expressions\NoErrorSuppressionRule
Ergebnis\PHPStan\Rules\Expressions\NoEvalRule
Ergebnis\PHPStan\Rules\Expressions\NoIssetRule
Ergebnis\PHPStan\Rules\Files\DeclareStrictTypesRule
Ergebnis\PHPStan\Rules\Functions\NoNullableReturnTypeDeclarationRule
Ergebnis\PHPStan\Rules\Functions\NoParameterWithNullableTypeDeclaration
Ergebnis\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule
Ergebnis\PHPStan\Rules\Methods\FinalInAbstractClassRule
Ergebnis\PHPStan\Rules\Methods\NoConstructorParameterWithDefaultValueRule
Ergebnis\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule
Ergebnis\PHPStan\Rules\Methods\NoParameterWithContainerTypeDeclarationRule
Ergebnis\PHPStan\Rules\Methods\NoParameterWithNullableTypeDeclarationRule
Ergebnis\PHPStan\Rules\Methods\NoParameterWithNullDefaultValueRule
Ergebnis\PHPStan\Rules\Methods\PrivateInFinalClassRule
Ergebnis\PHPStan\Rules\Statements\NoSwitchRule
Classes
Classes\FinalRule
This rule reports an error when a non-anonymous class is not final
.
💡 This rule ignores classes that
- use
@Entity
,@ORM\Entity
, or@ORM\Mapping\Entity
annotations - use
Doctrine\ORM\Mapping\Entity
attributes
on the class level.
Disallowing abstract
classes
By default, this rule allows to declare abstract
classes. If you want to disallow declaring abstract
classes, you can set the allowAbstractClasses
parameter to false
:
parameters: ergebnis: allowAbstractClasses: false
Excluding classes from inspection
If you want to exclude classes from being inspected by this rule, you can set the classesNotRequiredToBeAbstractOrFinal
parameter to a list of class names:
parameters: ergebnis: classesNotRequiredToBeAbstractOrFinal: - Foo\Bar\NeitherAbstractNorFinal - Bar\Baz\NeitherAbstractNorFinal
Classes\NoExtendsRule
This rule reports an error when a class extends another class.
Defaults
By default, this rule allows the following classes to be extended:
Allowing classes to be extended
If you want to allow additional classes to be extended, you can set the classesAllowedToBeExtended
parameter to a list of class names:
parameters: ergebnis: classesAllowedToBeExtended: - Ergebnis\PHPStan\Rules\Test\Integration\AbstractTestCase - PHPStan\Testing\RuleTestCase
Classes\PHPUnit\Framework\TestCaseWithSuffixRule
This rule reports an error when a concrete class is a sub-class of PHPUnit\Framework\TestCase
but does not have a Test
suffix.
Closures
Closures\NoNullableReturnTypeDeclarationRule
This rule reports an error when a closure uses a nullable return type declaration.
Closures\NoParameterWithNullableTypeDeclarationRule
This rule reports an error when a closure has a parameter with a nullable type declaration.
Closures\NoParameterWithNullDefaultValueRule
This rule reports an error when a closure has a parameter with null
as default value.
Expressions
Expressions\NoCompactRule
This rule reports an error when the function compact()
is used.
Expressions\NoEmptyRule
This rule reports an error when the language construct empty()
is used.
Expressions\NoEvalRule
This rule reports an error when the language construct eval()
is used.
Expressions\NoErrorSuppressionRule
This rule reports an error when @
is used to suppress errors.
Expressions\NoIssetRule
This rule reports an error when the language construct isset()
is used.
Files
Files\DeclareStrictTypesRule
This rule reports an error when a non-empty file does not contain a declare(strict_types=1)
declaration.
Functions
Functions\NoNullableReturnTypeDeclarationRule
This rule reports an error when a function uses a nullable return type declaration.
Functions\NoParameterWithNullableTypeDeclarationRule
This rule reports an error when a function has a parameter with a nullable type declaration.
Functions\NoParameterWithNullDefaultValueRule
This rule reports an error when a function has a parameter with null
as default value.
Methods
Methods\FinalInAbstractClassRule
This rule reports an error when a concrete public
or protected
method in an abstract
class is not final
.
Methods\NoConstructorParameterWithDefaultValueRule
This rule reports an error when a constructor declared in
- an anonymous class
- a class
has a default value.
Methods\NoNullableReturnTypeDeclarationRule
This rule reports an error when a method declared in
- an anonymous class
- a class
- an interface
uses a nullable return type declaration.
Methods\NoParameterWithContainerTypeDeclarationRule
This rule reports an error when a method has a type declaration for a known dependency injection container or service locator.
Defaults
By default, this rule disallows the use of type declarations indicating an implementation of
is expected to be injected into a method.
Configuring container interfaces
If you want to configure the list of interfaces implemented by dependency injection containers and service locators yourself, you can set the interfacesImplementedByContainers
parameter to a list of interface names:
parameters: ergebnis: interfacesImplementedByContainers: - Fancy\DependencyInjection\ContainerInterface - Other\ServiceLocatorInterface
Methods\NoParameterWithNullableTypeDeclarationRule
This rule reports an error when a method declared in
- an anonymous class
- a class
- an interface
has a parameter with a nullable type declaration.
Methods\NoParameterWithNullDefaultValueRule
This rule reports an error when a method declared in
- an anonymous class
- a class
- an interface
has a parameter with null
as default value.
Methods\PrivateInFinalClassRule
This rule reports an error when a method in a final
class is protected
but could be private
.
Statements
Statements\NoSwitchRule
This rule reports an error when the statement switch()
is used.
Changelog
Please have a look at CHANGELOG.md
.
Contributing
Please have a look at CONTRIBUTING.md
.
Code of Conduct
Please have a look at CODE_OF_CONDUCT.md
.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.
Credits
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).
Curious what I am building?
📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.