Forbids specific static method calls matching regex patterns. This rule checks static method calls against a configurable list of forbidden patterns and supports namespace-level, class-level, and method-level granularity.
The rule resolves self, static, and parent keywords to the actual class name before matching, so forbidden patterns work correctly even when these keywords are used.
-
class: Phauthentic\PHPStanRules\Architecture\ForbiddenStaticMethodsRule
arguments:
forbiddenStaticMethods:
- '/^App\\Legacy\\.*::.*/'
- '/^App\\Utils\\StaticHelper::.*/'
- '/^DateTime::createFromFormat$/'
tags:
- phpstan.rules.ruleforbiddenStaticMethods: Array of regex patterns to match against static method calls. Patterns are matched against the formatFQCN::methodName.
Patterns match against the fully qualified class name followed by :: and the method name. This allows you to forbid static calls at different levels of granularity:
Forbid all static calls to any class in a namespace:
forbiddenStaticMethods:
- '/^App\\Legacy\\.*::.*/'This forbids calls like App\Legacy\LegacyHelper::doSomething() and App\Legacy\OldService::run().
Forbid all static calls on a specific class:
forbiddenStaticMethods:
- '/^App\\Utils\\StaticHelper::.*/'This forbids all static method calls on App\Utils\StaticHelper, regardless of the method name.
Forbid a specific static method on a specific class:
forbiddenStaticMethods:
- '/^DateTime::createFromFormat$/'This forbids only DateTime::createFromFormat() while allowing other static methods like DateTime::getLastErrors().
Prevent usage of legacy static helper classes to encourage dependency injection:
-
class: Phauthentic\PHPStanRules\Architecture\ForbiddenStaticMethodsRule
arguments:
forbiddenStaticMethods:
- '/^App\\Legacy\\.*::.*/'
- '/^App\\Helpers\\.*::.*/'
tags:
- phpstan.rules.ruleForbid using static factory methods on certain classes while allowing other static methods:
-
class: Phauthentic\PHPStanRules\Architecture\ForbiddenStaticMethodsRule
arguments:
forbiddenStaticMethods:
- '/^DateTime::createFromFormat$/'
- '/^DateTime::createFromTimestamp$/'
tags:
- phpstan.rules.ruleCombine with a broad pattern to forbid all static calls from specific namespaces:
-
class: Phauthentic\PHPStanRules\Architecture\ForbiddenStaticMethodsRule
arguments:
forbiddenStaticMethods:
- '/^Illuminate\\Support\\Facades\\.*::.*/'
tags:
- phpstan.rules.ruleThe rule resolves the keywords self, static, and parent to the actual fully qualified class name before matching against the forbidden patterns. This means:
self::create()insideApp\Service\MyServiceis matched asApp\Service\MyService::createstatic::create()insideApp\Service\MyServiceis matched asApp\Service\MyService::createparent::create()inside a child class is matched against the parent class name
Dynamic class names (e.g., $class::method()) and dynamic method names (e.g., DateTime::$method()) are skipped.