Completed
Push — master ( 6ab7bd...834806 )
by Дмитрий
05:03
created

ResolveExpressionTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 69.57%

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 85
ccs 32
cts 46
cp 0.6957
rs 10
wmc 19
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveFunctionName() 0 15 4
A findReturnStatement() 0 8 3
B traverseNode() 0 18 6
B traverseArray() 0 14 6
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\Stmt\Return_;
11
use PHPSA\Context;
12
13
trait ResolveExpressionTrait
14
{
15
    /**
16
     * @param FuncCall $funcCall
17
     * @param Context $context
18
     * @return string|bool
19
     * @throws \PHPSA\Exception\RuntimeException
20
     */
21 9
    public function resolveFunctionName(FuncCall $funcCall, Context $context)
22
    {
23 9
        $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 9
        if ($funcNameCompiledExpression->isString() && $funcNameCompiledExpression->isCorrectValue()) {
26 9
            return $funcNameCompiledExpression->getValue();
27
        } else if (!$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 \PhpParser\Node[] $nodes
39
     * @return \PhpParser\Node\Stmt\Return_
40
     */
41 1
    protected function findReturnStatement(array $nodes)
42
    {
43 1
        foreach ($this->traverseArray($nodes) as $node) {
44 1
            if ($node instanceof Return_) {
45 1
                yield $node;
46 1
            }
47 1
        }
48 1
    }
49
50
    /**
51
     * For the code above
52
     * Я атеист, но когда я начинал это писать, только Бог и я понимали, что я делаю
53
     * Сейчас остался только Бог
54
     */
55
56
    /**
57
     * @param Node $node
58
     * @return \Generator
59
     */
60 1
    protected function traverseNode(Node $node)
61
    {
62 1
        foreach ($node->getSubNodeNames() as $name) {
63 1
            $subNode = &$node->$name;
64
65 1
            if (is_array($subNode)) {
66
                foreach ($this->traverseArray($subNode) as $rNode) {
67
                    yield $rNode;
68
                }
69 1
            } elseif ($subNode instanceof Node) {
70 1
                yield $node;
71
72 1
                foreach ($this->traverseNode($subNode) as $rNode) {
73
                    yield $rNode;
74 1
                }
75 1
            }
76 1
        }
77 1
    }
78
79
    /**
80
     * @param array $nodes
81
     * @return \Generator
82
     */
83 1
    protected function traverseArray(array $nodes)
84
    {
85 1
        foreach ($nodes as $node) {
86 1
            if (is_array($node)) {
87
                foreach ($this->traverseArray($node) as $rNode) {
88
                    yield $rNode;
89
                }
90 1
            } elseif ($node instanceof Node) {
91 1
                foreach ($this->traverseNode($node) as $rNode) {
92 1
                    yield $rNode;
93 1
                }
94 1
            }
95 1
        }
96 1
    }
97
}
98