Skip to content

Commit 9782790

Browse files
committed
add sniff to avoid phpstan ignores
1 parent 2c7b5da commit 9782790

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Sniff: AvoidIgnoringPHPStanSniff.
4+
*
5+
* @package travelopia-coding-standards
6+
*/
7+
8+
namespace Travelopia\Sniffs\PHP;
9+
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use PHP_CodeSniffer\Files\File;
12+
13+
/**
14+
* Sniff to check if PHPStan errors are ignored.
15+
*/
16+
class AvoidIgnoringPHPStanSniff implements Sniff {
17+
18+
/**
19+
* Register the sniff.
20+
*
21+
* @return mixed[]
22+
*/
23+
public function register(): array {
24+
return [ T_COMMENT ];
25+
}
26+
27+
/**
28+
* Process the sniff.
29+
*
30+
* @param File $phpcsFile The file being processed.
31+
* @param int $stackPtr Stack pointer.
32+
*
33+
* @return void
34+
*/
35+
public function process( File $phpcsFile, $stackPtr ): void {
36+
// Get tokens.
37+
$tokens = $phpcsFile->getTokens();
38+
39+
// Check for PHPStan ignores and add a warning.
40+
if ( str_contains( $tokens[ $stackPtr ]['content'], '@phpstan-ignore' ) || str_contains( $tokens[ $stackPtr ]['content'], '@phpstan-ignore-line' ) ) {
41+
$phpcsFile->addWarningOnLine(
42+
"Don't ignore PHPStan errors. Try to address the issue by adding additional checks.",
43+
$tokens[ $stackPtr ]['line'],
44+
'Avoid'
45+
);
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)