extractor icon indicating copy to clipboard operation
extractor copied to clipboard

Invalid call to `init()`

Open apfelbox opened this issue 6 years ago • 1 comments

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

apfelbox avatar Jul 02 '19 08:07 apfelbox

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:

  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.

apfelbox avatar Jul 02 '19 08:07 apfelbox