Skip to content

Commit ee7034b

Browse files
authored
Merge pull request PHPCSStandards#684 from rodrigoprimo/test-coverage-cyclomatic-complexity
Generic/CyclomaticComplexity: improve code coverage
2 parents c942e6a + 54a05bc commit ee7034b

5 files changed

+63
-29
lines changed

src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public function process(File $phpcsFile, $stackPtr)
6060
{
6161
$tokens = $phpcsFile->getTokens();
6262

63-
// Ignore abstract methods.
64-
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
63+
// Ignore abstract and interface methods. Bail early when live coding.
64+
if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
6565
return;
6666
}
6767

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.inc renamed to src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.1.inc

+19-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function complexityEleven()
4646
{
4747
while ($condition === true) {
4848
if ($condition) {
49-
} else if ($cond) {
49+
} elseif ($cond) {
5050
}
5151
}
5252

@@ -61,11 +61,11 @@ function complexityEleven()
6161
echo 'hi';
6262
}
6363
break;
64-
case '3':
65-
break;
6664
default:
6765
break;
6866
}
67+
68+
foreach ($array as $element) {}
6969
}
7070

7171

@@ -136,14 +136,6 @@ function complexityTwentyOne()
136136
echo 'hi';
137137
}
138138
break;
139-
case '3':
140-
switch ($cond) {
141-
case '1':
142-
break;
143-
case '2':
144-
break;
145-
}
146-
break;
147139
case '4':
148140
do {
149141
if ($condition) {
@@ -159,8 +151,16 @@ function complexityTwentyOne()
159151
}
160152
break;
161153
}
162-
}
163154

155+
try {
156+
for ($i = 0; $i < 10; $i++) {
157+
if ($i % 2) {
158+
doSomething();
159+
}
160+
}
161+
} catch (Exception $e) {
162+
}
163+
}
164164

165165
function complexityTenWithTernaries()
166166
{
@@ -451,4 +451,10 @@ function complexityElevenWithNullSafeOperator()
451451
$bits = $object5->getX()?->getY()?->getZ();
452452
}
453453

454-
?>
454+
abstract class AbstractClass {
455+
abstract public function sniffShouldIgnoreAbstractMethods();
456+
}
457+
458+
interface MyInterface {
459+
public function sniffShouldIgnoreInterfaceMethods();
460+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing opening curly bracket).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
function sniffShouldBailMissingScopeOpener()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing closing curly bracket).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
function sniffShouldBailMissingScopeCloser() {

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.php

+28-14
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ final class CyclomaticComplexityUnitTest 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 file being tested.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [118 => 1];
35+
switch ($testFile) {
36+
case 'CyclomaticComplexityUnitTest.1.inc':
37+
return [118 => 1];
38+
default:
39+
return [];
40+
}
3441

3542
}//end getErrorList()
3643

@@ -41,21 +48,28 @@ public function getErrorList()
4148
* The key of the array should represent the line number and the value
4249
* should represent the number of warnings that should occur on that line.
4350
*
51+
* @param string $testFile The name of the file being tested.
52+
*
4453
* @return array<int, int>
4554
*/
46-
public function getWarningList()
55+
public function getWarningList($testFile='')
4756
{
48-
return [
49-
45 => 1,
50-
72 => 1,
51-
189 => 1,
52-
237 => 1,
53-
285 => 1,
54-
333 => 1,
55-
381 => 1,
56-
417 => 1,
57-
445 => 1,
58-
];
57+
switch ($testFile) {
58+
case 'CyclomaticComplexityUnitTest.1.inc':
59+
return [
60+
45 => 1,
61+
72 => 1,
62+
189 => 1,
63+
237 => 1,
64+
285 => 1,
65+
333 => 1,
66+
381 => 1,
67+
417 => 1,
68+
445 => 1,
69+
];
70+
default:
71+
return [];
72+
}
5973

6074
}//end getWarningList()
6175

0 commit comments

Comments
 (0)