Skip to content

Phauthentic/phpstan-rules

Repository files navigation

PHPStan Rules

Additional rules for PHPStan, mostly focused on Clean Code and architecture conventions.

Usage

composer require/phauthentic/phpstan-rules --dev

Rules

Add them to your phpstan.neon configuration file under the section services.

Control Structure Nesting Rule

Ensures that the nesting level of if and try-catch statements does not exceed a specified limit.

Configuration Example:

    -
        class: Phauthentic\PhpstanRules\ControlStructureNestingRule
        arguments:
            maxNestingLevel: 2
        tags:
            - phpstan.rules.rule

Too Many Arguments Rule

Checks that methods do not have more than a specified number of arguments.

Configuration Example:

    -
        class: Phauthentic\PhpstanRules\TooManyArgumentsRule
        arguments:
            maxArguments: 3
        tags:
            - phpstan.rules.rule

Readonly Class Rule

Ensures that classes matching specified patterns are declared as readonly.

Configuration Example:

    -
        class: Phauthentic\PhpstanRules\ReadonlyClassRule
        arguments:
            patterns: ['/^App\\Controller\\/']
        tags:
            - phpstan.rules.rule

Dependency Constraints Rule

Enforces dependency constraints between namespaces by checking use statements.

The constructor takes an array of namespace dependencies. The key is the namespace that should not depend on the namespaces in the array of values.

In the example below nothing from App\Domain can depend on anything from App\Controller.

Configuration Example:

    -
        class: Phauthentic\PhpstanRules\DependencyConstraintsRule
        arguments:
            namespaceDependencies: [
                'App\\Domain\\': ['/^App\\Controller\\/']
            ]
        tags:
            - phpstan.rules.rule

Final Class Rule

Ensures that classes matching specified patterns are declared as final.

Configuration Example:

    -
        class: Phauthentic\PhpstanRules\FinalClassRule
        arguments:
            patterns: ['/^App\\Service\\/']
        tags:
            - phpstan.rules.rule

Namespace Class Pattern Rule

Ensures that classes inside namespaces matching a given regex must have names matching at least one of the provided patterns.

Configuration Example:

    -
        class: Phauthentic\PhpstanRules\NamespaceClassPatternRule
        arguments:
            namespaceClassPatterns: [
                [
                    namespace: '/^App\\Service$/',
                    classPatterns: ['/Class$/']
                ]
            ]
        tags:
            - phpstan.rules.rule

License

This library is under the MIT license.

Copyright Florian Krämer

About

PHPStan rules focused on architectural & clean code rules

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages