Completed
Push — master ( 09dcac...3533be )
by Дмитрий
06:57
created

ResolveExpressionTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 93.88%

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 46
cts 49
cp 0.9388
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveFunctionName() 0 15 4
A findNode() 0 8 3
B traverseArray() 0 16 6
C traverseNode() 0 23 8
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Helper;
7
8
use PhpParser\Node;
9
use PhpParser\Node\Expr\FuncCall;
10
use PhpParser\Node\Expr\Yield_;
11
use PhpParser\Node\Stmt\Return_;
12
use PHPSA\Context;
13
14
trait ResolveExpressionTrait
15
{
16
    /**
17
     * @param FuncCall $funcCall
18
     * @param Context $context
19
     * @return string|bool
20
     */
21 15
    public function resolveFunctionName(FuncCall $funcCall, Context $context)
22
    {
23 15
        $funcNameCompiledExpression = $context->getExpressionCompiler()->compile($funcCall->name);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $funcNameCompiledExpression exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
24
25 15
        if ($funcNameCompiledExpression->isString() && $funcNameCompiledExpression->isCorrectValue()) {
26 13
            return $funcNameCompiledExpression->getValue();
27 2
        } elseif (!$funcNameCompiledExpression->isCallable()) {
28 2
            $context->debug(
29 2
                'Unexpected function name type ' . $funcNameCompiledExpression->getTypeName(),
30 2
                $funcCall->name
31 2
            );
32 2
        }
33
34 2
        return false;
35
    }
36
37
    /**
38
     * @param array $nodes
39
     * @param string $nodeName Class name of Node(s) what We should return
40
     * @return \Generator
41
     */
42 12
    protected function findNode(array $nodes, $nodeName)
43
    {
44 12
        foreach ($this->traverseArray($nodes) as $node) {
45 12
            if (get_class($node) === $nodeName) {
46 4
                yield $node;
47 3
            }
48 12
        }
49 12
    }
50
51
    /**
52
     * For the code above
53
     * Я атеист, но когда я начинал это писать, только Бог и я понимали, что я делаю
54
     * Сейчас остался только Бог
55
     *
56
     * @param Node $node
57
     * @return \Generator
58
     *
59
     * @todo After move to PHP 7.0+ use yield from
60
     */
61 12
    protected function traverseNode(Node $node)
62
    {
63
        // Skip inherits nodes
64 12
        if ($node instanceof Node\FunctionLike || $node instanceof Node\Stmt\ClassLike) {
65 1
            return;
66
        }
67
68 12
        foreach ($node->getSubNodeNames() as $name) {
69 12
            $subNode = &$node->$name;
70
71 12
            if (is_array($subNode)) {
72 8
                foreach ($this->traverseArray($subNode) as $rNode) {
73 7
                    yield $rNode;
74 8
                }
75 12
            } elseif ($subNode instanceof Node) {
76 10
                yield $subNode;
77
78 10
                foreach ($this->traverseNode($subNode) as $rNode) {
79 7
                    yield $rNode;
80 10
                }
81 10
            }
82 12
        }
83 12
    }
84
85
    /**
86
     * @param array $nodes
87
     * @return \Generator
88
     */
89 12
    protected function traverseArray(array $nodes)
90
    {
91 12
        foreach ($nodes as $node) {
92 12
            if (is_array($node)) {
93
                foreach ($this->traverseArray($node) as $rNode) {
94
                    yield $rNode;
95
                }
96 12
            } elseif ($node instanceof Node) {
97 12
                yield $node;
98
99 12
                foreach ($this->traverseNode($node) as $rNode) {
100 10
                    yield $rNode;
101 12
                }
102 12
            }
103 12
        }
104 12
    }
105
}
106