Completed
Push — master ( d76c5e...03e4c2 )
by Tobias
03:46
created

TwigFileExtractor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 52
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSourceLocations() 0 18 3
A getType() 0 4 1
A addVisitor() 0 4 1
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 Translation\Extractor\Visitor\Visitor;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class TwigFileExtractor implements FileExtractor
22
{
23
    /**
24
     * @var Visitor[]|\Twig_NodeVisitorInterface[]
25
     */
26
    private $visitors = [];
27
28
    /**
29
     * @var \Twig_Environment
30
     */
31
    private $twig;
32
33
    /**
34
     * @param \Twig_Environment $twig
35
     */
36 4
    public function __construct(\Twig_Environment $twig)
37
    {
38 4
        $this->twig = $twig;
39 4
    }
40
41 4
    public function getSourceLocations(SplFileInfo $file, SourceCollection $collection)
42
    {
43 4
        foreach ($this->visitors as $v) {
44 4
            $v->init($collection, $file);
0 ignored issues
show
Bug introduced by
The method init does only exist in Translation\Extractor\Visitor\Visitor, but not in Twig_NodeVisitorInterface.

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...
45 4
        }
46
47 4
        $path = $file->getRelativePath();
48
49 4
        if (class_exists('Twig_Source')) {
50
            // Twig 2.0
51 4
            $stream = $this->twig->tokenize(new \Twig_Source($file->getContents(), $file->getRelativePathname(), $path));
52 4
        } else {
53
            $stream = $this->twig->tokenize($file->getContents(), $path);
0 ignored issues
show
Documentation introduced by
$file->getContents() is of type string, but the function expects a object<Twig_Source>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Twig_Environment::tokenize() has too many arguments starting with $path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
        }
55 4
        $tokens = $this->twig->parse($stream);
56 4
        $traverser = new \Twig_NodeTraverser($this->twig, $this->visitors);
0 ignored issues
show
Documentation introduced by
$this->visitors is of type array<integer,object<Tra..._NodeVisitorInterface>>, but the function expects a array<integer,object<Twig_NodeVisitorInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57 4
        $traverser->traverse($tokens);
58 4
    }
59
60 1
    public function getType()
61
    {
62 1
        return 'twig';
63
    }
64
65
    /**
66
     * @param \Twig_NodeVisitorInterface $visitor
67
     */
68 4
    public function addVisitor(\Twig_NodeVisitorInterface $visitor)
69
    {
70 4
        $this->visitors[] = $visitor;
71 4
    }
72
}
73