Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MagicConstantClosureSniff | |
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
20 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
processTokenWithinScope | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
processTokenOutsideScope | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createSniffCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Report error when __METHOD__ is used in closures, |
5 | * because it does not reporting the correct method used in logs |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | * http://www.gnu.org/copyleft/gpl.html |
21 | * |
22 | * @file |
23 | */ |
24 | |
25 | namespace MediaWiki\Sniffs\Usage; |
26 | |
27 | use PHP_CodeSniffer\Files\File; |
28 | use PHP_CodeSniffer\Sniffs\AbstractScopeSniff; |
29 | |
30 | class MagicConstantClosureSniff extends AbstractScopeSniff { |
31 | |
32 | /** |
33 | * @inheritDoc |
34 | */ |
35 | public function __construct() { |
36 | parent::__construct( |
37 | [ T_CLOSURE ], |
38 | // https://php.net/manual/en/language.constants.predefined.php |
39 | // T_CLASS_C, T_TRAIT_C and T_NS_C works in closures |
40 | // ::class also works in closures |
41 | [ T_METHOD_C, T_FUNC_C ] |
42 | ); |
43 | } |
44 | |
45 | /** |
46 | * @param File $phpcsFile |
47 | * @param int $stackPtr The current token index. |
48 | * @param int $currScope The position of the current scope. |
49 | * @return void |
50 | * @suppress PhanUnusedProtectedMethodParameter Inherit from parent class |
51 | */ |
52 | protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currScope ) { |
53 | $tokens = $phpcsFile->getTokens(); |
54 | $constant = $tokens[$stackPtr]['content']; |
55 | $phpcsFile->addWarning( |
56 | 'Avoid use of %s magic constant in closure', |
57 | $stackPtr, |
58 | $this->createSniffCode( 'FoundConstant', $constant ), |
59 | [ $constant ] |
60 | ); |
61 | } |
62 | |
63 | /** |
64 | * @param File $phpcsFile |
65 | * @param int $stackPtr The current token index. |
66 | * @return void |
67 | * @suppress PhanUnusedProtectedMethodParameter Inherit from parent class |
68 | */ |
69 | protected function processTokenOutsideScope( File $phpcsFile, $stackPtr ) { |
70 | } |
71 | |
72 | /** |
73 | * @param string $prefix |
74 | * @param string $constant |
75 | * |
76 | * @return string |
77 | */ |
78 | private function createSniffCode( string $prefix, string $constant ): string { |
79 | return $prefix . ucfirst( strtolower( trim( $constant, '_' ) ) ); |
80 | } |
81 | } |