Skip to content

Commit 2fe2330

Browse files
authored
Merge pull request #642 from rodrigoprimo/test-coverage-camel-caps-function-name
Generic/CamelCapsFunctionName: improve code coverage
2 parents 0f5680a + d59edc4 commit 2fe2330

File tree

5 files changed

+93
-51
lines changed

5 files changed

+93
-51
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
113113

114114
$methodName = $phpcsFile->getDeclarationName($stackPtr);
115115
if ($methodName === null) {
116-
// Ignore closures.
116+
// Live coding or parse error. Bow out.
117117
return;
118118
}
119119

@@ -150,7 +150,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
150150
return;
151151
}
152152

153-
// Ignore first underscore in methods prefixed with "_".
153+
// Ignore leading underscores in the method name.
154154
$methodName = ltrim($methodName, '_');
155155

156156
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
@@ -168,7 +168,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
168168
}
169169

170170
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
171-
return;
172171
} else {
173172
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
174173
}
@@ -189,7 +188,7 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
189188
{
190189
$functionName = $phpcsFile->getDeclarationName($stackPtr);
191190
if ($functionName === null) {
192-
// Ignore closures.
191+
// Live coding or parse error. Bow out.
193192
return;
194193
}
195194

@@ -206,7 +205,7 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
206205
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
207206
}
208207

209-
// Ignore first underscore in functions prefixed with "_".
208+
// Ignore leading underscores in the method name.
210209
$functionName = ltrim($functionName, '_');
211210

212211
if (Common::isCamelCaps($functionName, false, true, $this->strict) === false) {

src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.inc renamed to src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,22 @@ enum Suit: string implements Colorful, CardGame {
183183
public function parseMyDSN() {}
184184
public function get_some_value() {}
185185
}
186+
187+
interface MyInterface {
188+
public function getSomeValue();
189+
public function get_some_value();
190+
}
191+
192+
class MyClass {
193+
// phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict false
194+
function strictFOrmatDIsabled() {} // Ok.
195+
// phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict true
196+
197+
function strictFOrmatIsENabled() {} // Not ok.
198+
}
199+
200+
// phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict false
201+
function strictFOrmatDIsabled() {} // Ok.
202+
// phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict true
203+
204+
function strictFOrmatIsENabled() {} // Not ok.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// Intentional parse error (missing method name).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
class My_Class {
8+
public function {}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing function name).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
function

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

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,63 @@ final class CamelCapsFunctionNameUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the test file to process.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
$errors = [
34-
10 => 1,
35-
11 => 1,
36-
12 => 1,
37-
13 => 1,
38-
16 => 1,
39-
17 => 1,
40-
20 => 1,
41-
21 => 1,
42-
24 => 1,
43-
25 => 1,
44-
30 => 1,
45-
31 => 1,
46-
50 => 1,
47-
52 => 1,
48-
53 => 2,
49-
57 => 1,
50-
58 => 1,
51-
59 => 1,
52-
60 => 1,
53-
61 => 1,
54-
62 => 1,
55-
63 => 1,
56-
64 => 1,
57-
65 => 1,
58-
66 => 1,
59-
67 => 1,
60-
68 => 2,
61-
69 => 1,
62-
71 => 1,
63-
72 => 1,
64-
73 => 2,
65-
118 => 1,
66-
144 => 1,
67-
146 => 1,
68-
147 => 2,
69-
158 => 1,
70-
159 => 1,
71-
179 => 1,
72-
180 => 2,
73-
183 => 1,
74-
184 => 1,
75-
];
76-
77-
return $errors;
35+
switch ($testFile) {
36+
case 'CamelCapsFunctionNameUnitTest.1.inc':
37+
return[
38+
10 => 1,
39+
11 => 1,
40+
12 => 1,
41+
13 => 1,
42+
16 => 1,
43+
17 => 1,
44+
20 => 1,
45+
21 => 1,
46+
24 => 1,
47+
25 => 1,
48+
30 => 1,
49+
31 => 1,
50+
50 => 1,
51+
52 => 1,
52+
53 => 2,
53+
57 => 1,
54+
58 => 1,
55+
59 => 1,
56+
60 => 1,
57+
61 => 1,
58+
62 => 1,
59+
63 => 1,
60+
64 => 1,
61+
65 => 1,
62+
66 => 1,
63+
67 => 1,
64+
68 => 2,
65+
69 => 1,
66+
71 => 1,
67+
72 => 1,
68+
73 => 2,
69+
118 => 1,
70+
144 => 1,
71+
146 => 1,
72+
147 => 2,
73+
158 => 1,
74+
159 => 1,
75+
179 => 1,
76+
180 => 2,
77+
183 => 1,
78+
184 => 1,
79+
189 => 1,
80+
197 => 1,
81+
204 => 1,
82+
];
83+
default:
84+
return [];
85+
}//end switch
7886

7987
}//end getErrorList()
8088

0 commit comments

Comments
 (0)