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

Error when parsing method that creates anonymous class #616

remorhaz opened this issue Jan 24, 2019 · 1 comment · Fixed by #630

Error when parsing method that creates anonymous class #616

remorhaz opened this issue Jan 24, 2019 · 1 comment · Fixed by #630


Copy link

remorhaz commented Jan 24, 2019

Question Answer
Infection version 0.11.5 (infection.phar --version)
Test Framework version PHPUnit 7.5.2
PHP version 7.1.27 (php -v)
Platform Linux
Github Repo -

Infection reports an error if the following code occurs in source folder:

namespace My\Namespace;

class Bug

    public function createAnonymousClass()
        new class
            public function foo()

The error is:

In CodeCoverageMethodIgnoreVisitor.php line 56:

  Method foo does not exist

Seems that code seeks method in host class instead of anonymous one, because if I add private function foo() {} to Bug class, the error dissapears.
I don't know either bug is in PHP-Parser or in Infection, but it affects your software anyway.

@BackEndTea BackEndTea added the Bug label Jan 25, 2019
Copy link

BackEndTea commented Jan 25, 2019

I think we have some tests on dealing with anonymous classes, but maybe not for the CodeCoverageMethodIgnoreVisitor.

This could either be solved here:

if ($node instanceof Node\Stmt\ClassLike && isset($node->fullyQualifiedClassName)) {

By keeping a stack of the fqn, in the same way we do with the functions. here:

$this->scopeStack[] = $node;

Or we can solve it by wraping the following line in a try{}catch{}, and returing null in the catch.

$method = $reflection->getMethod($node->name->toString());

I'm more of a fan of the first solution since it will prevent the same kind of error in the future, but it may require more refactoring in other places.

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

Successfully merging a pull request may close this issue.

3 participants