Skip to content

Commit c3789d8

Browse files
edgardmessiassamdark
authored andcommitted
Several improvements to PrivatePropertiesUnderscoreSniff (#28)
* Fixed coding standards * Added name of property for private properties rule * Added PHPDoc block
1 parent c7d996e commit c3789d8

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
<?php
22

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+
*/
310
class Yii2_Sniffs_Properties_PrivatePropertiesUnderscoreSniff implements PHP_CodeSniffer_Sniff
411
{
5-
public function register()
6-
{
7-
return array(
8-
T_PRIVATE,
9-
);
10-
}
12+
public function register()
13+
{
14+
return [T_PRIVATE];
15+
}
1116

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+
}
2229
}

0 commit comments

Comments
 (0)