GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FunctionLocatorVisitor::leaveNode()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 7
nop 1
1
<?php
2
3
namespace Pinq\Parsing\PhpParser\Visitors;
4
5
use PhpParser\Node;
6
use PhpParser\NodeVisitorAbstract;
7
use Pinq\Expressions as O;
8
use Pinq\Parsing\FunctionDeclaration;
9
use Pinq\Parsing\FunctionLocation;
10
use Pinq\Parsing\FunctionSignature;
11
use Pinq\Parsing\PhpParser\AST;
12
use Pinq\Parsing\PhpParser\LocatedFunctionNode;
13
14
/**
15
 * Visits the supplied nodes and stores any located functions.
16
 *
17
 * @author Elliot Levin <[email protected]>
18
 */
19
class FunctionLocatorVisitor extends NodeVisitorAbstract
20
{
21
    /**
22
     * @var string
23
     */
24
    private $filePath;
25
26
    /**
27
     * The located function nodes grouped by their location hash.
28
     *
29
     * @var LocatedFunctionNode[][]
30
     */
31
    private $functionNodes = [];
32
33
    /**
34
     * @var string|null
35
     */
36
    private $namespace;
37
38
    /**
39
     * @var string|null
40
     */
41
    private $class;
42
43
    /**
44
     * @var string|null
45
     */
46
    private $trait;
47
48
    /**
49
     * @var string|null
50
     */
51
    private $function;
52
53
    /**
54
     * @var int
55
     */
56
    private $closureNestingLevel = 0;
57
58
    public function __construct($filePath)
59
    {
60
        $this->filePath = $filePath;
61
    }
62
63
    /**
64
     * @return LocatedFunctionNode[][]
65
     */
66
    public function getLocatedFunctionNodesMap()
67
    {
68
        return $this->functionNodes;
69
    }
70
71
    private function getFunctionNodeSignature(Node\Stmt\Function_ $node)
72
    {
73
        return FunctionSignature::func(
74
                $node->byRef,
75
                $node->name,
76
                $this->getParameterExpressions($node->params)
77
        );
78
    }
79
80
    private function getClosureNodeSignature(Node\Expr\Closure $node)
81
    {
82
        $scopedVariableNames = [];
83
        foreach ($node->uses as $use) {
84
            $scopedVariableNames[] = $use->var;
85
        }
86
87
        return FunctionSignature::closure(
88
                $node->byRef,
89
                $this->getParameterExpressions($node->params),
90
                $scopedVariableNames
91
        );
92
    }
93
94
    private function getMethodNodeSignature(Node\Stmt\ClassMethod $node)
95
    {
96
        if ($node->isPublic()) {
97
            $accessModifier = FunctionSignature::ACCESS_PUBLIC;
98
        } elseif ($node->isProtected()) {
99
            $accessModifier = FunctionSignature::ACCESS_PROTECTED;
100
        } else {
101
            $accessModifier = FunctionSignature::ACCESS_PRIVATE;
102
        }
103
104
        if ($node->isFinal()) {
105
            $polymorphModifier = FunctionSignature::POLYMORPH_FINAL;
106
        } elseif ($node->isAbstract()) {
107
            $polymorphModifier = FunctionSignature::POLYMORPH_ABSTRACT;
108
        } else {
109
            $polymorphModifier = null;
110
        }
111
112
        return FunctionSignature::method(
113
                $node->byRef,
114
                $accessModifier,
115
                $polymorphModifier,
116
                $node->isStatic(),
117
                $node->name,
118
                $this->getParameterExpressions($node->params)
119
        );
120
    }
121
122
    private function getLocatedFunctionNode(Node $node, FunctionSignature $signature)
123
    {
124
        return new LocatedFunctionNode(
125
                $signature,
126
                $this->getFunctionLocation($node),
127
                $this->getFunctionDeclaration(),
128
                $node);
129
    }
130
131
    /**
132
     * @param Node\Param[] $parameters
133
     *
134
     * @return O\ParameterExpression[]
135
     */
136
    private function getParameterExpressions(array $parameters)
137
    {
138
        return AST::convert($parameters);
139
    }
140
141
    private function getFunctionLocation(Node $node)
142
    {
143
        return new FunctionLocation(
144
                $this->filePath,
145
                $this->namespace,
146
                $node->getAttribute('startLine'),
147
                $node->getAttribute('endLine'));
148
    }
149
150
    private function getFunctionDeclaration()
151
    {
152
        return new FunctionDeclaration(
153
                $this->namespace,
154
                $this->class,
155
                $this->trait,
156
                $this->function,
157
                $this->closureNestingLevel);
158
    }
159
160
    public function enterNode(Node $node)
161
    {
162
        switch (true) {
163
164
            case $node instanceof Node\Stmt\Namespace_:
165
                $this->namespace = (string) $node->name;
166
                break;
167
168
            case $node instanceof Node\Stmt\Class_:
169
                $this->class = $node->name;
170
                break;
171
172
            case $node instanceof Node\Stmt\Trait_:
173
                $this->trait = $node->name;
174
                break;
175
176 View Code Duplication
            case $node instanceof Node\Stmt\Function_:
177
                $signature = $this->getFunctionNodeSignature($node);
178
                $this->foundFunctionNode($this->getLocatedFunctionNode($node, $signature));
179
                $this->function = $node->name;
180
                break;
181
182 View Code Duplication
            case $node instanceof Node\Stmt\ClassMethod:
183
                $signature = $this->getMethodNodeSignature($node);
184
                $this->foundFunctionNode($this->getLocatedFunctionNode($node, $signature));
185
                $this->function = $node->name;
186
                break;
187
188
            case $node instanceof Node\Expr\Closure:
189
                $signature = $this->getClosureNodeSignature($node);
190
                $this->foundFunctionNode($this->getLocatedFunctionNode($node, $signature));
191
                $this->closureNestingLevel++;
192
                break;
193
194
            default:
195
                break;
196
        }
197
    }
198
199
    public function leaveNode(Node $node)
200
    {
201
        switch (true) {
202
203
            case $node instanceof Node\Stmt\Namespace_:
204
                $this->namespace = null;
205
                break;
206
207
            case $node instanceof Node\Stmt\Class_:
208
                $this->class = null;
209
                break;
210
211
            case $node instanceof Node\Stmt\Trait_:
212
                $this->trait = null;
213
                break;
214
215
            case $node instanceof Node\Stmt\Function_:
216
            case $node instanceof Node\Stmt\ClassMethod:
217
                $this->function = null;
218
                break;
219
220
            case $node instanceof Node\Expr\Closure:
221
                $this->closureNestingLevel--;
222
                break;
223
224
            default:
225
                break;
226
        }
227
    }
228
229
    private function foundFunctionNode(LocatedFunctionNode $locatedNode)
230
    {
231
        $locationHash = $locatedNode->getLocation()->getHash();
232
        if (!isset($this->functionNodes[$locationHash])) {
233
            $this->functionNodes[$locationHash] = [];
234
        }
235
236
        $this->functionNodes[$locationHash][] = $locatedNode;
237
    }
238
}
239