Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
AssertEmptySniff | |
0.00% |
0 / 17 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
process | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Sniffs\PHPUnit; |
4 | |
5 | use PHP_CodeSniffer\Files\File; |
6 | use PHP_CodeSniffer\Sniffs\Sniff; |
7 | |
8 | /** |
9 | * Warn when using PHPUnit's looser assertEmpty() |
10 | * |
11 | * @author DannyS712 |
12 | * @license GPL-2.0-or-later |
13 | */ |
14 | class AssertEmptySniff implements Sniff { |
15 | use PHPUnitTestTrait; |
16 | |
17 | /** |
18 | * @inheritDoc |
19 | */ |
20 | public function register() { |
21 | return [ T_STRING ]; |
22 | } |
23 | |
24 | /** |
25 | * @param File $phpcsFile |
26 | * @param int $stackPtr |
27 | * |
28 | * @return void|int |
29 | */ |
30 | public function process( File $phpcsFile, $stackPtr ) { |
31 | if ( !$this->isTestFile( $phpcsFile, $stackPtr ) ) { |
32 | return $phpcsFile->numTokens; |
33 | } |
34 | |
35 | $tokens = $phpcsFile->getTokens(); |
36 | |
37 | // Only check code in a method in a class |
38 | if ( $tokens[$stackPtr]['level'] < 2 ) { |
39 | return; |
40 | } |
41 | |
42 | // Ensure its the right method name |
43 | if ( $tokens[$stackPtr]['content'] !== 'assertEmpty' ) { |
44 | return; |
45 | } |
46 | |
47 | $openParen = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); |
48 | |
49 | // If the next non-whitespace token isn't parenthesis, its not a call to assertEmpty |
50 | if ( !isset( $tokens[$openParen]['parenthesis_closer'] ) ) { |
51 | return $openParen; |
52 | } |
53 | |
54 | $phpcsFile->addWarning( |
55 | 'assertEmpty performs loose comparisons and should not be used.', |
56 | $stackPtr, |
57 | 'AssertEmptyUsed' |
58 | ); |
59 | |
60 | // Minimum number of tokens before the next possible assertEmpty |
61 | return $tokens[$openParen]['parenthesis_closer'] + 4; |
62 | } |
63 | |
64 | } |