|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * Class for a sniff to oblige private property name prefixed with underscore |
| 5 | + * |
| 6 | + * This Sniff check if a underscore is missing before private property name. |
| 7 | + * The private property name is identified by the PHP token T_VARIABLE |
| 8 | + * The private visibility is identified by the PHP token T_PRIVATE |
| 9 | + */ |
3 | 10 | class Yii2_Sniffs_Properties_PrivatePropertiesUnderscoreSniff implements PHP_CodeSniffer_Sniff
|
4 | 11 | {
|
5 |
| - public function register() |
6 |
| - { |
7 |
| - return array( |
8 |
| - T_PRIVATE, |
9 |
| - ); |
10 |
| - } |
| 12 | + public function register() |
| 13 | + { |
| 14 | + return [T_PRIVATE]; |
| 15 | + } |
11 | 16 |
|
12 |
| - public function process(PHP_CodeSniffer_File $file, $pointer) |
13 |
| - { |
14 |
| - $tokens = $file->getTokens(); |
15 |
| - if ($tokens[$pointer]['content'] === 'private' && |
16 |
| - $tokens[$pointer + 1]['type'] === 'T_WHITESPACE' && |
17 |
| - $tokens[$pointer + 2]['type'] === 'T_VARIABLE' && |
18 |
| - strpos($tokens[$pointer + 2]['content'], '$_') !== 0) { |
19 |
| - $file->addError('Private property name must be prefixed with underscore.', $pointer); |
20 |
| - } |
21 |
| - } |
| 17 | + public function process(PHP_CodeSniffer_File $file, $pointer) |
| 18 | + { |
| 19 | + $tokens = $file->getTokens(); |
| 20 | + if ($tokens[$pointer]['content'] === 'private' && |
| 21 | + $tokens[$pointer + 1]['type'] === 'T_WHITESPACE' && |
| 22 | + $tokens[$pointer + 2]['type'] === 'T_VARIABLE' && |
| 23 | + strpos($tokens[$pointer + 2]['content'], '$_') !== 0) { |
| 24 | + |
| 25 | + $data = [$tokens[$pointer + 2]['content']]; |
| 26 | + $file->addError('Private property name "%s" must be prefixed with underscore.', $pointer, 'NoUnderscore', $data); |
| 27 | + } |
| 28 | + } |
22 | 29 | }
|
0 commit comments