FlashMessage::afterTraverse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 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\Symfony;
13
14
use PhpParser\Node;
15
use PhpParser\NodeVisitor;
16
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class FlashMessage extends BasePHPVisitor implements NodeVisitor
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function beforeTraverse(array $nodes): ?Node
27
    {
28 2
        return null;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function enterNode(Node $node): ?Node
35
    {
36 2
        if (!$node instanceof Node\Expr\MethodCall) {
37 2
            return null;
38
        }
39
40 2
        if (!\is_string($node->name) && !$node->name instanceof Node\Identifier) {
41 1
            return null;
42
        }
43
44 2
        $name = (string) $node->name;
45
46
        // This prevents dealing with some fatal edge cases when getting the callerName
47 2
        if (!\in_array($name, ['addFlash', 'add'])) {
48 1
            return null;
49
        }
50
51 2
        $caller = $node->var;
52
        // $caller might be "Node\Expr\New_"
53 2
        $callerName = isset($caller->name) ? (string) $caller->name : '';
54
55
        /*
56
         * Make sure the caller is from a variable named "this" or a function called "getFlashbag"
57
         */
58
        //If $this->addFlash()  or xxx->getFlashbag()->add()
59 2
        if (('addFlash' === $name && 'this' === $callerName && $caller instanceof Node\Expr\Variable) ||
60 2
            ('add' === $name && 'getFlashBag' === $callerName && $caller instanceof Node\Expr\MethodCall)
61
        ) {
62 2
            if (null !== $label = $this->getStringArgument($node, 1)) {
63 2
                $this->addLocation($label, $node->getAttribute('startLine'), $node);
64
            }
65
        }
66
67 2
        return null;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function leaveNode(Node $node): ?Node
74
    {
75 2
        return null;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function afterTraverse(array $nodes): ?Node
82
    {
83 2
        return null;
84
    }
85
}
86