Completed
Push — master ( 0a4015...907089 )
by Tobias
01:30
created

src/FileExtractor/TwigFileExtractor.php (1 issue)

Upgrade to new PHP Analysis Engine

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
It seems like you code against a concrete implementation and not the interface Twig\NodeVisitor\NodeVisitorInterface as the method init() does only exist in the following implementations of said interface: Translation\Extractor\Visitor\Twig\Twig2Visitor.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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