Search by

danilovl / phpstan-rules

danilovl

Custom PHPStan rules shared by danilovl packages: no array shapes, no call chains as arguments, test method order and others

Package info

github.com/danilovl/phpstan-rules

Type:phpstan-extension

pkg:composer/danilovl/phpstan-rules

Statistics

Installs: 79

Dependents: 23

Suggesters: 0

Stars: 0

Open Issues: 0

v0.0.4 2026-12-02 23:13 UTC

This package is auto-updated.

Last update: 2026-12-02 23:14:41 UTC


README

phpunit downloads latest Stable Version license

PHPStan rules

About

Custom PHPStan rules shared by danilovl/* packages. They enforce code style that php-cs-fixer and PHPStan do not check: no call chains or sprintf() as arguments, no calls with arguments in conditions, typed constants, test method order and others.

Requirements

  • PHP 8.5 or higher
  • PHPStan 2.1 or higher

1. Installation

composer require --dev danilovl/phpstan-rules

With phpstan/extension-installer the rules are registered automatically.

Without it, include the extension in phpstan.neon:

includes:
    - vendor/danilovl/phpstan-rules/extension.neon

2. Rules

Identifier Rule Wrong Right
app.nestedArrayFunction no array_*() as an argument of another function array_values(array_unique($ids)) $unique = array_unique($ids); array_values($unique)
app.callInCondition no method call with arguments in if/elseif/while/do/ternary/foreach/match/switch if ($this->find($id) !== null) $entity = $this->find($id); if ($entity !== null)
app.computedArrayKey only a variable, constant or literal inside [...] $colors[count($a) % 3] $index = count($a) % 3; $colors[$index]
app.sprintfArgument no sprintf() result as an argument throw new LogicException(sprintf(...)) $message = sprintf(...); throw new LogicException($message)
app.concatArgument no string concatenation as an argument new Response('<p>' . $text . '</p>') $html = '<p>' . $text . '</p>'; new Response($html)
app.multilineArray an array with a nested array, with two or more objects or with three or more keyed items is written on several lines, and every item of a multi-line array is on its own line ['id' => $id, 'name' => $name, 'categoryId' => $categoryId], ['constraints' => [new NotBlank, new Positive]] one item per line
app.blankLineBeforeIf an empty line before if, unless it is the first statement of a block or its condition uses only the variable assigned on the line above $category = $this->find($id); + if ($name === '' || $category === null) empty line before if; $isValid = ...; + if ($isValid) may stay together
app.multilineParameters a function or method with three or more parameters, or with a signature line over 120 characters, declares one parameter per line public function sign(string $id, ?string $db = null, ?string $user = null): string one parameter per line
app.newArgument no object with several arguments created inside a call new JsonResponse(new Dto($a, $b)) $dto = new Dto($a, $b); new JsonResponse($dto)
app.callChainArgument no chain of two or more calls as an argument $this->assertSame(1, $a->b()->c()) $value = $a->b()->c(); $this->assertSame(1, $value)
app.singleParameterConstructor a constructor with one parameter is declared on one line; if the parameter has an attribute, the attribute and the parameter are each on their own line parameter on its own line; __construct(#[Autowire(...)] private Foo $foo) {} public function __construct(private Foo $foo) {}; __construct( + #[Autowire(...)] + private Foo $foo + ) {} on separate lines
app.testMethodOrder order in a TestCase: properties, setUp/tearDown, tests, data providers, helpers provider before a test providers after all tests
app.testMethodName test method name max 40 characters including test testPaymentsWithSameProductAreSplitByExactAmount testSameProductSplitByAmount
app.typedConstant class constants declare their type private const LIMIT = 15; private const int LIMIT = 15;
app.arrowFunction an arrow function is one line and does not return a call chain fn (Work $work): string => $work->getAuthor()->getName() move the logic into a method or use a closure
app.helperStaticMethod methods of classes in a Helper namespace under /src/ are static public function format(...) in Helper\DateHelper public static function format(...), or move the class to Service
app.dataProviderReturnType a data provider (#[DataProvider]) is a static method returning Generator public static function provideCases(): iterable public static function provideCases(): Generator
app.conditionalExpectException no expectException*() inside if/match/switch/ternary in a TestCase if ($fails) { $this->expectException(...); } separate tests for the success and the failure cases

Optional rules

These rules are off by default and are enabled in phpstan.neon.

Identifier Parameter Rule Wrong Right
app.noArrayShape danilovlRules.noArrayShape no array{...} in phpdoc of files under /src/ @return array{id: int} return a DTO
app.readonlyClass danilovlRules.readonlyClass a class without a parent whose properties are all readonly is declared readonly class final class Foo { public function __construct(private readonly Bar $bar) {} } final readonly class Foo { public function __construct(private Bar $bar) {} }
parameters:
    danilovlRules:
        readonlyClass: true
        noArrayShape: true

app.readonlyClass is optional because a readonly class can only be extended by another readonly class: in a library it breaks user classes that extend a public class and add a mutable property. Enable it in projects whose classes are not extension points, or ignore the extension points by path.

app.noArrayShape is optional because replacing an array{...} return type with a DTO changes the public API of a library. Enable it in applications, or in a library for a new major version.

Built-in functions with arguments (is_string($x), count($x)) are allowed in conditions; attribute arguments are not checked by app.newArgument.

A single rule can be ignored by its identifier in phpstan.neon:

parameters:
    ignoreErrors:
        -
            identifier: app.testMethodName
            path: tests/Legacy/*

3. Development

composer tests
composer phpstan
composer cs-fixer-check

License

The PHPStan rules are open-sourced software licensed under the MIT license.