66use PHP_CodeSniffer \Files \File ;
77use PHP_CodeSniffer \Files \LocalFile ;
88use PHP_CodeSniffer \Runner ;
9+ use PHP_CodeSniffer \Sniffs \Sniff ;
910use ReflectionClass ;
1011use function array_map ;
1112use function array_merge ;
@@ -44,20 +45,22 @@ protected static function checkFile(string $filePath, array $sniffProperties = [
4445 $ codeSniffer ->init ();
4546
4647 if (count ($ sniffProperties ) > 0 ) {
47- $ codeSniffer ->ruleset ->ruleset [static ::getSniffName ()]['properties ' ] = $ sniffProperties ;
48+ $ codeSniffer ->ruleset ->ruleset [self ::getSniffName ()]['properties ' ] = $ sniffProperties ;
4849 }
4950
50- $ sniffClassName = static ::getSniffClassName ();
51+ $ sniffClassName = self ::getSniffClassName ();
52+ /** @var Sniff $sniff */
53+ $ sniff = new $ sniffClassName ();
5154
52- $ codeSniffer ->ruleset ->sniffs = [$ sniffClassName => new $ sniffClassName () ];
55+ $ codeSniffer ->ruleset ->sniffs = [$ sniffClassName => $ sniff ];
5356
5457 if (count ($ codesToCheck ) > 0 ) {
55- foreach (static ::getSniffClassReflection ()->getConstants () as $ constantName => $ constantValue ) {
58+ foreach (self ::getSniffClassReflection ()->getConstants () as $ constantName => $ constantValue ) {
5659 if (strpos ($ constantName , 'CODE_ ' ) !== 0 || in_array ($ constantValue , $ codesToCheck , true )) {
5760 continue ;
5861 }
5962
60- $ codeSniffer ->ruleset ->ruleset [sprintf ('%s.%s ' , static ::getSniffName (), $ constantValue )]['severity ' ] = 0 ;
63+ $ codeSniffer ->ruleset ->ruleset [sprintf ('%s.%s ' , self ::getSniffName (), $ constantValue )]['severity ' ] = 0 ;
6164 }
6265 }
6366
@@ -80,7 +83,7 @@ protected static function assertSniffError(File $phpcsFile, int $line, string $c
8083 $ errors = $ phpcsFile ->getErrors ();
8184 self ::assertTrue (isset ($ errors [$ line ]), sprintf ('Expected error on line %s, but none found. ' , $ line ));
8285
83- $ sniffCode = sprintf ('%s.%s ' , static ::getSniffName (), $ code );
86+ $ sniffCode = sprintf ('%s.%s ' , self ::getSniffName (), $ code );
8487
8588 self ::assertTrue (
8689 self ::hasError ($ errors [$ line ], $ sniffCode , $ message ),
@@ -135,21 +138,26 @@ private static function getSniffName(): string
135138 '' ,
136139 '' ,
137140 ],
138- static ::getSniffClassName ()
141+ self ::getSniffClassName ()
139142 );
140143 }
141144
145+ /**
146+ * @return class-string
147+ */
142148 private static function getSniffClassName (): string
143149 {
144- return substr (static ::class, 0 , -strlen ('Test ' ));
150+ /** @var class-string $sniffClassName */
151+ $ sniffClassName = substr (static ::class, 0 , -strlen ('Test ' ));
152+
153+ return $ sniffClassName ;
145154 }
146155
147156 private static function getSniffClassReflection (): ReflectionClass
148157 {
149158 static $ reflections = [];
150159
151- /** @phpstan-var class-string $className */
152- $ className = static ::getSniffClassName ();
160+ $ className = self ::getSniffClassName ();
153161
154162 return $ reflections [$ className ] ?? $ reflections [$ className ] = new ReflectionClass ($ className );
155163 }
0 commit comments