22
33[ ![ Downloads] ( https://img.shields.io/packagist/dt/symplify/phpstan-rules.svg?style=flat-square )] ( https://packagist.org/packages/symplify/phpstan-rules/stats )
44
5- Set of rules for PHPStan used by Symplify projects
6-
7- - See [ Rules Overview] ( docs/rules_overview.md )
5+ Set of 35 custom PHPStan rules that check architecture, typos, class namespace locations, accidental visibility override and more. Useful for any type of PHP project, from legacy to modern stack.
86
97<br >
108
@@ -18,7 +16,7 @@ composer require symplify/phpstan-rules --dev
1816
1917<br >
2018
21- ## 1 . Add Prepared Sets
19+ ## A . Add Prepared Sets
2220
2321Sets are bunch of rules grouped by a common area, e.g. improve naming. You can pick from 5 sets:
2422
@@ -30,20 +28,9 @@ includes:
3028 - vendor/symplify/phpstan-rules/config/static-rules.neon
3129` ` `
3230
33- Add sets one by one, fix what you find useful and ignore the rest.
34-
35- <br>
36-
37- Do you write custom [Rector](http://github.com/rectorphp/rector-src) rules? Add rules for them too:
38-
39- ` ` ` yaml
40- includes :
41- - vendor/symplify/phpstan-rules/config/rector-rules.neon
42- ` ` `
43-
4431<br>
4532
46- ## 2 . Cherry-pick Configurable Rules
33+ ## B . Cherry-pick Configurable Rules
4734
4835There is one set with pre-configured configurable rules. Include it and see what is errors are found:
4936
@@ -55,7 +42,7 @@ includes:
5542
5643<br>
5744
58- Would you like to ** tailor it to fit your taste** ? Pick one PHPStan rule and configure it manually ↓
45+ Would you like to tailor it to fit your taste? Pick one PHPStan rule and configure it manually ↓
5946
6047` ` ` yaml
6148services :
@@ -74,14 +61,16 @@ services:
7461
7562<br>
7663
77- <!-- ruledoc-start -->
7864# 30 Rules Overview
7965
8066## AnnotateRegexClassConstWithRegexLinkRule
8167
8268Add regex101.com link to that shows the regex in practise, so it will be easier to maintain in case of bug/extension in the future
8369
84- - class: [` Symplify\PHPStanRules\Rules\AnnotateRegexClassConstWithRegexLinkRule`](../src/Rules/AnnotateRegexClassConstWithRegexLinkRule.php)
70+ ` ` ` yaml
71+ rules :
72+ - Symplify\PHPStanRules\Rules\AnnotateRegexClassConstWithRegexLinkRule.php)
73+ ` ` `
8574
8675` ` ` php
8776class SomeClass
@@ -112,7 +101,10 @@ class SomeClass
112101
113102Interface must be located in "Contract" or "Contracts" namespace
114103
115- - class : [`Symplify\PHPStanRules\Rules\CheckRequiredInterfaceInContractNamespaceRule`](../src/Rules/CheckRequiredInterfaceInContractNamespaceRule.php)
104+ ``` yaml
105+ rules :
106+ - Symplify\PHPStanRules\Rules\CheckRequiredInterfaceInContractNamespaceRule
107+ ` ` `
116108
117109` ` ` php
118110namespace App\Repository;
@@ -144,8 +136,6 @@ Class should have suffix "%s" to respect parent type
144136
145137:wrench : ** configure it!**
146138
147- - class : [`Symplify\PHPStanRules\Rules\ClassNameRespectsParentSuffixRule`](../src/Rules/ClassNameRespectsParentSuffixRule.php)
148-
149139``` yaml
150140services :
151141 -
@@ -182,7 +172,10 @@ class SomeCommand extends Command
182172
183173Interface have suffix of "Interface", trait have "Trait" suffix exclusively
184174
185- - class : [`Symplify\PHPStanRules\Rules\Explicit\ExplicitClassPrefixSuffixRule`](../src/Rules/Explicit/ExplicitClassPrefixSuffixRule.php)
175+ ``` yaml
176+ rules :
177+ - Symplify\PHPStanRules\Rules\Explicit\ExplicitClassPrefixSuffixRule
178+ ` ` `
186179
187180` ` ` php
188181<?php
@@ -228,7 +221,10 @@ abstract class AbstractClass
228221
229222Array method calls [ $this, "method"] are not allowed. Use explicit method instead to help PhpStorm, PHPStan and Rector understand your code
230223
231- - class : [`Symplify\PHPStanRules\Rules\Complexity\ForbiddenArrayMethodCallRule`](../src/Rules/Complexity/ForbiddenArrayMethodCallRule.php)
224+ ``` yaml
225+ rules :
226+ - Symplify\PHPStanRules\Rules\Complexity\ForbiddenArrayMethodCallRule
227+ ` ` `
232228
233229` ` ` php
234230usort($items, [$this, "method"]);
@@ -252,7 +248,10 @@ usort($items, function (array $apples) {
252248
253249Only abstract classes can be extended
254250
255- - class : [`Symplify\PHPStanRules\Rules\ForbiddenExtendOfNonAbstractClassRule`](../src/Rules/ForbiddenExtendOfNonAbstractClassRule.php)
251+ ``` yaml
252+ rules :
253+ - Symplify\PHPStanRules\Rules\ForbiddenExtendOfNonAbstractClassRule
254+ ` ` `
256255
257256` ` ` php
258257final class SomeClass extends ParentClass
@@ -288,8 +287,6 @@ Function `"%s()"` cannot be used/left in the code
288287
289288:wrench : ** configure it!**
290289
291- - class : [`Symplify\PHPStanRules\Rules\ForbiddenFuncCallRule`](../src/Rules/ForbiddenFuncCallRule.php)
292-
293290``` yaml
294291services :
295292 -
@@ -351,7 +348,10 @@ echo $value;
351348
352349Multiple class/interface/trait is not allowed in single file
353350
354- - class : [`Symplify\PHPStanRules\Rules\ForbiddenMultipleClassLikeInOneFileRule`](../src/Rules/ForbiddenMultipleClassLikeInOneFileRule.php)
351+ ``` yaml
352+ rules :
353+ - Symplify\PHPStanRules\Rules\ForbiddenMultipleClassLikeInOneFileRule
354+ ` ` `
355355
356356` ` ` php
357357// src/SomeClass.php
@@ -390,8 +390,6 @@ interface SomeInterface
390390
391391:wrench : ** configure it!**
392392
393- - class : [`Symplify\PHPStanRules\Rules\ForbiddenNodeRule`](../src/Rules/ForbiddenNodeRule.php)
394-
395393``` yaml
396394services :
397395 -
@@ -424,7 +422,10 @@ return strlen('...');
424422
425423Avoid static access of constants, as they can change value. Use interface and contract method instead
426424
427- - class : [`Symplify\PHPStanRules\Rules\ForbiddenStaticClassConstFetchRule`](../src/Rules/ForbiddenStaticClassConstFetchRule.php)
425+ ``` yaml
426+ rules :
427+ - Symplify\PHPStanRules\Rules\ForbiddenStaticClassConstFetchRule
428+ ` ` `
428429
429430` ` ` php
430431class SomeClass
@@ -458,7 +459,10 @@ class SomeClass
458459
459460Use explicit names over dynamic ones
460461
461- - class : [`Symplify\PHPStanRules\Rules\NoDynamicNameRule`](../src/Rules/NoDynamicNameRule.php)
462+ ``` yaml
463+ rules :
464+ - Symplify\PHPStanRules\Rules\NoDynamicNameRule
465+ ` ` `
462466
463467` ` ` php
464468class SomeClass
@@ -492,7 +496,10 @@ class SomeClass
492496
493497Class with #[ Entity] attribute must be located in "Entity" namespace to be loaded by Doctrine
494498
495- - class : [`Symplify\PHPStanRules\Rules\NoEntityOutsideEntityNamespaceRule`](../src/Rules/NoEntityOutsideEntityNamespaceRule.php)
499+ ``` yaml
500+ rules :
501+ - Symplify\PHPStanRules\Rules\NoEntityOutsideEntityNamespaceRule
502+ ` ` `
496503
497504` ` ` php
498505namespace App\ValueObject;
@@ -528,7 +535,10 @@ class Product
528535
529536Global constants are forbidden. Use enum-like class list instead
530537
531- - class : [`Symplify\PHPStanRules\Rules\NoGlobalConstRule`](../src/Rules/NoGlobalConstRule.php)
538+ ``` yaml
539+ rules :
540+ - Symplify\PHPStanRules\Rules\NoGlobalConstRule
541+ ` ` `
532542
533543` ` ` php
534544const SOME_GLOBAL_CONST = 'value';
@@ -556,7 +566,10 @@ class SomeClass
556566
557567Use local named constant instead of inline string for regex to explain meaning by constant name
558568
559- - class : [`Symplify\PHPStanRules\Rules\NoInlineStringRegexRule`](../src/Rules/NoInlineStringRegexRule.php)
569+ ``` yaml
570+ rules :
571+ - Symplify\PHPStanRules\Rules\NoInlineStringRegexRule
572+ ` ` `
560573
561574` ` ` php
562575class SomeClass
@@ -595,7 +608,10 @@ class SomeClass
595608
596609Use explicit return value over magic &reference
597610
598- - class : [`Symplify\PHPStanRules\Rules\NoReferenceRule`](../src/Rules/NoReferenceRule.php)
611+ ``` yaml
612+ rules :
613+ - Symplify\PHPStanRules\Rules\NoReferenceRule
614+ ` ` `
599615
600616` ` ` php
601617class SomeClass
@@ -628,7 +644,10 @@ class SomeClass
628644
629645Use value object over return of values
630646
631- - class : [`Symplify\PHPStanRules\Rules\NoReturnArrayVariableListRule`](../src/Rules/NoReturnArrayVariableListRule.php)
647+ ``` yaml
648+ rules :
649+ - Symplify\PHPStanRules\Rules\NoReturnArrayVariableListRule
650+ ` ` `
632651
633652` ` ` php
634653class ReturnVariables
@@ -662,7 +681,10 @@ final class ReturnVariables
662681
663682Setter method cannot return anything, only set value
664683
665- - class : [`Symplify\PHPStanRules\Rules\NoReturnSetterMethodRule`](../src/Rules/NoReturnSetterMethodRule.php)
684+ ``` yaml
685+ rules :
686+ - Symplify\PHPStanRules\Rules\NoReturnSetterMethodRule
687+ ` ` `
666688
667689` ` ` php
668690final class SomeClass
@@ -700,7 +722,10 @@ final class SomeClass
700722
701723Interface "%s" has only single implementer. Consider using the class directly as there is no point in using the interface.
702724
703- - class : [`Symplify\PHPStanRules\Rules\NoSingleInterfaceImplementerRule`](../src/Rules/NoSingleInterfaceImplementerRule.php)
725+ ``` yaml
726+ rules :
727+ - Symplify\PHPStanRules\Rules\NoSingleInterfaceImplementerRule
728+ ` ` `
704729
705730` ` ` php
706731class SomeClass implements SomeInterface
@@ -738,7 +763,10 @@ interface SomeInterface
738763
739764Mocking "%s" class is forbidden. Use direct/anonymous class instead for better static analysis
740765
741- - class : [`Symplify\PHPStanRules\Rules\PHPUnit\NoTestMocksRule`](../src/Rules/PHPUnit/NoTestMocksRule.php)
766+ ``` yaml
767+ rules :
768+ - Symplify\PHPStanRules\Rules\PHPUnit\NoTestMocksRule
769+ ` ` `
742770
743771` ` ` php
744772use PHPUnit\Framework\TestCase;
@@ -778,8 +806,6 @@ Instead of "%s" class/interface use "%s"
778806
779807:wrench : ** configure it!**
780808
781- - class : [`Symplify\PHPStanRules\Rules\PreferredClassRule`](../src/Rules/PreferredClassRule.php)
782-
783809``` yaml
784810services :
785811 -
@@ -824,7 +850,10 @@ class SomeClass
824850
825851Change ` "%s()" ` method visibility to "%s" to respect parent method visibility.
826852
827- - class : [`Symplify\PHPStanRules\Rules\PreventParentMethodVisibilityOverrideRule`](../src/Rules/PreventParentMethodVisibilityOverrideRule.php)
853+ ``` yaml
854+ rules :
855+ - Symplify\PHPStanRules\Rules\PreventParentMethodVisibilityOverrideRule
856+ ` ` `
828857
829858` ` ` php
830859class SomeParentClass
@@ -870,7 +899,10 @@ class SomeClass extends SomeParentClass
870899
871900Name your constant with "_ REGEX" suffix, instead of "%s"
872901
873- - class : [`Symplify\PHPStanRules\Rules\RegexSuffixInRegexConstantRule`](../src/Rules/RegexSuffixInRegexConstantRule.php)
902+ ``` yaml
903+ rules :
904+ - Symplify\PHPStanRules\Rules\RegexSuffixInRegexConstantRule
905+ ` ` `
874906
875907` ` ` php
876908class SomeClass
@@ -908,7 +940,10 @@ class SomeClass
908940
909941Attribute must have all names explicitly defined
910942
911- - class : [`Symplify\PHPStanRules\Rules\RequireAttributeNameRule`](../src/Rules/RequireAttributeNameRule.php)
943+ ``` yaml
944+ rules :
945+ - Symplify\PHPStanRules\Rules\RequireAttributeNameRule
946+ ` ` `
912947
913948` ` ` php
914949use Symfony\Component\Routing\Annotation\Route;
@@ -946,7 +981,10 @@ class SomeController
946981
947982Attribute must be located in "Attribute" namespace
948983
949- - class : [`Symplify\PHPStanRules\Rules\Domain\RequireAttributeNamespaceRule`](../src/Rules/Domain/RequireAttributeNamespaceRule.php)
984+ ``` yaml
985+ rules :
986+ - Symplify\PHPStanRules\Rules\Domain\RequireAttributeNamespaceRule
987+ ` ` `
950988
951989` ` ` php
952990// app/Entity/SomeAttribute.php
@@ -980,7 +1018,10 @@ final class SomeAttribute
9801018
9811019` Exception ` must be located in "Exception" namespace
9821020
983- - class : [`Symplify\PHPStanRules\Rules\Domain\RequireExceptionNamespaceRule`](../src/Rules/Domain/RequireExceptionNamespaceRule.php)
1021+ ``` yaml
1022+ rules :
1023+ - Symplify\PHPStanRules\Rules\Domain\RequireExceptionNamespaceRule
1024+ ` ` `
9841025
9851026` ` ` php
9861027// app/Controller/SomeException.php
@@ -1013,7 +1054,10 @@ final class SomeException extends Exception
10131054
10141055Use invokable controller with ` __invoke() ` method instead of named action method
10151056
1016- - class : [`Symplify\PHPStanRules\Symfony\Rules\RequireInvokableControllerRule`](../src/Symfony/Rules/RequireInvokableControllerRule.php)
1057+ ``` yaml
1058+ rules :
1059+ - Symplify\PHPStanRules\Symfony\Rules\RequireInvokableControllerRule
1060+ ` ` `
10171061
10181062` ` ` php
10191063use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -1053,7 +1097,10 @@ final class SomeController extends AbstractController
10531097
10541098Enum constants "%s" are duplicated. Make them unique instead
10551099
1056- - class : [`Symplify\PHPStanRules\Rules\Enum\RequireUniqueEnumConstantRule`](../src/Rules/Enum/RequireUniqueEnumConstantRule.php)
1100+ ``` yaml
1101+ rules :
1102+ - Symplify\PHPStanRules\Rules\Enum\RequireUniqueEnumConstantRule
1103+ ` ` `
10571104
10581105` ` ` php
10591106use MyCLabs\Enum\Enum;
@@ -1091,8 +1138,6 @@ Class "%s" is missing `@see` annotation with test case class reference
10911138
10921139:wrench : ** configure it!**
10931140
1094- - class : [`Symplify\PHPStanRules\Rules\SeeAnnotationToTestRule`](../src/Rules/SeeAnnotationToTestRule.php)
1095-
10961141``` yaml
10971142services :
10981143 -
@@ -1132,7 +1177,10 @@ class SomeClass extends Rule
11321177
11331178Constant "%s" must be uppercase
11341179
1135- - class : [`Symplify\PHPStanRules\Rules\UppercaseConstantRule`](../src/Rules/UppercaseConstantRule.php)
1180+ ``` yaml
1181+ rules :
1182+ - Symplify\PHPStanRules\Rules\UppercaseConstantRule
1183+ ` ` `
11361184
11371185` ` ` php
11381186final class SomeClass
0 commit comments