Completed
Push — master ( 019c60...7ec1c5 )
by Tobias
05:21
created

SourceLocationContainerVisitor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 79
ccs 33
cts 36
cp 0.9167
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeTraverse() 0 3 1
C enterNode() 0 54 13
A leaveNode() 0 3 1
A afterTraverse() 0 3 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
final class SourceLocationContainerVisitor 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);
46
            }
47 1
            $this->useStatements = [];
48
49 1
            return;
50
        }
51
52 1
        if ($node instanceof Node\Stmt\UseUse) {
53 1
            $key = isset($node->alias) ? $node->alias : $node->name->parts[count($node->name->parts) - 1];
54 1
            $this->useStatements[$key] = implode('\\', $node->name->parts);
55
56 1
            return;
57
        }
58
59 1
        if (!$node instanceof Node\Stmt\Class_) {
60 1
            return;
61
        }
62
63 1
        $isContainer = false;
64 1
        foreach ($node->implements as $interface) {
65 1
            $name = implode('\\', $interface->parts);
66 1
            if (isset($this->useStatements[$name])) {
67 1
                $name = $this->useStatements[$name];
68
            }
69
70 1
            if ('Translation\Extractor\TranslationSourceLocationContainer' === $name) {
71 1
                $isContainer = true;
72
73 1
                break;
74
            }
75
        }
76
77 1
        if (!$isContainer) {
78
            return;
79
        }
80
81 1
        $sourceLocations = call_user_func([$this->namespace.'\\'.$node->name, 'getTranslationSourceLocations']);
82 1
        if (!is_array($sourceLocations)) {
83
            throw new \RuntimeException(sprintf('%s::getTranslationSourceLocations() was expected to return an array of SourceLocations, but got %s.', $this->namespace.'\\'.$node->name, gettype($sourceLocations)));
84
        }
85
86 1
        foreach ($sourceLocations as $sourceLocation) {
87 1
            if (!$sourceLocation instanceof SourceLocation) {
88
                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)));
89
            }
90
91 1
            $this->collection->addLocation($sourceLocation);
92
        }
93 1
    }
94
95 1
    public function leaveNode(Node $node)
96
    {
97 1
    }
98
99 1
    public function afterTraverse(array $nodes)
100
    {
101 1
    }
102
}
103