These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the PHP Translation package. |
||
5 | * |
||
6 | * (c) PHP Translation team <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Translation\Extractor\FileExtractor; |
||
13 | |||
14 | use Symfony\Component\Finder\SplFileInfo; |
||
15 | use Translation\Extractor\Model\SourceCollection; |
||
16 | use Twig\Environment; |
||
17 | use Twig\Extension\AbstractExtension; |
||
18 | use Twig\NodeVisitor\NodeVisitorInterface; |
||
19 | use Twig\Source; |
||
20 | |||
21 | /** |
||
22 | * @author Tobias Nyholm <[email protected]> |
||
23 | */ |
||
24 | final class TwigFileExtractor extends AbstractExtension implements FileExtractor |
||
25 | { |
||
26 | /** |
||
27 | * @var NodeVisitorInterface[] |
||
28 | */ |
||
29 | private $visitors = []; |
||
30 | |||
31 | /** |
||
32 | * @var Environment |
||
33 | */ |
||
34 | private $twig; |
||
35 | |||
36 | 5 | public function __construct(Environment $twig) |
|
37 | { |
||
38 | 5 | $this->twig = $twig; |
|
39 | 5 | $twig->addExtension($this); |
|
40 | 5 | } |
|
41 | |||
42 | 5 | public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void |
|
43 | { |
||
44 | 5 | foreach ($this->visitors as $v) { |
|
45 | 5 | $v->init($collection, $file); |
|
0 ignored issues
–
show
|
|||
46 | } |
||
47 | |||
48 | 5 | $path = $file->getRelativePath(); |
|
49 | |||
50 | 5 | $stream = $this->twig->tokenize(new Source($file->getContents(), $file->getRelativePathname(), $path)); |
|
51 | 5 | $this->twig->parse($stream); |
|
52 | 5 | } |
|
53 | |||
54 | 1 | public function getType(): string |
|
55 | { |
||
56 | 1 | return 'twig'; |
|
57 | } |
||
58 | |||
59 | 5 | public function addVisitor(NodeVisitorInterface $visitor): void |
|
60 | { |
||
61 | 5 | $this->visitors[] = $visitor; |
|
62 | 5 | } |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 5 | public function getNodeVisitors(): array |
|
68 | { |
||
69 | 5 | return $this->visitors; |
|
70 | } |
||
71 | |||
72 | public function getName(): string |
||
73 | { |
||
74 | return 'php.translation'; |
||
75 | } |
||
76 | } |
||
77 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: