Skip to content

Commit

Permalink
Resolve double-increment of the CyclomaticComplexity count for do/whi…
Browse files Browse the repository at this point in the history
…le loop

This is a bugfix for Issue #3468, which identified that the count was being incremented twice when calculating the score because the sniff was checking for both the T_DO and the terminating T_WHILE tokens, incrementing CYC by two (once for each token) in the do/while loop, rather than just by one (as it should).

Code in the unit test .inc file has ben modified to reflect this change, and the test php file has also been modified to reflect the change in error line numbers.
  • Loading branch information
MarkBaker authored and gsherwood committed Dec 6, 2021
1 parent c1a5420 commit 77c4747
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function process(File $phpcsFile, $stackPtr)
T_FOR => true,
T_FOREACH => true,
T_WHILE => true,
T_DO => true,
// T_DO is not required for incrementing CYC, as the terminating while in a do/while loop triggers the branch.
// T_DO => true.
T_ELSEIF => true,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ function complexityTwenty()

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
do {
if ($condition) {
} else if ($cond) {
}
} while ($cond);
break;
case '2':
while ($cond) {
Expand Down Expand Up @@ -116,9 +118,11 @@ function complexityTwenty()
function complexityTwentyOne()
{
while ($condition === true) {
if ($condition) {
} else if ($cond) {
}
do {
if ($condition) {
} else if ($cond) {
}
} while ($cond);
}

switch ($condition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CyclomaticComplexityUnitTest extends AbstractSniffUnitTest
*/
public function getErrorList()
{
return [116 => 1];
return [118 => 1];

}//end getErrorList()

Expand Down

0 comments on commit 77c4747

Please sign in to comment.