Completed
Push — master ( 1de6a4...fe9fab )
by Tobias
12:14 queued 10:27
created

SourceLocationContainerVisitor::enterNode()   C

Complexity

Conditions 13
Paths 30

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 13.0501

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 28
cts 30
cp 0.9333
rs 6.7593
c 0
b 0
f 0
cc 13
eloc 29
nc 30
nop 1
crap 13.0501

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        if ($node instanceof Node\Stmt\Namespace_) {
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