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.

ScopeInterpreter::visitSlice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Pinq\Queries\Builders;
4
5
use Pinq\Direction;
6
use Pinq\Expressions as O;
7
use Pinq\IQueryable;
8
use Pinq\PinqException;
9
use Pinq\Queries;
10
use Pinq\Queries\Builders\Interpretations\IScopeInterpretation;
11
use Pinq\Queries\Segments;
12
use Pinq\Utilities;
13
14
/**
15
 * Implementation of the scope expression interpreter.
16
 *
17
 * @author Elliot Levin <[email protected]>
18
 */
19
class ScopeInterpreter extends ExpressionInterpreter implements IScopeInterpreter
20
{
21
    /**
22
     * @var IScopeInterpretation
23
     */
24
    protected $interpretation;
25
26
    /**
27
     * Because the method expression are evaluated top-down,
28
     * Query segments are interpreted in reverse order, so they are stored as
29
     * callbacks and called in reverse order.
30
     *
31
     * @var callable[]
32
     */
33
    protected $segmentCallbacks = [];
34
35
    /**
36
     * @var int
37
     */
38
    protected $segmentCounter = 0;
39
40
    /**
41
     * @var string
42
     */
43
    protected $segmentId = '';
44
45
    public function __construct(
46
            IScopeInterpretation $interpretation,
47
            O\IEvaluationContext $evaluationContext = null,
48
            $idPrefix = 'scope'
49
    ) {
50
        parent::__construct($idPrefix, $evaluationContext);
51
        $this->interpretation = $interpretation;
52
    }
53
54
    public function getInterpretation()
55
    {
56
        return $this->interpretation;
57
    }
58
59
    public function interpretScope(O\Expression $expression)
60
    {
61
        $this->segmentCallbacks = [];
62
        $this->visit($expression);
63
64
        foreach (array_reverse($this->segmentCallbacks) as $callback) {
65
            $callback();
66
        }
67
    }
68
69
    final protected function visit(O\Expression $expression)
70
    {
71
        if ($expression instanceof O\ValueExpression) {
72
            $queryable = $expression->getValue();
73
            if (!($queryable instanceof IQueryable)) {
74
                throw new PinqException('Invalid scope expression: must originate from %s, %s given',
75
                        IQueryable::IQUERYABLE_TYPE,
76
                        Utilities::getTypeOrClass($queryable));
77
            }
78
79
            if ($queryable->isSource()) {
80
                $this->addSegment(
81
                        function () use ($queryable) {
82
                            $this->interpretation->interpretScopeSource($queryable);
83
                        }
84
                );
85
86
                return;
87
            }
88
89
            $expression = $queryable->getExpression();
90
        }
91
92
        $methodName = $this->getMethodName($expression);
93
        $this->segmentCounter++;
94
        $this->segmentId = "{$this->segmentCounter}-{$methodName}";
95
        if (!method_exists($this, "visit$methodName")) {
96
            throw new PinqException('Cannot interpret query scope with method call \'%s\'', $methodName);
97
        }
98
        $this->{"visit$methodName"}($expression);
99
    }
100
101
    final protected function addSegment(callable $segmentCallback)
102
    {
103
        $segmentId                = $this->segmentId;
104
        $this->segmentCallbacks[] = function () use ($segmentCallback, $segmentId) {
105
            $segmentCallback($segmentId);
106
        };
107
    }
108
109 View Code Duplication
    final protected function visitWhere(O\MethodCallExpression $expression)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $this->addSegment(
112
                function ($segmentId) use ($expression) {
113
                    $this->interpretation->interpretWhere(
114
                            $segmentId,
115
                            $this->getFunctionAt("$segmentId-predicate", 0, $expression)
116
                    );
117
                }
118
        );
119
120
        $this->visit($expression->getValue());
121
    }
122
123
    final protected function getSegmentId($parameter = null)
124
    {
125
        return $parameter === null ? $this->segmentId : "{$this->segmentId}-$parameter";
126
    }
127
128
    final protected function visitOrderBy(O\MethodCallExpression $expression)
129
    {
130
        $this->visitOrderings($expression);
131
    }
132
133
    final protected function visitOrderings(O\MethodCallExpression $expression)
134
    {
135
        $orderings = [];
136
137
        $count = 0;
138
        while ($expression instanceof O\MethodCallExpression
139
                && stripos($methodName = $this->getMethodName($expression), 'thenBy') === 0) {
140
141
            $orderings[] = $this->visitOrdering($count, $expression);
142
            $expression  = $expression->getValue();
143
            $count++;
144
        }
145
146
        if (stripos($this->getMethodName($expression), 'orderBy') !== 0) {
147
            throw new PinqException(
148
                    'Cannot visit ordering query: must begin with an orderBy[Ascending|Descending] query method, %s given');
149
        }
150
151
        $orderings[] = $this->visitOrdering(++$count, $expression);
152
153
        $this->addSegment(
154
                function ($segmentId) use ($orderings) {
155
                    $this->interpretation->interpretOrderings($segmentId, array_reverse($orderings));
156
                }
157
        );
158
159
        $this->visit($expression->getValue());
160
    }
161
162
    final protected function visitOrdering($count, O\MethodCallExpression $expression)
163
    {
164
        $projection = $this->getFunctionAt($this->getSegmentId("order-$count"), 0, $expression);
165
166
        $methodName = $this->getMethodName($expression);
167
        if (stripos($methodName, 'Ascending') !== false) {
168
            $isAscending = true;
169
        } elseif (stripos($methodName, 'Descending') !== false) {
170
            $isAscending = false;
171
        } else {
172
            $isAscending = $this->getArgumentValueAt(1, $expression) !== Direction::DESCENDING;
173
        }
174
175
        return [
176
                $projection,
177
                $this->getSegmentId("$count-isAscending"),
178
                $isAscending
179
        ];
180
    }
181
182
    final protected function visitOrderByAscending(O\MethodCallExpression $expression)
183
    {
184
        $this->visitOrderings($expression);
185
    }
186
187
    final protected function visitOrderByDescending(O\MethodCallExpression $expression)
188
    {
189
        $this->visitOrderings($expression);
190
    }
191
192
    final protected function visitThenBy(O\MethodCallExpression $expression)
193
    {
194
        $this->visitOrderings($expression);
195
    }
196
197
    final protected function visitThenByAscending(O\MethodCallExpression $expression)
198
    {
199
        $this->visitOrderings($expression);
200
    }
201
202
    final protected function visitThenByDescending(O\MethodCallExpression $expression)
203
    {
204
        $this->visitOrderings($expression);
205
    }
206
207
    final protected function visitSlice(O\MethodCallExpression $expression)
208
    {
209
        $this->addSlice($this->getArgumentValueAt(0, $expression), $this->getArgumentValueAt(1, $expression));
210
211
        $this->visit($expression->getValue());
212
    }
213
214
    final protected function addSlice($start, $amount)
215
    {
216
        $this->addSegment(
217
                function ($segmentId) use ($start, $amount) {
218
                    $this->interpretation->interpretSlice(
219
                            $segmentId,
220
                            "$segmentId-start",
221
                            $start,
222
                            "$segmentId-amount",
223
                            $amount
224
                    );
225
                }
226
        );
227
    }
228
229
    final protected function visitSkip(O\MethodCallExpression $expression)
230
    {
231
        $this->addSlice($this->getArgumentValueAt(0, $expression), null);
232
233
        $this->visit($expression->getValue());
234
    }
235
236
    final protected function visitTake(O\MethodCallExpression $expression)
237
    {
238
        $this->addSlice(0, $this->getArgumentValueAt(0, $expression));
239
240
        $this->visit($expression->getValue());
241
    }
242
243 View Code Duplication
    final protected function visitIndexBy(O\MethodCallExpression $expression)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245
        $this->addSegment(
246
                function ($segmentId) use ($expression) {
247
                    $this->interpretation->interpretIndexBy(
248
                            $segmentId,
249
                            $this->getFunctionAt("$segmentId-projection", 0, $expression)
250
                    );
251
                }
252
        );
253
254
        $this->visit($expression->getValue());
255
    }
256
257
    final protected function visitKeys(O\MethodCallExpression $expression)
258
    {
259
        $this->addSegment(
260
                function ($segmentId) {
261
                    $this->interpretation->interpretKeys($segmentId);
262
                }
263
        );
264
265
        $this->visit($expression->getValue());
266
    }
267
268
    final protected function visitReindex(O\MethodCallExpression $expression)
269
    {
270
        $this->addSegment(
271
                function ($segmentId) {
272
                    $this->interpretation->interpretReindex($segmentId);
273
                }
274
        );
275
276
        $this->visit($expression->getValue());
277
    }
278
279 View Code Duplication
    final protected function visitGroupBy(O\MethodCallExpression $expression)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
    {
281
        $this->addSegment(
282
                function ($segmentId) use ($expression) {
283
                    $this->interpretation->interpretGroupBy(
284
                            $segmentId,
285
                            $this->getFunctionAt("$segmentId-projection", 0, $expression)
286
                    );
287
                }
288
        );
289
290
        $this->visit($expression->getValue());
291
    }
292
293
    final protected function visitTo(O\MethodCallExpression $expression)
294
    {
295
        $joinToFunction = $this->getFunctionAt($this->getSegmentId('projection'), 0, $expression);
296
        $expression     = $this->getSourceMethodCall($expression);
297
298
        $optionsInterpreter = $this->buildJoinOptionsInterpreter($this->segmentId);
299
        $optionsInterpreter->interpretJoinOptions(
300
                $expression,
301
                $sourceExpression
302
        );
303
304
        $this->addSegment(
305
                function ($segmentId) use ($optionsInterpreter, $joinToFunction) {
306
                    $this->interpretation->interpretJoin(
307
                            $segmentId,
308
                            $optionsInterpreter->getInterpretation(),
309
                            $joinToFunction
310
                    );
311
                }
312
        );
313
        $this->visit($sourceExpression->getValue());
314
    }
315
316
    public function buildJoinOptionsInterpreter($segmentId)
317
    {
318
        return new JoinOptionsInterpreter(
319
                $segmentId,
320
                $this->interpretation->buildJoinOptionsInterpretation(),
321
                $this->buildSourceInterpreter($segmentId),
322
                $this->evaluationContext
323
        );
324
    }
325
326
    public function buildSourceInterpreter($segmentId)
327
    {
328
        return new SourceInterpreter(
329
                $segmentId,
330
                $this->interpretation->buildSourceInterpretation(),
331
                new static(
332
                        $this->interpretation->buildScopeInterpretation(),
333
                        $this->evaluationContext,
334
                        "$segmentId-scope"),
335
            $this->evaluationContext
336
        );
337
    }
338
339 View Code Duplication
    final protected function visitSelect(O\MethodCallExpression $expression)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
340
    {
341
        $this->addSegment(
342
                function ($segmentId) use ($expression) {
343
                    $this->interpretation->interpretSelect(
344
                            $segmentId,
345
                            $this->getFunctionAt("$segmentId-projection", 0, $expression)
346
                    );
347
                }
348
        );
349
        $this->visit($expression->getValue());
350
    }
351
352 View Code Duplication
    final protected function visitSelectMany(O\MethodCallExpression $expression)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
353
    {
354
        $this->addSegment(
355
                function ($segmentId) use ($expression) {
356
                    $this->interpretation->interpretSelectMany(
357
                            $segmentId,
358
                            $this->getFunctionAt("$segmentId-projection", 0, $expression)
359
                    );
360
                }
361
        );
362
        $this->visit($expression->getValue());
363
    }
364
365
    final protected function visitUnique(O\MethodCallExpression $expression)
366
    {
367
        $this->addSegment(
368
                function ($segmentId) {
369
                    $this->interpretation->interpretUnique($segmentId);
370
                }
371
        );
372
        $this->visit($expression->getValue());
373
    }
374
375
    final protected function visitAppend(O\MethodCallExpression $expression)
376
    {
377
        $this->visitOperation(Segments\Operation::APPEND, $expression);
378
    }
379
380
    final protected function visitOperation($operationType, O\MethodCallExpression $expression)
381
    {
382
        $this->addSegment(
383
                function ($segmentId) use ($operationType, $expression) {
384
                    $sourceInterpreter = $this->buildSourceInterpreter($segmentId);
385
                    $sourceInterpreter->interpretSource($this->getArgumentAt(0, $expression));
386
387
                    $this->interpretation->interpretOperation(
388
                            $this->getSegmentId($operationType),
389
                            $operationType,
390
                            $sourceInterpreter->getInterpretation()
391
                    );
392
                }
393
        );
394
395
        $this->visit($expression->getValue());
396
    }
397
398
    final protected function visitWhereIn(O\MethodCallExpression $expression)
399
    {
400
        $this->visitOperation(Segments\Operation::WHERE_IN, $expression);
401
    }
402
403
    final protected function visitExcept(O\MethodCallExpression $expression)
404
    {
405
        $this->visitOperation(Segments\Operation::EXCEPT, $expression);
406
    }
407
408
    final protected function visitUnion(O\MethodCallExpression $expression)
409
    {
410
        $this->visitOperation(Segments\Operation::UNION, $expression);
411
    }
412
413
    final protected function visitIntersect(O\MethodCallExpression $expression)
414
    {
415
        $this->visitOperation(Segments\Operation::INTERSECT, $expression);
416
    }
417
418
    final protected function visitDifference(O\MethodCallExpression $expression)
419
    {
420
        $this->visitOperation(Segments\Operation::DIFFERENCE, $expression);
421
    }
422
}
423