Skip to content

Commit 2371b0a

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/CyclomaticComplexity: improve code coverage
1 parent 0c2d6d4 commit 2371b0a

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function process(File $phpcsFile, $stackPtr)
6060
{
6161
$tokens = $phpcsFile->getTokens();
6262

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

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

Lines changed: 19 additions & 13 deletions
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+
}
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 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()
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 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() {

0 commit comments

Comments
 (0)