5
5
namespace Cdn77 \Sniffs \NamingConventions ;
6
6
7
7
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
-
14
8
use PHP_CodeSniffer \Sniffs \AbstractVariableSniff ;
15
9
use PHP_CodeSniffer \Util \Common ;
16
10
use PHP_CodeSniffer \Util \Tokens ;
17
11
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 ;
20
18
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 ;
21
25
26
+ class ValidVariableNameSniff extends AbstractVariableSniff
27
+ {
22
28
/**
23
29
* Processes this test, when one of its tokens is encountered.
24
30
*
25
- * @param \PHP_CodeSniffer\Files\ File $phpcsFile The file being scanned.
31
+ * @param File $phpcsFile The file being scanned.
26
32
* @param int $stackPtr The position of the current token in the
27
33
* stack passed in $tokens.
28
34
*
29
- * @return void
35
+ * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
30
36
*/
31
- protected function processVariable (File $ phpcsFile , $ stackPtr )
37
+ protected function processVariable (File $ phpcsFile , $ stackPtr ): void
32
38
{
33
- $ tokens = $ phpcsFile ->getTokens ();
39
+ $ tokens = $ phpcsFile ->getTokens ();
34
40
$ varName = ltrim ($ tokens [$ stackPtr ]['content ' ], '$ ' );
35
41
36
42
// If it's a php reserved var, then its ok.
37
43
if (isset ($ this ->phpReservedVars [$ varName ]) === true ) {
38
44
return ;
39
45
}
40
46
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
43
52
|| $ tokens [$ objOperator ]['code ' ] === T_NULLSAFE_OBJECT_OPERATOR
44
53
) {
45
54
// 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
+
47
58
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 );
49
60
if ($ tokens [$ bracket ]['code ' ] !== T_OPEN_PARENTHESIS ) {
50
61
$ objVarName = $ tokens [$ var ]['content ' ];
51
62
52
63
// There is no way for us to know if the var is public or
53
64
// private, so we have to ignore a leading underscore if there is
54
65
// one and just check the main part of the variable name.
55
66
$ originalVarName = $ objVarName ;
56
- if (substr ($ objVarName , 0 , 1 ) === ' _ ' ) {
67
+ if (strpos ($ objVarName , ' _ ' ) === 0 ) {
57
68
$ objVarName = substr ($ objVarName , 1 );
58
69
}
59
70
60
71
if (Common::isCamelCaps ($ objVarName , false , true , false ) === false ) {
61
72
$ error = 'Member variable "%s" is not in valid camel caps format ' ;
62
- $ data = [$ originalVarName ];
73
+ $ data = [$ originalVarName ];
63
74
$ phpcsFile ->addError ($ error , $ var , 'MemberNotCamelCaps ' , $ data );
64
75
}
65
- }//end if
66
- }//end if
67
- }//end if
76
+ }
77
+ }
78
+ }
68
79
69
- $ objOperator = $ phpcsFile ->findPrevious (T_WHITESPACE , ( $ stackPtr - 1 ) , null , true );
80
+ $ objOperator = $ phpcsFile ->findPrevious (T_WHITESPACE , $ stackPtr - 1 , null , true );
70
81
if ($ tokens [$ objOperator ]['code ' ] === T_DOUBLE_COLON ) {
71
82
// The variable lives within a class, and is referenced like
72
83
// this: MyClass::$_variable, so we don't know its scope.
73
84
$ objVarName = $ varName ;
74
- if (substr ($ objVarName , 0 , 1 ) === ' _ ' ) {
85
+ if (strpos ($ objVarName , ' _ ' ) === 0 ) {
75
86
$ objVarName = substr ($ objVarName , 1 );
76
87
}
77
88
78
89
if (Common::isCamelCaps ($ objVarName , false , true , false ) === false ) {
79
90
$ error = 'Member variable "%s" is not in valid camel caps format ' ;
80
- $ data = [$ tokens [$ stackPtr ]['content ' ]];
91
+ $ data = [$ tokens [$ stackPtr ]['content ' ]];
81
92
$ phpcsFile ->addError ($ error , $ stackPtr , 'MemberNotCamelCaps ' , $ data );
82
93
}
83
94
@@ -88,104 +99,109 @@ protected function processVariable(File $phpcsFile, $stackPtr)
88
99
// so we have to ignore a leading underscore if there is one and just
89
100
// check the main part of the variable name.
90
101
$ originalVarName = $ varName ;
91
- if (substr ($ varName , 0 , 1 ) === ' _ ' ) {
102
+ if (strpos ($ varName , ' _ ' ) === 0 ) {
92
103
$ inClass = $ phpcsFile ->hasCondition ($ stackPtr , Tokens::$ ooScopeTokens );
93
104
if ($ inClass === true ) {
94
105
$ varName = substr ($ varName , 1 );
95
106
}
96
107
}
97
108
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 ;
102
111
}
103
112
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
+ }
106
117
107
118
/**
108
119
* Processes class member variables.
109
120
*
110
- * @param \PHP_CodeSniffer\Files\ File $phpcsFile The file being scanned.
121
+ * @param File $phpcsFile The file being scanned.
111
122
* @param int $stackPtr The position of the current token in the
112
123
* stack passed in $tokens.
113
124
*
114
- * @return void
125
+ * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
115
126
*/
116
- protected function processMemberVar (File $ phpcsFile , $ stackPtr )
127
+ protected function processMemberVar (File $ phpcsFile , $ stackPtr ): void
117
128
{
118
129
$ tokens = $ phpcsFile ->getTokens ();
119
130
120
- $ varName = ltrim ($ tokens [$ stackPtr ]['content ' ], '$ ' );
131
+ $ varName = ltrim ($ tokens [$ stackPtr ]['content ' ], '$ ' );
121
132
$ memberProps = $ phpcsFile ->getMemberProperties ($ stackPtr );
122
- if (empty ( $ memberProps) === true ) {
133
+ if ($ memberProps === [] ) {
123
134
// Couldn't get any info about this variable, which
124
135
// generally means it is invalid or possibly has a parse
125
136
// error. Any errors will be reported by the core, so
126
137
// we can ignore it.
127
138
return ;
128
139
}
129
140
130
- $ public = ($ memberProps ['scope ' ] !== 'private ' );
141
+ $ public = ($ memberProps ['scope ' ] !== 'private ' );
131
142
$ errorData = [$ varName ];
132
143
133
144
if ($ public === true ) {
134
- if (substr ($ varName , 0 , 1 ) === ' _ ' ) {
145
+ if (strpos ($ varName , ' _ ' ) === 0 ) {
135
146
$ error = '%s member variable "%s" must not contain a leading underscore ' ;
136
- $ data = [
147
+ $ data = [
137
148
ucfirst ($ memberProps ['scope ' ]),
138
149
$ errorData [0 ],
139
150
];
140
151
$ phpcsFile ->addError ($ error , $ stackPtr , 'PublicHasUnderscore ' , $ data );
141
152
}
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 );
147
156
}
148
157
149
158
// Remove a potential underscore prefix for testing CamelCaps.
150
159
$ varName = ltrim ($ varName , '_ ' );
151
160
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 ;
155
163
}
156
164
157
- }//end processMemberVar()
158
-
165
+ $ error = 'Member variable "%s" is not in valid camel caps format ' ;
166
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'MemberNotCamelCaps ' , $ errorData );
167
+ }
159
168
160
169
/**
161
170
* Processes the variable found within a double quoted string.
162
171
*
163
- * @param \PHP_CodeSniffer\Files\ File $phpcsFile The file being scanned.
172
+ * @param File $phpcsFile The file being scanned.
164
173
* @param int $stackPtr The position of the double quoted
165
174
* string.
166
175
*
167
- * @return void
176
+ * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
168
177
*/
169
- protected function processVariableInString (File $ phpcsFile , $ stackPtr )
178
+ protected function processVariableInString (File $ phpcsFile , $ stackPtr ): void
170
179
{
171
180
$ tokens = $ phpcsFile ->getTokens ();
172
181
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 ;
186
190
}
187
191
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
+ }
189
197
198
+ if (Common::isCamelCaps ($ varName , false , true , false ) !== false ) {
199
+ continue ;
200
+ }
190
201
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