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

fix(node-visitor): Ensure that missing statements do not crash in visitor #7

Closed
wants to merge 1 commit into from

Conversation

MattyRad
Copy link
Contributor

@MattyRad MattyRad commented Aug 17, 2023

Hello,

I'm not too familiar with assertions in PHP, so perhaps the expectation of this library is that you must have assertions configured in a certain way. If so, feel free to close the issue/PR.

assert(is_array($statements));

Nevertheless, I'm encountering an error while computing complexity on interfaces.

SebastianBergmann\Complexity\ComplexityCalculatingVisitor::cyclomaticComplexity(): Argument #1 ($statements) must be of type array, null given, called in .../vendor/sebastian/complexity/src/Visitor/ComplexityCalculatingVisitor.php on line 61

I understand that interfaces have no complexity... so another solution is to filter interfaces and prevent them from ever having their complexity computed. But it would be nice to be able to scan an entire directory without having to perform tedious filtering.

If you'd prefer not to blanket return null, checking the parent node might also work:

        if ((!$node instanceof ClassMethod && !$node instanceof Function_) || $node->getAttribute('parent') instanceof Interface_) { return null; }

@sebastianbergmann
Copy link
Owner

perhaps the expectation of this library is that you must have assertions configured in a certain way

I use assertions such as assert(is_array($statements)); do document assumptions. Static analysis tools such as Psalm or IDEs such as PhpStorm use this information to reason about the code. Furthermore, I have configured

zend.assertions=1
assert.exception=1

so that assert() statements are evaluated by the PHP runtime and result in an exception when they do not hold.

@sebastianbergmann
Copy link
Owner

Please rebase your branch against main so that you get af325e1. This should fix the Composer-related build failures. Sorry for the inconvenience.

@sebastianbergmann
Copy link
Owner

Nevermind, I just merged your test and changed the visitor to ignore interfaces. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants