Skip to content

Commit 8cbe101

Browse files
committed
Apply CS so something can be coded, actually. Also, use strpos() and assert to rule out impossible types. Drop empty().
1 parent eb8db2e commit 8cbe101

File tree

1 file changed

+83
-67
lines changed

1 file changed

+83
-67
lines changed

src/Cdn77/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,90 @@
55
namespace Cdn77\Sniffs\NamingConventions;
66

77
use PHP_CodeSniffer\Files\File;
8-
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use const T_COMMA;
10-
use const T_OPEN_PARENTHESIS;
11-
use const T_OPEN_SHORT_ARRAY;
12-
use const T_WHITESPACE;
13-
148
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
159
use PHP_CodeSniffer\Util\Common;
1610
use PHP_CodeSniffer\Util\Tokens;
1711

18-
class ValidVariableNameSniff extends AbstractVariableSniff
19-
{
12+
use function assert;
13+
use function ltrim;
14+
use function preg_match_all;
15+
use function strpos;
16+
use function substr;
17+
use function ucfirst;
2018

19+
use const T_DOUBLE_COLON;
20+
use const T_NULLSAFE_OBJECT_OPERATOR;
21+
use const T_OBJECT_OPERATOR;
22+
use const T_OPEN_PARENTHESIS;
23+
use const T_STRING;
24+
use const T_WHITESPACE;
2125

26+
class ValidVariableNameSniff extends AbstractVariableSniff
27+
{
2228
/**
2329
* Processes this test, when one of its tokens is encountered.
2430
*
25-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
31+
* @param File $phpcsFile The file being scanned.
2632
* @param int $stackPtr The position of the current token in the
2733
* stack passed in $tokens.
2834
*
29-
* @return void
35+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
3036
*/
31-
protected function processVariable(File $phpcsFile, $stackPtr)
37+
protected function processVariable(File $phpcsFile, $stackPtr): void
3238
{
33-
$tokens = $phpcsFile->getTokens();
39+
$tokens = $phpcsFile->getTokens();
3440
$varName = ltrim($tokens[$stackPtr]['content'], '$');
3541

3642
// If it's a php reserved var, then its ok.
3743
if (isset($this->phpReservedVars[$varName]) === true) {
3844
return;
3945
}
4046

41-
$objOperator = $phpcsFile->findNext([T_WHITESPACE], ($stackPtr + 1), null, true);
42-
if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR
47+
$objOperator = $phpcsFile->findNext([T_WHITESPACE], $stackPtr + 1, null, true);
48+
assert($objOperator !== false);
49+
50+
if (
51+
$tokens[$objOperator]['code'] === T_OBJECT_OPERATOR
4352
|| $tokens[$objOperator]['code'] === T_NULLSAFE_OBJECT_OPERATOR
4453
) {
4554
// Check to see if we are using a variable from an object.
46-
$var = $phpcsFile->findNext([T_WHITESPACE], ($objOperator + 1), null, true);
55+
$var = $phpcsFile->findNext([T_WHITESPACE], $objOperator + 1, null, true);
56+
assert($var !== false);
57+
4758
if ($tokens[$var]['code'] === T_STRING) {
48-
$bracket = $phpcsFile->findNext([T_WHITESPACE], ($var + 1), null, true);
59+
$bracket = $phpcsFile->findNext([T_WHITESPACE], $var + 1, null, true);
4960
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
5061
$objVarName = $tokens[$var]['content'];
5162

5263
// There is no way for us to know if the var is public or
5364
// private, so we have to ignore a leading underscore if there is
5465
// one and just check the main part of the variable name.
5566
$originalVarName = $objVarName;
56-
if (substr($objVarName, 0, 1) === '_') {
67+
if (strpos($objVarName, '_') === 0) {
5768
$objVarName = substr($objVarName, 1);
5869
}
5970

6071
if (Common::isCamelCaps($objVarName, false, true, false) === false) {
6172
$error = 'Member variable "%s" is not in valid camel caps format';
62-
$data = [$originalVarName];
73+
$data = [$originalVarName];
6374
$phpcsFile->addError($error, $var, 'MemberNotCamelCaps', $data);
6475
}
65-
}//end if
66-
}//end if
67-
}//end if
76+
}
77+
}
78+
}
6879

69-
$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
80+
$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
7081
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
7182
// The variable lives within a class, and is referenced like
7283
// this: MyClass::$_variable, so we don't know its scope.
7384
$objVarName = $varName;
74-
if (substr($objVarName, 0, 1) === '_') {
85+
if (strpos($objVarName, '_') === 0) {
7586
$objVarName = substr($objVarName, 1);
7687
}
7788

7889
if (Common::isCamelCaps($objVarName, false, true, false) === false) {
7990
$error = 'Member variable "%s" is not in valid camel caps format';
80-
$data = [$tokens[$stackPtr]['content']];
91+
$data = [$tokens[$stackPtr]['content']];
8192
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $data);
8293
}
8394

@@ -88,104 +99,109 @@ protected function processVariable(File $phpcsFile, $stackPtr)
8899
// so we have to ignore a leading underscore if there is one and just
89100
// check the main part of the variable name.
90101
$originalVarName = $varName;
91-
if (substr($varName, 0, 1) === '_') {
102+
if (strpos($varName, '_') === 0) {
92103
$inClass = $phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens);
93104
if ($inClass === true) {
94105
$varName = substr($varName, 1);
95106
}
96107
}
97108

98-
if (Common::isCamelCaps($varName, false, true, false) === false) {
99-
$error = 'Variable "%s" is not in valid camel caps format';
100-
$data = [$originalVarName];
101-
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
109+
if (Common::isCamelCaps($varName, false, true, false) !== false) {
110+
return;
102111
}
103112

104-
}//end processVariable()
105-
113+
$error = 'Variable "%s" is not in valid camel caps format';
114+
$data = [$originalVarName];
115+
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
116+
}
106117

107118
/**
108119
* Processes class member variables.
109120
*
110-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
121+
* @param File $phpcsFile The file being scanned.
111122
* @param int $stackPtr The position of the current token in the
112123
* stack passed in $tokens.
113124
*
114-
* @return void
125+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
115126
*/
116-
protected function processMemberVar(File $phpcsFile, $stackPtr)
127+
protected function processMemberVar(File $phpcsFile, $stackPtr): void
117128
{
118129
$tokens = $phpcsFile->getTokens();
119130

120-
$varName = ltrim($tokens[$stackPtr]['content'], '$');
131+
$varName = ltrim($tokens[$stackPtr]['content'], '$');
121132
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
122-
if (empty($memberProps) === true) {
133+
if ($memberProps === []) {
123134
// Couldn't get any info about this variable, which
124135
// generally means it is invalid or possibly has a parse
125136
// error. Any errors will be reported by the core, so
126137
// we can ignore it.
127138
return;
128139
}
129140

130-
$public = ($memberProps['scope'] !== 'private');
141+
$public = ($memberProps['scope'] !== 'private');
131142
$errorData = [$varName];
132143

133144
if ($public === true) {
134-
if (substr($varName, 0, 1) === '_') {
145+
if (strpos($varName, '_') === 0) {
135146
$error = '%s member variable "%s" must not contain a leading underscore';
136-
$data = [
147+
$data = [
137148
ucfirst($memberProps['scope']),
138149
$errorData[0],
139150
];
140151
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
141152
}
142-
} else {
143-
if (substr($varName, 0, 1) !== '_') {
144-
$error = 'Private member variable "%s" must contain a leading underscore';
145-
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
146-
}
153+
} elseif (strpos($varName, '_') !== 0) {
154+
$error = 'Private member variable "%s" must contain a leading underscore';
155+
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
147156
}
148157

149158
// Remove a potential underscore prefix for testing CamelCaps.
150159
$varName = ltrim($varName, '_');
151160

152-
if (Common::isCamelCaps($varName, false, true, false) === false) {
153-
$error = 'Member variable "%s" is not in valid camel caps format';
154-
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData);
161+
if (Common::isCamelCaps($varName, false, true, false) !== false) {
162+
return;
155163
}
156164

157-
}//end processMemberVar()
158-
165+
$error = 'Member variable "%s" is not in valid camel caps format';
166+
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData);
167+
}
159168

160169
/**
161170
* Processes the variable found within a double quoted string.
162171
*
163-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
172+
* @param File $phpcsFile The file being scanned.
164173
* @param int $stackPtr The position of the double quoted
165174
* string.
166175
*
167-
* @return void
176+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
168177
*/
169-
protected function processVariableInString(File $phpcsFile, $stackPtr)
178+
protected function processVariableInString(File $phpcsFile, $stackPtr): void
170179
{
171180
$tokens = $phpcsFile->getTokens();
172181

173-
if (preg_match_all('|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
174-
foreach ($matches[1] as $varName) {
175-
// If it's a php reserved var, then its ok.
176-
if (isset($this->phpReservedVars[$varName]) === true) {
177-
continue;
178-
}
179-
180-
if (Common::isCamelCaps($varName, false, true, false) === false) {
181-
$error = 'Variable "%s" is not in valid camel caps format';
182-
$data = [$varName];
183-
$phpcsFile->addError($error, $stackPtr, 'StringNotCamelCaps', $data);
184-
}
185-
}
182+
if (
183+
preg_match_all(
184+
'|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|',
185+
$tokens[$stackPtr]['content'],
186+
$matches
187+
) === 0
188+
) {
189+
return;
186190
}
187191

188-
}//end processVariableInString()
192+
foreach ($matches[1] as $varName) {
193+
// If it's a php reserved var, then its ok.
194+
if (isset($this->phpReservedVars[$varName]) === true) {
195+
continue;
196+
}
189197

198+
if (Common::isCamelCaps($varName, false, true, false) !== false) {
199+
continue;
200+
}
190201

191-
}//end class
202+
$error = 'Variable "%s" is not in valid camel caps format';
203+
$data = [$varName];
204+
$phpcsFile->addError($error, $stackPtr, 'StringNotCamelCaps', $data);
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)