2017 © Pedro Peláez
 

library phpstan-extensions

Slam extension of phpstan

image

slam/phpstan-extensions

Slam extension of phpstan

  • Monday, July 23, 2018
  • by Slam
  • Repository
  • 1 Watchers
  • 4 Stars
  • 4,886 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 9 Versions
  • 78 % Grown

The README.md

Slam PHPStan extensions

Latest Stable Version Downloads Integrate Code Coverage, (*1)

Extensions for PHPStan, (*2)

Installation

To use this extension, require it in Composer:, (*3)

composer require --dev slam/phpstan-extensions

Usage

When you are using phpstan/extension-installer, conf/slam-rules.neon will be automatically included., (*4)

Otherwise you need to include conf/slam-rules.neon in your phpstan.neon:, (*5)

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

Rules

  1. SlamPhpStan\UnusedVariableRule: check for variable inside functions never used after initial assignment
  2. SlamPhpStan\MissingClosureParameterTypehintRule: requires parameter typehints for closures; WARNING: no PhpDoc allowed, see phpstan/phpstan-strict-rules#87
  3. SlamPhpStan\StringToClassRule: requires strings that refer to classes to be expressed with ::class notation
  4. SlamPhpStan\GotoRule: no goto allowed
  5. 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"
  6. SlamPhpStan\PhpUnitFqcnAnnotationRule: classes found in following PHPUnit annotations must exist:
    1. @expectedException
    2. @covers
    3. @coversDefaultClass
    4. @uses
  7. SlamPhpStan\AccessGlobalVariableWithinContextRule: inhibit the access to globals within classes that extend or implement a certain class/interface
  8. 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

Not-NOW config

A not-now-rules.neon config is present for forbidding raw date system calls:, (*6)

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

These rules forbid:, (*7)

  1. new DateTimeImmutable()
  2. new DateTime('yesterday')
  3. date('Y-m-d')
  4. time()
  5. strtotime('noon')

You should instead rely on a clock abstraction like lcobucci/clock., (*8)

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., (*9)

Symfony-specific config

A symfony-rules.neon config is present for Symfony projects:, (*10)

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

With the following configurations:, (*11)

  1. SlamPhpStan\SymfonyFilesystemRule: forbid calls to raw filesystem functions well wrapped by symfony/filesystem component
  2. SlamPhpStan\SymfonyProcessRule: forbid calls to raw system functions well wrapped by symfony/process component

Yii-specific config

A yii-rules.neon config is present for Yii projects:, (*12)

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

With the following configurations:, (*13)

  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

The Versions