Skip to content

Commit 8d132e5

Browse files
committed
Merge branch 'feature/1880-validvariablename-dont-hide-one-message-behind-another' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 10fadb1 + ccacdd8 commit 8d132e5

File tree

9 files changed

+68
-79
lines changed

9 files changed

+68
-79
lines changed

src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
110110
// Is this a magic method. i.e., is prefixed with "__" ?
111111
if (preg_match('|^__[^_]|', $methodName) !== 0) {
112112
$magicPart = strtolower(substr($methodName, 2));
113-
if (isset($this->magicMethods[$magicPart]) === false
114-
&& isset($this->methodsDoubleUnderscore[$magicPart]) === false
113+
if (isset($this->magicMethods[$magicPart]) === true
114+
|| isset($this->methodsDoubleUnderscore[$magicPart]) === true
115115
) {
116-
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
117-
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
116+
return;
118117
}
119118

120-
return;
119+
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
120+
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
121121
}
122122

123123
// PHP4 constructors are allowed to break our rules.
@@ -178,12 +178,12 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
178178
// Is this a magic function. i.e., it is prefixed with "__".
179179
if (preg_match('|^__[^_]|', $functionName) !== 0) {
180180
$magicPart = strtolower(substr($functionName, 2));
181-
if (isset($this->magicFunctions[$magicPart]) === false) {
182-
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
183-
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
181+
if (isset($this->magicFunctions[$magicPart]) === true) {
182+
return;
184183
}
185184

186-
return;
185+
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
186+
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
187187
}
188188

189189
// Ignore first underscore in functions prefixed with "_".

src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function getErrorList()
4040
31 => 1,
4141
50 => 1,
4242
52 => 1,
43-
53 => 1,
43+
53 => 2,
4444
57 => 1,
4545
58 => 1,
4646
59 => 1,
@@ -52,16 +52,16 @@ public function getErrorList()
5252
65 => 1,
5353
66 => 1,
5454
67 => 1,
55-
68 => 1,
55+
68 => 2,
5656
69 => 1,
5757
71 => 1,
5858
72 => 1,
59-
73 => 1,
59+
73 => 2,
6060
74 => 1,
6161
118 => 1,
6262
144 => 1,
6363
146 => 1,
64-
147 => 1,
64+
147 => 2,
6565
];
6666

6767
return $errors;

src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
8282
// Is this a magic method. i.e., is prefixed with "__" ?
8383
if (preg_match('|^__[^_]|', $methodName) !== 0) {
8484
$magicPart = strtolower(substr($methodName, 2));
85-
if (isset($this->magicMethods[$magicPart]) === false) {
86-
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
87-
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
85+
if (isset($this->magicMethods[$magicPart]) === true) {
86+
return;
8887
}
8988

90-
return;
89+
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
90+
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
9191
}
9292

9393
// PHP4 constructors are allowed to break our rules.
@@ -116,7 +116,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
116116
$error = 'Private method name "%s" must be prefixed with an underscore';
117117
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
118118
$phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'no');
119-
return;
120119
} else {
121120
$phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'yes');
122121
}
@@ -130,20 +129,11 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
130129
$errorData[0],
131130
];
132131
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
133-
return;
134132
}
135133

136-
// If the scope was specified on the method, then the method must be
137-
// camel caps and an underscore should be checked for. If it wasn't
138-
// specified, treat it like a public method and remove the underscore
139-
// prefix if there is one because we cant determine if it is private or
140-
// public.
141-
$testMethodName = $methodName;
142-
if ($scopeSpecified === false && $methodName{0} === '_') {
143-
$testMethodName = substr($methodName, 1);
144-
}
134+
$testMethodName = ltrim($methodName, '_');
145135

146-
if (Common::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
136+
if (Common::isCamelCaps($testMethodName, false, true, false) === false) {
147137
if ($scopeSpecified === true) {
148138
$error = '%s method name "%s" is not in camel caps format';
149139
$data = [
@@ -155,8 +145,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
155145
$error = 'Method name "%s" is not in camel caps format';
156146
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
157147
}
158-
159-
return;
160148
}
161149

162150
}//end processTokenWithinScope()
@@ -189,12 +177,12 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
189177
// Is this a magic function. i.e., it is prefixed with "__".
190178
if (preg_match('|^__[^_]|', $functionName) !== 0) {
191179
$magicPart = strtolower(substr($functionName, 2));
192-
if (isset($this->magicFunctions[$magicPart]) === false) {
193-
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
194-
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
180+
if (isset($this->magicFunctions[$magicPart]) === true) {
181+
return;
195182
}
196183

197-
return;
184+
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
185+
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
198186
}
199187

200188
// Function names can be in two parts; the package name and
@@ -216,13 +204,11 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
216204
if ($functionName{0} === '_') {
217205
$error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore';
218206
$phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData);
219-
return;
220207
}
221208

222209
if ($functionName{0} !== strtoupper($functionName{0})) {
223210
$error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter';
224211
$phpcsFile->addError($error, $stackPtr, 'FunctionNoCapital', $errorData);
225-
return;
226212
}
227213
}
228214

src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ public function getErrorList()
3232
14 => 1,
3333
15 => 1,
3434
16 => 1,
35-
17 => 1,
36-
18 => 1,
37-
19 => 1,
38-
20 => 1,
35+
17 => 2,
36+
18 => 2,
37+
19 => 2,
38+
20 => 2,
3939
24 => 1,
4040
25 => 1,
4141
26 => 1,
4242
27 => 1,
4343
28 => 1,
4444
29 => 1,
45-
30 => 1,
46-
31 => 1,
47-
32 => 1,
48-
33 => 1,
45+
30 => 2,
46+
31 => 2,
47+
32 => 2,
48+
33 => 2,
4949
35 => 1,
5050
36 => 1,
51-
37 => 1,
52-
38 => 1,
53-
39 => 1,
54-
40 => 1,
51+
37 => 2,
52+
38 => 2,
53+
39 => 2,
54+
40 => 2,
5555
43 => 1,
5656
44 => 1,
5757
45 => 1,
@@ -70,26 +70,26 @@ public function getErrorList()
7070
70 => 1,
7171
71 => 1,
7272
72 => 1,
73-
73 => 1,
74-
74 => 1,
75-
75 => 1,
76-
76 => 1,
73+
73 => 2,
74+
74 => 2,
75+
75 => 2,
76+
76 => 2,
7777
80 => 1,
7878
81 => 1,
7979
82 => 1,
8080
83 => 1,
8181
84 => 1,
8282
85 => 1,
83-
86 => 1,
84-
87 => 1,
85-
88 => 1,
86-
89 => 1,
83+
86 => 2,
84+
87 => 2,
85+
88 => 2,
86+
89 => 2,
8787
91 => 1,
8888
92 => 1,
89-
93 => 1,
90-
94 => 1,
91-
95 => 1,
92-
96 => 1,
89+
93 => 2,
90+
94 => 2,
91+
95 => 2,
92+
96 => 2,
9393
99 => 1,
9494
100 => 1,
9595
101 => 1,
@@ -108,13 +108,13 @@ public function getErrorList()
108108
124 => 1,
109109
125 => 1,
110110
126 => 1,
111-
127 => 1,
112-
128 => 1,
113-
129 => 1,
114-
130 => 1,
111+
127 => 2,
112+
128 => 2,
113+
129 => 2,
114+
130 => 2,
115115
149 => 1,
116116
151 => 1,
117-
152 => 1,
117+
152 => 2,
118118
155 => 1,
119119
156 => 1,
120120
157 => 1,
@@ -126,18 +126,18 @@ public function getErrorList()
126126
163 => 1,
127127
164 => 1,
128128
165 => 1,
129-
166 => 1,
129+
166 => 3,
130130
167 => 1,
131131
169 => 1,
132132
170 => 1,
133-
171 => 1,
134-
173 => 1,
133+
171 => 3,
134+
173 => 2,
135135
174 => 1,
136136
175 => 1,
137137
207 => 1,
138138
227 => 1,
139139
229 => 1,
140-
230 => 1,
140+
230 => 2,
141141
];
142142

143143
}//end getErrorList()

src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
3939
if (preg_match('|^__[^_]|', $functionName) !== 0) {
4040
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
4141
$phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData);
42-
return;
42+
43+
$functionName = ltrim($functionName, '_');
4344
}
4445

4546
if (Common::isCamelCaps($functionName, false, true, false) === false) {

src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,18 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
139139
$errorData[0],
140140
];
141141
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
142-
return;
143142
}
144143
} else {
145144
if (substr($varName, 0, 1) !== '_') {
146145
$error = 'Private member variable "%s" must contain a leading underscore';
147146
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
148-
return;
149147
}
150148
}
151149

152-
if (Common::isCamelCaps($varName, false, $public, false) === false) {
150+
// Remove a potential underscore prefix for testing CamelCaps.
151+
$varName = ltrim($varName, '_');
152+
153+
if (Common::isCamelCaps($varName, false, true, false) === false) {
153154
$error = 'Member variable "%s" is not in valid camel caps format';
154155
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData);
155156
}

src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getErrorList()
3535
11 => 1,
3636
12 => 1,
3737
13 => 1,
38-
14 => 1,
38+
14 => 2,
3939
];
4040

4141
}//end getErrorList()

src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getErrorList()
5151
67 => 1,
5252
81 => 1,
5353
106 => 1,
54-
107 => 1,
54+
107 => 2,
5555
108 => 1,
5656
117 => 1,
5757
];

src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
141141
$error = 'Public member variable "%s" must not contain a leading underscore';
142142
$data = [$varName];
143143
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
144-
return;
145144
}
146145
} else {
147146
if (substr($varName, 0, 1) !== '_') {
@@ -152,11 +151,13 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
152151
$varName,
153152
];
154153
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
155-
return;
156154
}
157155
}
158156

159-
if (Common::isCamelCaps($varName, false, $public, false) === false) {
157+
// Remove a potential underscore prefix for testing CamelCaps.
158+
$varName = ltrim($varName, '_');
159+
160+
if (Common::isCamelCaps($varName, false, true, false) === false) {
160161
$error = 'Member variable "%s" is not in valid camel caps format';
161162
$data = [$varName];
162163
$phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);

0 commit comments

Comments
 (0)