Invalid call to `init()`
Hi,
the PhpFileExtractor is calling init() on every visitor:
https://github.com/php-translation/extractor/blob/82fc51a07fb626c0bfb31fb3f041baa88fee6acb/src/FileExtractor/PHPFileExtractor.php#L38
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
After trying to come up with a fix, I don't quite grasp the implementation.
It appears that the line
https://github.com/php-translation/extractor/blob/82fc51a07fb626c0bfb31fb3f041baa88fee6acb/src/FileExtractor/PHPFileExtractor.php#L28
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:
- introduce new specialized interfaces
PhpVisitorInterface extends NodeVisitor, Visitor - do manual
instanceofchecks in the extractor.
I can prepare a PR, please just tell me which solution you prefer.