Completed
Push — master ( 8009cb...0bf5b8 )
by Olivier
03:52 queued 11s
created

ItemLabel::enterNode()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.0155

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 15
cts 16
cp 0.9375
rs 8.1954
c 0
b 0
f 0
cc 8
nc 7
nop 1
crap 8.0155
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\Knp\Menu;
13
14
use PhpParser\Node;
15
use PhpParser\NodeVisitor;
16
17
/**
18
 * This class extracts knp menu item labels:
19
 *     - $menu->addChild('foo')
20
 *     - $menu['foo']->setLabel('bar').
21
 */
22
final class ItemLabel extends AbstractKnpMenuVisitor implements NodeVisitor
23
{
24 2
    public function enterNode(Node $node): ?Node
25
    {
26 2
        if (!$this->isKnpMenuBuildingMethod($node)) {
27 2
            return null;
28
        }
29
30 2
        parent::enterNode($node);
31
32 2
        if (!$node instanceof Node\Expr\MethodCall) {
33 2
            return null;
34
        }
35
36 2
        if (!\is_string($node->name) && !$node->name instanceof Node\Identifier) {
37
            return null;
38
        }
39
40 2
        $methodName = (string) $node->name;
41 2
        if (!\in_array($methodName, ['addChild', 'setLabel'], true)) {
42 2
            return null;
43
        }
44
45 2
        if (null !== $label = $this->getStringArgument($node, 0)) {
46 2
            $line = $node->getAttribute('startLine');
47 2
            if (null !== $location = $this->getLocation($label, $line, $node)) {
48 2
                $this->lateCollect($location);
49
            }
50
        }
51
52 2
        return null;
53
    }
54
}
55