Completed
Pull Request — master (#341)
by Strahinja
04:06
created

ResolveExpressionTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 77.55%

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 38
cts 49
cp 0.7755
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 12
    public function resolveFunctionName(FuncCall $funcCall, Context $context)
22
    {
23 12
        $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 12
        if ($funcNameCompiledExpression->isString() && $funcNameCompiledExpression->isCorrectValue()) {
26 12
            return $funcNameCompiledExpression->getValue();
27
        } elseif (!$funcNameCompiledExpression->isCallable()) {
28
            $context->debug(
29
                'Unexpected function name type ' . $funcNameCompiledExpression->getTypeName(),
30
                $funcCall->name
31
            );
32
        }
33
34
        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 1
    protected function findNode(array $nodes, $nodeName)
43
    {
44 1
        foreach ($this->traverseArray($nodes) as $node) {
45 1
            if (get_class($node) === $nodeName) {
46 1
                yield $node;
47 1
            }
48 1
        }
49 1
    }
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 1
    protected function traverseNode(Node $node)
62
    {
63
        // Skip inherits nodes
64 1
        if ($node instanceof Node\FunctionLike || $node instanceof Node\Stmt\ClassLike) {
65 1
            return;
66
        }
67
68 1
        foreach ($node->getSubNodeNames() as $name) {
69 1
            $subNode = &$node->$name;
70
71 1
            if (is_array($subNode)) {
72 1
                foreach ($this->traverseArray($subNode) as $rNode) {
73 1
                    yield $rNode;
74 1
                }
75 1
            } elseif ($subNode instanceof Node) {
76 1
                yield $subNode;
77
78 1
                foreach ($this->traverseNode($subNode) as $rNode) {
79
                    yield $rNode;
80 1
                }
81 1
            }
82 1
        }
83 1
    }
84
85
    /**
86
     * @param array $nodes
87
     * @return \Generator
88
     */
89 1
    protected function traverseArray(array $nodes)
90
    {
91 1
        foreach ($nodes as $node) {
92 1
            if (is_array($node)) {
93
                foreach ($this->traverseArray($node) as $rNode) {
94
                    yield $rNode;
95
                }
96 1
            } elseif ($node instanceof Node) {
97 1
                yield $node;
98
99 1
                foreach ($this->traverseNode($node) as $rNode) {
100 1
                    yield $rNode;
101 1
                }
102 1
            }
103 1
        }
104 1
    }
105
}
106