-
-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic/CyclomaticComplexity: potential inconsistencies when counting cyclomatic complexity #744
Comments
@rodrigoprimo Thanks for opening this issue. For reference - issues regarding previous changes made to the sniff (added tokens), including one which is still open which contains an interesting discussion about the concept: squizlabs/PHP_CodeSniffer#3501
This will need a backtrace to see when the If I look at the explanation regarding this in the PDepends documentation, I agree with you that it should not be included, just like If we want to apply this consistently though, it also means that Looking even more closely at the Along similar lines, all Consider these three code samples, which are functionally the same, but have quite different cyclomatic complexity: // Cyclomatic complexity: 12.
function switching() {
switch ($foo) {
case 0:
case 1:
case 2:
do_something();
break;
case 3:
case 4:
case 5:
do_something();
break;
case 6:
case 7:
case 8:
do_something();
break;
case 9:
default:
do_something();
break;
}
}
// Cyclomatic complexity: 6.
function switchTrue() {
switch (true) {
case $foo == 0 || $foo == 1 || $foo == 2:
do_something();
break;
case $foo == 3 || $foo == 4 || $foo == 5:
do_something();
break;
case $foo == 6 || $foo == 7 || $foo == 8:
do_something();
break;
case 9:
default:
do_something();
break;
}
}
// Cyclomatic complexity: 4.
function usingIfs() {
if ($foo == 0 || $foo == 1 || $foo == 2) {
do_something();
} elseif ($foo == 3 || $foo == 4 || $foo == 5) {
do_something();
} elseif ($foo == 6 || $foo == 7 || $foo == 8) {
do_something();
} else {
do_something();
}
}
I fully agree. |
Maybe @MarkBaker would be interested in having another look at this, seeing as he made most of the more recent changes ? |
@jrfnl, I believe you meant to link to squizlabs/PHP_CodeSniffer#3501 instead of squizlabs/PHP_CodeSniffer#350 |
Similarly, in the case of compound I can probably take another look and see how these issues might be resolved. |
And with some more thought: a |
Is your feature request related to a problem?
While working on #684, I noticed some divergences between how PHPCS and PHPMD/PHP Depend count cyclomatic complexity. For example, PHPCS increases the cyclomatic complexity by one whenever it finds a default statement, while PHPMD/PHP Depend doesn't.
Maybe there needs to be a review of the sniff to ensure that it is correctly counting cyclomatic complexity? This issue is somewhat related to #726, which presents another case that the sniff considers but that potentially it should not.
I'm not sure if PHPCS or PHPMD is correct, and I was not able to find what is the definition of cyclomatic complexity that is used by the sniff to check if it is following it or not.
The text was updated successfully, but these errors were encountered: