Completed
Push — master ( 0a4015...907089 )
by Tobias
01:30
created

src/Visitor/Php/SourceLocationContainerVisitor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 2
    public function beforeTraverse(array $nodes)
37
    {
38 2
    }
39
40 2
    public function enterNode(Node $node)
41
    {
42 2 View Code Duplication
        if ($node instanceof Node\Stmt\Namespace_) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43 2
            if (isset($node->name)) {
44
                // Save namespace of this class for later.
45 2
                $this->namespace = implode('\\', $node->name->parts);
46
            }
47 2
            $this->useStatements = [];
48
49 2
            return;
50
        }
51
52 2
        if ($node instanceof Node\Stmt\UseUse) {
53 2
            $key = isset($node->alias) ? $node->alias : $node->name->parts[count($node->name->parts) - 1];
54 2
            $this->useStatements[(string) $key] = implode('\\', $node->name->parts);
55
56 2
            return;
57
        }
58
59 2
        if (!$node instanceof Node\Stmt\Class_) {
60 2
            return;
61
        }
62
63 2
        $isContainer = false;
64 2
        foreach ($node->implements as $interface) {
65 2
            $name = implode('\\', $interface->parts);
66 2
            if (isset($this->useStatements[$name])) {
67 2
                $name = $this->useStatements[$name];
68
            }
69
70 2
            if ('Translation\Extractor\TranslationSourceLocationContainer' === $name) {
71 2
                $isContainer = true;
72
73 2
                break;
74
            }
75
        }
76
77 2
        if (!$isContainer) {
78 1
            return;
79
        }
80
81 2
        $sourceLocations = call_user_func([$this->namespace.'\\'.$node->name, 'getTranslationSourceLocations']);
82 2
        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 2
        foreach ($sourceLocations as $sourceLocation) {
87 2
            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 2
            $this->collection->addLocation($sourceLocation);
92
        }
93 2
    }
94
95 2
    public function leaveNode(Node $node)
96
    {
97 2
    }
98
99 2
    public function afterTraverse(array $nodes)
100
    {
101 2
    }
102
}
103