Completed
Push — master ( 7019d5...b13e27 )
by Tobias
03:22
created

TwigVisitor::doEnterNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
cc 2
nc 2
nop 1
crap 2.0185
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
16
/**
17
 * Factory class and base class for TwigVisitor.
18
 *
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
abstract class TwigVisitor extends BaseVisitor
22
{
23
    /**
24
     * @var Worker
25
     */
26
    private $worker;
27
28
    /**
29
     * @param Worker|null $worker
30
     */
31 6
    public function __construct(Worker $worker = null)
32
    {
33 6
        if (null === $worker) {
34 6
            $worker = new Worker();
35
        }
36
37 6
        $this->worker = $worker;
38 6
    }
39
40
    /**
41
     * @return Twig1Visitor|Twig2Visitor
42
     *
43
     * @deprecated since 1.2. Will be removed in 2.0. Use TwigVisitorFactory instead.
44
     */
45
    public static function create()
46
    {
47
        if (-1 === version_compare(\Twig_Environment::VERSION, '2.0')) {
48
            return new Twig1Visitor();
49
        }
50
51
        return new Twig2Visitor();
52
    }
53
54
    /**
55
     * @param \Twig_Node|\Twig_NodeInterface $node
56
     *
57
     * @return \Twig_Node|\Twig_NodeInterface
58
     */
59 6
    protected function doEnterNode($node)
60
    {
61
        // If not initialized
62 6
        if (null === $this->collection) {
63
            // We have not executed BaseVisitor::init which means that we are not currently extracting
64
            return $node;
65
        }
66
67 6
        return $this->worker->work($node, $this->collection, function () {
68 6
            return $this->getAbsoluteFilePath();
69 6
        });
70
    }
71
}
72