Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ClassMatchesFilenameSniff | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
182 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
process | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
isMaintenanceScript | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Sniffs\Files; |
22 | |
23 | use PHP_CodeSniffer\Files\File; |
24 | use PHP_CodeSniffer\Sniffs\Sniff; |
25 | |
26 | class ClassMatchesFilenameSniff implements Sniff { |
27 | |
28 | /** |
29 | * @inheritDoc |
30 | */ |
31 | public function register(): array { |
32 | return [ T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM ]; |
33 | } |
34 | |
35 | /** |
36 | * Check the class name against the filename |
37 | * This check is only done once, the rest of the file is always ignored. |
38 | * |
39 | * @param File $phpcsFile |
40 | * @param int $stackPtr |
41 | * @return int |
42 | */ |
43 | public function process( File $phpcsFile, $stackPtr ) { |
44 | $fname = $phpcsFile->getFilename(); |
45 | if ( $fname === 'STDIN' ) { |
46 | return $phpcsFile->numTokens; |
47 | } |
48 | |
49 | $base = basename( $fname ); |
50 | $name = $phpcsFile->getDeclarationName( $stackPtr ); |
51 | if ( $base !== "$name.php" ) { |
52 | $wrongCase = strcasecmp( $base, "$name.php" ) === 0; |
53 | if ( $wrongCase && $this->isMaintenanceScript( $phpcsFile ) ) { |
54 | // Maintenance scripts follow the class name, but the first |
55 | // letter is lowercase. |
56 | $expected = lcfirst( $name ); |
57 | if ( $base === "$expected.php" ) { |
58 | // OK! |
59 | return $phpcsFile->numTokens; |
60 | } |
61 | } |
62 | $phpcsFile->addError( |
63 | 'Class name \'%s\' does not match filename \'%s\'', |
64 | $stackPtr, |
65 | $wrongCase ? 'WrongCase' : 'NotMatch', |
66 | [ $name, $base ] |
67 | ); |
68 | } |
69 | |
70 | return $phpcsFile->numTokens; |
71 | } |
72 | |
73 | /** |
74 | * Figure out whether the file is a MediaWiki maintenance script |
75 | * |
76 | * @param File $phpcsFile |
77 | * |
78 | * @return bool |
79 | */ |
80 | private function isMaintenanceScript( File $phpcsFile ): bool { |
81 | $tokens = $phpcsFile->getTokens(); |
82 | |
83 | // Per convention the line we are looking for is the last in all maintenance scripts |
84 | for ( $i = $phpcsFile->numTokens; $i--; ) { |
85 | if ( $tokens[$i]['level'] !== 0 ) { |
86 | // Only look into the global scope |
87 | return false; |
88 | } |
89 | if ( $tokens[$i]['code'] === T_STRING |
90 | && $tokens[$i]['content'] === 'RUN_MAINTENANCE_IF_MAIN' |
91 | ) { |
92 | return true; |
93 | } |
94 | } |
95 | |
96 | return false; |
97 | } |
98 | |
99 | } |