Skip to content

Commit

Permalink
Merge pull request #684 from rodrigoprimo/test-coverage-cyclomatic-co…
Browse files Browse the repository at this point in the history
…mplexity

Generic/CyclomaticComplexity: improve code coverage
  • Loading branch information
jrfnl authored Dec 2, 2024
2 parents c942e6a + 54a05bc commit ee7034b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function complexityEleven()
{
while ($condition === true) {
if ($condition) {
} else if ($cond) {
} elseif ($cond) {
}
}

Expand All @@ -61,11 +61,11 @@ function complexityEleven()
echo 'hi';
}
break;
case '3':
break;
default:
break;
}

foreach ($array as $element) {}
}


Expand Down Expand Up @@ -136,14 +136,6 @@ function complexityTwentyOne()
echo 'hi';
}
break;
case '3':
switch ($cond) {
case '1':
break;
case '2':
break;
}
break;
case '4':
do {
if ($condition) {
Expand All @@ -159,8 +151,16 @@ function complexityTwentyOne()
}
break;
}
}

try {
for ($i = 0; $i < 10; $i++) {
if ($i % 2) {
doSomething();
}
}
} catch (Exception $e) {
}
}

function complexityTenWithTernaries()
{
Expand Down Expand Up @@ -451,4 +451,10 @@ function complexityElevenWithNullSafeOperator()
$bits = $object5->getX()?->getY()?->getZ();
}

?>
abstract class AbstractClass {
abstract public function sniffShouldIgnoreAbstractMethods();
}

interface MyInterface {
public function sniffShouldIgnoreInterfaceMethods();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing opening curly bracket).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

function sniffShouldBailMissingScopeOpener()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing closing curly bracket).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

function sniffShouldBailMissingScopeCloser() {
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ final class CyclomaticComplexityUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [118 => 1];
switch ($testFile) {
case 'CyclomaticComplexityUnitTest.1.inc':
return [118 => 1];
default:
return [];
}

}//end getErrorList()

Expand All @@ -41,21 +48,28 @@ public function getErrorList()
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [
45 => 1,
72 => 1,
189 => 1,
237 => 1,
285 => 1,
333 => 1,
381 => 1,
417 => 1,
445 => 1,
];
switch ($testFile) {
case 'CyclomaticComplexityUnitTest.1.inc':
return [
45 => 1,
72 => 1,
189 => 1,
237 => 1,
285 => 1,
333 => 1,
381 => 1,
417 => 1,
445 => 1,
];
default:
return [];
}

}//end getWarningList()

Expand Down

0 comments on commit ee7034b

Please sign in to comment.