Skip to content
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: improve code coverage #684

Merged

Conversation

rodrigoprimo
Copy link
Contributor

Description

This PR improves code coverage for the Generic.Metrics.CyclomaticComplexity sniff.

I noticed three things while working on this sniff that we might want to address in the future:

  • The sniff does not check the cyclomatic complexity of anonymous functions, and I believe it should. That being said, I'm not too familiar with the concept of cyclomatic complexity, and I could be missing something.
  • The sniff error message could be improved if it included the function name.
  • There are 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 (https://pdepend.org/documentation/software-metrics/cyclomatic-complexity.html).

Just looking for some initial feedback on those three items to decide if I should address them or not on future PRs. I am happy to move them to an issue as well if you prefer. I'm inclined to think that the first two items are worthwhile and the third is not (assuming this is not a very popular sniff and that there is no easy-to-find clear definition of how to count cyclomatic complexity in PHP).

Related issues/external references

Part of #146

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

@jrfnl
Copy link
Member

jrfnl commented Nov 24, 2024

Just looking for some initial feedback on those three items to decide if I should address them or not on future PRs. I am happy to move them to an issue as well if you prefer.

Please move these questions/points to individual issues. They each have their own decision point and those decision points are unrelated to the decision point of this PR.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigoprimo Thanks for this PR and for continuing with the work to increase code coverage.

IMO this one needs a little more work.

Aside from the comments left in-line, I'd like to ask you to have a look at the list of tokens the sniff is looking for:

$find = [
T_CASE => true,
T_DEFAULT => true,
T_CATCH => true,
T_IF => true,
T_FOR => true,
T_FOREACH => true,
T_WHILE => true,
T_ELSEIF => true,
T_INLINE_THEN => true,
T_COALESCE => true,
T_COALESCE_EQUAL => true,
T_MATCH_ARROW => true,
T_NULLSAFE_OBJECT_OPERATOR => true,
];

I can see at least four tokens in that list which are not used in the tests. I believe tests should be added/adjusted to ensure those tokens are included in the tests.

Aside from that, on top of the questions you already asked (and which need to be moved to issues), I also saw another issue in this sniff for which I've opened issue #726.

@rodrigoprimo
Copy link
Contributor Author

Please move these questions/points to individual issues. They each have their own decision point and those decision points are unrelated to the decision point of this PR.

I just created the three issues:

#742
#743
#744

@rodrigoprimo
Copy link
Contributor Author

Thanks for your review, @jrfnl. I just replied to your inline comments.

Aside from the comments left in-line, I'd like to ask you to have a look at the list of tokens the sniff is looking for:
I can see at least four tokens in that list which are not used in the tests. I believe tests should be added/adjusted to ensure those tokens are included in the tests.

Good point. I updated the existing tests to include at least one occurrence of each of the tokens that the sniff looks for to count cyclomatic complexity. I added the following tokens: T_ELSEIF, T_CATCH, T_FOREACH and T_FOR.

rodrigoprimo added a commit to rodrigoprimo/PHP_CodeSniffer that referenced this pull request Dec 2, 2024
…` is not set

Quoting @jrfnl: "Only checking for the scope_opener, when
accessing/using both the scope_opener and scope_closer indexes, is
probably fine in practical terms. However, the part of the code base
which sets these indexes is not sufficiently covered by tests, nor does
it document that those indexes will only be set if both can be set, so
there may be edge case exceptions" (PHPCSStandards#684 (comment)).

The sniff was already working fine before this change. Checking if
`scope_closer` is just an extra precaution to err on the side of caution.
Doing this to be able to create tests with syntax errors on separate
files.
@jrfnl jrfnl added this to the 3.11.2 milestone Dec 2, 2024
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for this @rodrigoprimo !

I'll rebase the PR and squash most of the commits. Will merge once the build has passed again.

…` is not set

Quoting @jrfnl: "Only checking for the scope_opener, when
accessing/using both the scope_opener and scope_closer indexes, is
probably fine in practical terms. However, the part of the code base
which sets these indexes is not sufficiently covered by tests, nor does
it document that those indexes will only be set if both can be set, so
there may be edge case exceptions" (PHPCSStandards#684 (comment)).

The sniff was already working fine before this change. Checking if
`scope_closer` is just an extra precaution to err on the side of caution.
@jrfnl jrfnl force-pushed the test-coverage-cyclomatic-complexity branch from 8b26262 to 54a05bc Compare December 2, 2024 21:49
@jrfnl jrfnl enabled auto-merge December 2, 2024 21:50
@jrfnl jrfnl merged commit ee7034b into PHPCSStandards:master Dec 2, 2024
45 checks passed
@jrfnl jrfnl deleted the test-coverage-cyclomatic-complexity branch December 2, 2024 22:18
jrfnl pushed a commit that referenced this pull request Dec 2, 2024
…` is not set

Quoting @jrfnl: "Only checking for the scope_opener, when
accessing/using both the scope_opener and scope_closer indexes, is
probably fine in practical terms. However, the part of the code base
which sets these indexes is not sufficiently covered by tests, nor does
it document that those indexes will only be set if both can be set, so
there may be edge case exceptions" (#684 (comment)).

The sniff was already working fine before this change. Checking if
`scope_closer` is just an extra precaution to err on the side of caution.
rodrigoprimo added a commit to rodrigoprimo/PHP_CodeSniffer that referenced this pull request Dec 9, 2024
… set

Quoting @jrfnl: "Only checking for the scope_opener, when
accessing/using both the scope_opener and scope_closer indexes, is
probably fine in practical terms. However, the part of the code base
which sets these indexes is not sufficiently covered by tests, nor does
it document that those indexes will only be set if both can be set, so
there may be edge case exceptions" (PHPCSStandards#684 (comment)).

The sniff was already working fine before this change. Checking if
`scope_closer` is just an extra precaution to err on the side of caution.

This commit also updates the related code comment to better reflect what
the if condition does.
jrfnl pushed a commit to rodrigoprimo/PHP_CodeSniffer that referenced this pull request Dec 10, 2024
… set

Quoting @jrfnl: "Only checking for the scope_opener, when
accessing/using both the scope_opener and scope_closer indexes, is
probably fine in practical terms. However, the part of the code base
which sets these indexes is not sufficiently covered by tests, nor does
it document that those indexes will only be set if both can be set, so
there may be edge case exceptions" (PHPCSStandards#684 (comment)).

The sniff was already working fine before this change. Checking if
`scope_closer` is just an extra precaution to err on the side of caution.

This commit also updates the related code comment to better reflect what
the if condition does.
jrfnl pushed a commit that referenced this pull request Dec 10, 2024
… set

Quoting @jrfnl: "Only checking for the scope_opener, when
accessing/using both the scope_opener and scope_closer indexes, is
probably fine in practical terms. However, the part of the code base
which sets these indexes is not sufficiently covered by tests, nor does
it document that those indexes will only be set if both can be set, so
there may be edge case exceptions" (#684 (comment)).

The sniff was already working fine before this change. Checking if
`scope_closer` is just an extra precaution to err on the side of caution.

This commit also updates the related code comment to better reflect what
the if condition does.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants