Completed
Push — master ( e82f42...e87d40 )
by Tobias
02:31
created

SourceLocationContainerExtractor::afterTraverse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 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\Visitor\Php;
13
14
use PhpParser\Node;
15
use PhpParser\NodeVisitor;
16
use Translation\Extractor\Model\SourceLocation;
17
18
/**
19
 * Extract translations from classes implementing
20
 * Translation\Extractor\Model\SourceLocation\TranslationSourceLocationContainer.
21
 *
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
class SourceLocationContainerExtractor extends BasePHPVisitor implements NodeVisitor
25
{
26
    /**
27
     * @var string
28
     */
29
    private $namespace = '';
30
31
    /**
32
     * @var array
33
     */
34
    private $useStatements = [];
35
36 1
    public function beforeTraverse(array $nodes)
37
    {
38 1
    }
39
40 1
    public function enterNode(Node $node)
41
    {
42 1
        if ($node instanceof Node\Stmt\Namespace_) {
43 1
            if (isset($node->name)) {
44
                // Save namespace of this class for later.
45 1
                $this->namespace = implode('\\', $node->name->parts);
0 ignored issues
show
Deprecated Code introduced by
The property PhpParser\Node\Name::$parts has been deprecated with message: Avoid directly accessing $parts, use methods instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
46 1
            }
47 1
            $this->useStatements = [];
48
49 1
            return;
50
        }
51
52 1
        if ($node instanceof Node\Stmt\UseUse) {
53 1
            $this->useStatements[$node->alias] = implode('\\', $node->name->parts);
54
55 1
            return;
56
        }
57
58 1
        if (!$node instanceof Node\Stmt\Class_) {
59 1
            return;
60
        }
61
62 1
        $isContainer = false;
63 1
        foreach ($node->implements as $interface) {
64 1
            $name = implode('\\', $interface->parts);
0 ignored issues
show
Deprecated Code introduced by
The property PhpParser\Node\Name::$parts has been deprecated with message: Avoid directly accessing $parts, use methods instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
65 1
            if (isset($this->useStatements[$name])) {
66 1
                $name = $this->useStatements[$name];
67 1
            }
68
69 1
            if ('Translation\Extractor\TranslationSourceLocationContainer' === $name) {
70 1
                $isContainer = true;
71 1
                break;
72
            }
73 1
        }
74
75 1
        if (!$isContainer) {
76
            return;
77
        }
78
79 1
        $sourceLocations = call_user_func([$this->namespace.'\\'.$node->name, 'getTranslationSourceLocations']);
80 1
        if (!is_array($sourceLocations)) {
81
            throw new \RuntimeException(sprintf('%s::getTranslationSourceLocations() was expected to return an array of SourceLocations, but got %s.', $this->namespace.'\\'.$node->name, gettype($sourceLocations)));
82
        }
83
84 1
        foreach ($sourceLocations as $sourceLocation) {
85 1
            if (!$sourceLocation instanceof SourceLocation) {
86
                throw new \RuntimeException(sprintf('%s::getTranslationSourceLocations() was expected to return an array of SourceLocations, but got an array which contains an item of type %s.', $this->namespace.'\\'.$node->name, gettype($sourceLocation)));
87
            }
88
89 1
            $this->collection->addLocation($sourceLocation);
90 1
        }
91 1
    }
92
93 1
    public function leaveNode(Node $node)
94
    {
95 1
    }
96
97 1
    public function afterTraverse(array $nodes)
98
    {
99 1
    }
100
}
101