phpstan extensions
v6.6.0
Extensions for PHPStan
To use this extension, require it in Composer:
composer require --dev slam/phpstan-extensionsWhen you are using phpstan/extension-installer,
conf/slam-rules.neon will be automatically included.
Otherwise you need to include conf/slam-rules.neon in your phpstan.neon:
includes:
- vendor/slam/phpstan-extensions/conf/slam-rules.neonSlamPhpStanUnusedVariableRule: check for variable inside functions never used after initial assignmentSlamPhpStanMissingClosureParameterTypehintRule: requires parameter typehints for closures; WARNING: no PhpDoc
allowed, see phpstan/phpstan-strict-rules#87SlamPhpStanStringToClassRule: requires strings that refer to classes to be expressed with ::class notationSlamPhpStanGotoRule: no goto allowedSlamPhpStanClassNotationRule:
SlamPhpStanPhpUnitFqcnAnnotationRule: classes found in following PHPUnit annotations must exist:
@expectedException@covers@coversDefaultClass@usesSlamPhpStanAccessGlobalVariableWithinContextRule: inhibit the access to globals within
classes that extend or implement a certain class/interfaceSlamPhpStanAccessStaticPropertyWithinModelContextRule: inhibit the access to static attributes of a class within
classes that extend or implement a certain class/interface, useful to prohibit usage of singletons in modelsA not-now-rules.neon config is present for forbidding raw date system calls:
includes:
- vendor/slam/phpstan-extensions/conf/not-now-rules.neonThese rules forbid:
new DateTimeImmutable()new DateTime('yesterday')date('Y-m-d')time()strtotime('noon')You should instead rely on a clock abstraction like lcobucci/clock.
WARNING: the rules are not perfect, a user can tricks them easily; they are meant only to help the transition to a proper clock abstraction.
A symfony-rules.neon config is present for Symfony projects:
includes:
- vendor/slam/phpstan-extensions/conf/symfony-rules.neonWith the following configurations:
SlamPhpStanSymfonyFilesystemRule: forbid calls to raw filesystem functions well wrapped by
symfony/filesystem componentSlamPhpStanSymfonyProcessRule: forbid calls to raw system functions well wrapped by
symfony/process componentA yii-rules.neon config is present for Yii projects:
includes:
- vendor/slam/phpstan-extensions/conf/yii-rules.neonWith the following configurations:
SlamPhpStanAccessGlobalVariableWithinContextRule to deny the usage of $_GET, $_POST and other global variables
in models implementing yiidbActiveRecordInterface: accessing to singletons in models is considered an anti-patternSlamPhpStanAccessStaticPropertyWithinModelContextRule to deny the usage of yiiBaseYii static variables like
$app in models implementing yiidbActiveRecordInterface: accessing to singletons in models is considered an
anti-pattern