Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
SetMethodsSniff | |
0.00% |
0 / 23 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
process | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Sniffs\PHPUnit; |
4 | |
5 | use PHP_CodeSniffer\Files\File; |
6 | use PHP_CodeSniffer\Sniffs\Sniff; |
7 | |
8 | /** |
9 | * Replace calls to MockBuilder::setMethods (deprecated in PHPUnit 8) with MockBuilder::onlyMethods. This is based |
10 | * on the assumption that most such calls do not intend to add new methods (which would require using addMethods). |
11 | * As such, this sniff only serves as a first automatic step, but manual review of replacements is necessary |
12 | * (which shouldn't be hard anyway, as PHPUnit will fail hard if the wrong method is used). |
13 | * This is also not going to trigger for setMethods() calls that don't belong to MockBuilder. |
14 | */ |
15 | class SetMethodsSniff implements Sniff { |
16 | use PHPUnitTestTrait; |
17 | |
18 | /** |
19 | * @inheritDoc |
20 | */ |
21 | public function register(): array { |
22 | return [ T_STRING ]; |
23 | } |
24 | |
25 | /** |
26 | * @param File $phpcsFile |
27 | * @param int $stackPtr |
28 | * |
29 | * @return void|int |
30 | */ |
31 | public function process( File $phpcsFile, $stackPtr ) { |
32 | if ( !$this->isTestFile( $phpcsFile, $stackPtr ) ) { |
33 | return $phpcsFile->numTokens; |
34 | } |
35 | |
36 | $tokens = $phpcsFile->getTokens(); |
37 | $tokContent = $tokens[$stackPtr]['content']; |
38 | |
39 | // We don't care about stuff that's not in a method in a class |
40 | if ( $tokens[$stackPtr]['level'] < 2 || $tokContent !== 'setMethods' ) { |
41 | return; |
42 | } |
43 | |
44 | $parOpener = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); |
45 | if ( $tokens[$parOpener]['code'] !== T_OPEN_PARENTHESIS ) { |
46 | return; |
47 | } |
48 | |
49 | $fix = $phpcsFile->addFixableWarning( |
50 | 'setMethods is deprecated in PHPUnit 8 and should be replaced with onlyMethods ' . |
51 | 'or addMethods', |
52 | $stackPtr, |
53 | 'SetMethods' |
54 | ); |
55 | if ( !$fix ) { |
56 | return; |
57 | } |
58 | |
59 | $phpcsFile->fixer->replaceToken( $stackPtr, 'onlyMethods' ); |
60 | // Special case: onlyMethods() takes an empty array, not null. |
61 | $firstArgToken = $phpcsFile->findNext( T_WHITESPACE, $parOpener + 1, null, true ); |
62 | if ( $tokens[$firstArgToken]['code'] === T_NULL ) { |
63 | $phpcsFile->fixer->replaceToken( $firstArgToken, '[]' ); |
64 | } |
65 | return $stackPtr + 1; |
66 | } |
67 | |
68 | } |