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

Invalid call to init() #128

Open
apfelbox opened this issue Jul 2, 2019 · 1 comment
Open

Invalid call to init() #128

apfelbox opened this issue Jul 2, 2019 · 1 comment

Comments

@apfelbox
Copy link

apfelbox commented Jul 2, 2019

Hi,

the PhpFileExtractor is calling init() on every visitor:

$v->init($collection, $file);

although one of the allowed types is PhpParser\NodeVisitor, which doesn't have an init() method: https://github.com/nikic/PHP-Parser/blob/3f718ee2c3a22b656773476f19dd8d72a50e2413/lib/PhpParser/NodeVisitor.php

@apfelbox
Copy link
Author

apfelbox commented Jul 2, 2019

After trying to come up with a fix, I don't quite grasp the implementation.

It appears that the line

* @var Visitor[]|NodeVisitor[]

is meant that the expected type is an array of objects that implement both interfaces, because the Visitor interface only has the init() method and can therefore not be used with PhpParser\NodeTraverser:: addVisitor().

A type-safe fix would mean to separate the Visitor interface to a PhpVisitorInterface, that has an init() method and extends PhpParser\NodeVisitor.

The current implementation seems to try to keep the visitor implementations sort of abstract by having a single main interface, but different implementations of the visitor need to implement different additional interfaces -- so that is basically not type safe at all right now.

I see two possible solutions:

  1. introduce new specialized interfaces PhpVisitorInterface extends NodeVisitor, Visitor
  2. do manual instanceof checks in the extractor.

I can prepare a PR, please just tell me which solution you prefer.

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

No branches or pull requests

1 participant