Skip to content
Extensions for PHPStan
PHP
Branch: master
Clone or download

README.md

Slam PHPStan extensions

Build Status Code Coverage Packagist Total Downloads

Extensions for PHPStan

Installation

To use this extension, require it in Composer:

composer require --dev slam/phpstan-extensions

Usage

When 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.neon

Rules

  1. SlamPhpStan\DateTimeImmutableAlteredAndUnusedRule: check for DateTimeImmutable calls that alter the object but don't use the result; likely an error of the transition from mutable DateTime class
  2. SlamPhpStan\SymfonyFilesystemRule: forbid calls to raw filesystem functions well wrapped by symfony/filesystem component
  3. SlamPhpStan\SymfonyProcessRule: forbid calls to raw system functions well wrapped by symfony/process component
  4. SlamPhpStan\UnusedVariableRule: check for variable inside functions never used after initial assignment
  5. SlamPhpStan\StringToClassRule: requires strings that refer to classes to be expressed with ::class notation
  6. SlamPhpStan\GotoRule: no goto allowed
  7. SlamPhpStan\ClassNotationRule:
    1. Interfaces must end with "Interface"
    2. Traits must end with "Trait"
    3. Abstract classes must start with "Abstract"
    4. Exceptions must end with "Exception"
  8. SlamPhpStan\PhpUnitFqcnAnnotationRule: classes found in following PHPUnit annotations must exist:
    1. @expectedException
    2. @covers
    3. @coversDefaultClass
    4. @uses
  9. SlamPhpStan\AccessGlobalVariableWithinContextRule: inhibit the access to globals within classes that extend or implement a certain class/interface
  10. SlamPhpStan\AccessStaticPropertyWithinModelContextRule: 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 models

Yii-specific config

A yii-rules.neon config is present for Yii projects:

includes:
    - vendor/slam/phpstan-extensions/conf/yii-rules.neon

With the following configurations:

  1. SlamPhpStan\AccessGlobalVariableWithinContextRule to deny the usage of $_GET, $_POST and other global variables in models implementing yii\db\ActiveRecordInterface: accessing to singletons in models is considered an anti-pattern
  2. SlamPhpStan\AccessStaticPropertyWithinModelContextRule to deny the usage of yii\BaseYii static variables like $app in models implementing yii\db\ActiveRecordInterface: accessing to singletons in models is considered an anti-pattern
You can’t perform that action at this time.