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

src/FileExtractor/PHPFileExtractor.php (2 issues)

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 PhpParser\Error;
15
use PhpParser\NodeTraverser;
16
use PhpParser\NodeVisitor;
17
use PhpParser\ParserFactory;
18
use Symfony\Component\Finder\SplFileInfo;
19
use Translation\Extractor\Model\SourceCollection;
20
use Translation\Extractor\Visitor\Visitor;
21
22
/**
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
final class PHPFileExtractor implements FileExtractor
26
{
27
    /**
28
     * @var Visitor[]|NodeVisitor[]
29
     */
30
    private $visitors = [];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 28
    public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
36
    {
37 28
        $path = $file->getRelativePath();
38 28
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
39 28
        $traverser = new NodeTraverser();
40 28
        foreach ($this->visitors as $v) {
41 28
            $v->init($collection, $file);
0 ignored issues
show
The method init does only exist in Translation\Extractor\Visitor\Visitor, but not in PhpParser\NodeVisitor.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42 28
            $traverser->addVisitor($v);
0 ignored issues
show
It seems like $v defined by $v on line 40 can also be of type object<Translation\Extractor\Visitor\Visitor>; however, PhpParser\NodeTraverser::addVisitor() does only seem to accept object<PhpParser\NodeVisitor>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
        }
44
45
        try {
46 28
            $tokens = $parser->parse($file->getContents());
47 28
            $traverser->traverse($tokens);
48
        } catch (Error $e) {
49
            trigger_error(sprintf('Skipping file "%s" because of parse Error: %s. ', $path, $e->getMessage()));
50
        }
51 28
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function getType(): string
57
    {
58 1
        return 'php';
59
    }
60
61 28
    public function addVisitor(NodeVisitor $visitor): void
62
    {
63 28
        $this->visitors[] = $visitor;
64 28
    }
65
}
66