Skip to content

Commit a9c9566

Browse files
committed
PEAR/ValidFunctionName: don't hide one message behind another
1 parent 7ccf2bf commit a9c9566

File tree

2 files changed

+43
-57
lines changed

2 files changed

+43
-57
lines changed

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()

0 commit comments

Comments
 (0)