Completed
Push — master ( c06770...3231e7 )
by Tobias
01:32
created

TwigVisitor::enterNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 5
cp 0.8
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.032
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\Visitor\Twig;
13
14
use Translation\Extractor\Visitor\BaseVisitor;
15
use Twig\Environment;
16
use Twig\Node\Node;
17
use Twig\NodeVisitor\NodeVisitorInterface;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
final class TwigVisitor extends BaseVisitor implements NodeVisitorInterface
23
{
24
    private $worker;
25
26 5
    public function __construct(Worker $worker = null)
27
    {
28 5
        if (null === $worker) {
29 5
            $worker = new Worker();
30
        }
31
32 5
        $this->worker = $worker;
33 5
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 5
    public function enterNode(Node $node, Environment $env): Node
39
    {
40
        // If not initialized
41 5
        if (null === $this->collection) {
42
            // We have not executed BaseVisitor::init which means that we are not currently extracting
43
            return $node;
44
        }
45
46
        return $this->worker->work($node, $this->collection, function () {
47 5
            return $this->getAbsoluteFilePath();
48 5
        });
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 5
    public function leaveNode(Node $node, Environment $env): ?Node
55
    {
56 5
        return $node;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 5
    public function getPriority()
63
    {
64 5
        return 0;
65
    }
66
}
67