TreeWalkerChain   F
last analyzed

Complexity

Total Complexity 98

Size/Duplication

Total Lines 526
Duplicated Lines 0 %

Test Coverage

Coverage 6.97%

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 526
ccs 14
cts 201
cp 0.0697
rs 2
c 0
b 0
f 0
wmc 98

51 Methods

Rating   Name   Duplication   Size   Complexity  
A walkInputParameter() 0 4 2
A walkHavingClause() 0 4 2
A walkDeleteStatement() 0 4 2
A walkConditionalPrimary() 0 4 2
A walkPathExpression() 0 4 2
A walkGroupByItem() 0 4 2
A walkSubselect() 0 4 2
A walkUpdateClause() 0 4 2
A walkSimpleArithmeticExpression() 0 4 2
A walkSelectClause() 0 4 2
A walkQuantifiedExpression() 0 4 2
A walkFunction() 0 4 2
A walkResultVariable() 0 4 2
A walkEmptyCollectionComparisonExpression() 0 4 2
A walkCollectionMemberExpression() 0 4 2
A walkSimpleSelectExpression() 0 4 2
A walkSubselectFromClause() 0 4 2
A walkAggregateExpression() 0 4 2
A walkInExpression() 0 4 2
A walkSelectExpression() 0 4 2
A walkInstanceOfExpression() 0 4 2
A getExecutor() 0 2 1
A walkExistsExpression() 0 4 2
A walkConditionalExpression() 0 4 2
A walkStringPrimary() 0 4 2
A walkComparisonExpression() 0 4 2
A walkGroupByClause() 0 4 2
A walkArithmeticFactor() 0 4 2
A walkStateFieldPathExpression() 0 4 2
A walkUpdateStatement() 0 4 2
A walkNullComparisonExpression() 0 4 2
A walkLikeExpression() 0 4 2
A walkOrderByClause() 0 4 2
A walkUpdateItem() 0 4 2
A addTreeWalker() 0 3 1
A walkArithmeticExpression() 0 4 2
A getQueryComponents() 0 3 1
A walkArithmeticTerm() 0 4 2
A __construct() 0 4 1
A walkLiteral() 0 4 2
A walkJoin() 0 4 2
A walkWhereClause() 0 4 2
A walkDeleteClause() 0 4 2
A walkBetweenExpression() 0 4 2
A walkOrderByItem() 0 4 2
A walkConditionalFactor() 0 4 2
A walkSimpleSelectClause() 0 4 2
A walkFromClause() 0 4 2
A walkConditionalTerm() 0 4 2
A setQueryComponent() 0 9 2
A walkSelectStatement() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like TreeWalkerChain often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TreeWalkerChain, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query;
6
7
use function array_diff;
8
use function array_keys;
9
10
/**
11
 * Represents a chain of tree walkers that modify an AST and finally emit output.
12
 * Only the last walker in the chain can emit output. Any previous walkers can modify
13
 * the AST to influence the final output produced by the last walker.
14
 */
15
class TreeWalkerChain implements TreeWalker
16
{
17
    /**
18
     * The tree walkers.
19
     *
20
     * @var TreeWalker[]
21
     */
22
    private $walkers;
23
24
    /**
25
     * The query components of the original query (the "symbol table") that was produced by the Parser.
26
     *
27
     * @var mixed[][]
28
     */
29
    private $queryComponents;
30
31
    /**
32
     * Returns the internal queryComponents array.
33
     *
34
     * @return mixed[][]
35
     */
36 98
    public function getQueryComponents()
37
    {
38 98
        return $this->queryComponents;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function setQueryComponent($dqlAlias, array $queryComponent)
45
    {
46
        $requiredKeys = ['metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token'];
47
48
        if (array_diff($requiredKeys, array_keys($queryComponent))) {
49
            throw QueryException::invalidQueryComponent($dqlAlias);
50
        }
51
52
        $this->queryComponents[$dqlAlias] = $queryComponent;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 98
    public function __construct($query, $parserResult, array $queryComponents)
59
    {
60 98
        $this->queryComponents = $queryComponents;
61 98
        $this->walkers         = new TreeWalkerChainIterator($this, $query, $parserResult);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\ORM\Query\T... $query, $parserResult) of type Doctrine\ORM\Query\TreeWalkerChainIterator is incompatible with the declared type Doctrine\ORM\Query\TreeWalker[] of property $walkers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 98
    }
63
64
    /**
65
     * Adds a tree walker to the chain.
66
     *
67
     * @param string $walkerClass The class of the walker to instantiate.
68
     */
69 98
    public function addTreeWalker($walkerClass)
70
    {
71 98
        $this->walkers[] = $walkerClass;
72 98
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 98
    public function walkSelectStatement(AST\SelectStatement $AST)
78
    {
79 98
        foreach ($this->walkers as $walker) {
80 98
            $walker->walkSelectStatement($AST);
81
82 92
            $this->queryComponents = $walker->getQueryComponents();
83
        }
84 92
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function walkSelectClause($selectClause)
90
    {
91
        foreach ($this->walkers as $walker) {
92
            $walker->walkSelectClause($selectClause);
93
        }
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function walkFromClause($fromClause)
100
    {
101
        foreach ($this->walkers as $walker) {
102
            $walker->walkFromClause($fromClause);
103
        }
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function walkFunction($function)
110
    {
111
        foreach ($this->walkers as $walker) {
112
            $walker->walkFunction($function);
113
        }
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function walkOrderByClause($orderByClause)
120
    {
121
        foreach ($this->walkers as $walker) {
122
            $walker->walkOrderByClause($orderByClause);
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function walkOrderByItem($orderByItem)
130
    {
131
        foreach ($this->walkers as $walker) {
132
            $walker->walkOrderByItem($orderByItem);
133
        }
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function walkHavingClause($havingClause)
140
    {
141
        foreach ($this->walkers as $walker) {
142
            $walker->walkHavingClause($havingClause);
143
        }
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function walkJoin($join)
150
    {
151
        foreach ($this->walkers as $walker) {
152
            $walker->walkJoin($join);
153
        }
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function walkSelectExpression($selectExpression)
160
    {
161
        foreach ($this->walkers as $walker) {
162
            $walker->walkSelectExpression($selectExpression);
163
        }
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function walkQuantifiedExpression($qExpr)
170
    {
171
        foreach ($this->walkers as $walker) {
172
            $walker->walkQuantifiedExpression($qExpr);
173
        }
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function walkSubselect($subselect)
180
    {
181
        foreach ($this->walkers as $walker) {
182
            $walker->walkSubselect($subselect);
183
        }
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function walkSubselectFromClause($subselectFromClause)
190
    {
191
        foreach ($this->walkers as $walker) {
192
            $walker->walkSubselectFromClause($subselectFromClause);
193
        }
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function walkSimpleSelectClause($simpleSelectClause)
200
    {
201
        foreach ($this->walkers as $walker) {
202
            $walker->walkSimpleSelectClause($simpleSelectClause);
203
        }
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function walkSimpleSelectExpression($simpleSelectExpression)
210
    {
211
        foreach ($this->walkers as $walker) {
212
            $walker->walkSimpleSelectExpression($simpleSelectExpression);
213
        }
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function walkAggregateExpression($aggExpression)
220
    {
221
        foreach ($this->walkers as $walker) {
222
            $walker->walkAggregateExpression($aggExpression);
223
        }
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function walkGroupByClause($groupByClause)
230
    {
231
        foreach ($this->walkers as $walker) {
232
            $walker->walkGroupByClause($groupByClause);
233
        }
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function walkGroupByItem($groupByItem)
240
    {
241
        foreach ($this->walkers as $walker) {
242
            $walker->walkGroupByItem($groupByItem);
243
        }
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function walkUpdateStatement(AST\UpdateStatement $AST)
250
    {
251
        foreach ($this->walkers as $walker) {
252
            $walker->walkUpdateStatement($AST);
253
        }
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function walkDeleteStatement(AST\DeleteStatement $AST)
260
    {
261
        foreach ($this->walkers as $walker) {
262
            $walker->walkDeleteStatement($AST);
263
        }
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function walkDeleteClause(AST\DeleteClause $deleteClause)
270
    {
271
        foreach ($this->walkers as $walker) {
272
            $walker->walkDeleteClause($deleteClause);
273
        }
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279
    public function walkUpdateClause($updateClause)
280
    {
281
        foreach ($this->walkers as $walker) {
282
            $walker->walkUpdateClause($updateClause);
283
        }
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function walkUpdateItem($updateItem)
290
    {
291
        foreach ($this->walkers as $walker) {
292
            $walker->walkUpdateItem($updateItem);
293
        }
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299
    public function walkWhereClause($whereClause)
300
    {
301
        foreach ($this->walkers as $walker) {
302
            $walker->walkWhereClause($whereClause);
303
        }
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function walkConditionalExpression($condExpr)
310
    {
311
        foreach ($this->walkers as $walker) {
312
            $walker->walkConditionalExpression($condExpr);
313
        }
314
    }
315
316
    /**
317
     * {@inheritdoc}
318
     */
319
    public function walkConditionalTerm($condTerm)
320
    {
321
        foreach ($this->walkers as $walker) {
322
            $walker->walkConditionalTerm($condTerm);
323
        }
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function walkConditionalFactor($factor)
330
    {
331
        foreach ($this->walkers as $walker) {
332
            $walker->walkConditionalFactor($factor);
333
        }
334
    }
335
336
    /**
337
     * {@inheritdoc}
338
     */
339
    public function walkConditionalPrimary($condPrimary)
340
    {
341
        foreach ($this->walkers as $walker) {
342
            $walker->walkConditionalPrimary($condPrimary);
343
        }
344
    }
345
346
    /**
347
     * {@inheritdoc}
348
     */
349
    public function walkExistsExpression($existsExpr)
350
    {
351
        foreach ($this->walkers as $walker) {
352
            $walker->walkExistsExpression($existsExpr);
353
        }
354
    }
355
356
    /**
357
     * {@inheritdoc}
358
     */
359
    public function walkCollectionMemberExpression($collMemberExpr)
360
    {
361
        foreach ($this->walkers as $walker) {
362
            $walker->walkCollectionMemberExpression($collMemberExpr);
363
        }
364
    }
365
366
    /**
367
     * {@inheritdoc}
368
     */
369
    public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
370
    {
371
        foreach ($this->walkers as $walker) {
372
            $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr);
373
        }
374
    }
375
376
    /**
377
     * {@inheritdoc}
378
     */
379
    public function walkNullComparisonExpression($nullCompExpr)
380
    {
381
        foreach ($this->walkers as $walker) {
382
            $walker->walkNullComparisonExpression($nullCompExpr);
383
        }
384
    }
385
386
    /**
387
     * {@inheritdoc}
388
     */
389
    public function walkInExpression($inExpr)
390
    {
391
        foreach ($this->walkers as $walker) {
392
            $walker->walkInExpression($inExpr);
393
        }
394
    }
395
396
    /**
397
     * {@inheritdoc}
398
     */
399
    public function walkInstanceOfExpression($instanceOfExpr)
400
    {
401
        foreach ($this->walkers as $walker) {
402
            $walker->walkInstanceOfExpression($instanceOfExpr);
403
        }
404
    }
405
406
    /**
407
     * {@inheritdoc}
408
     */
409
    public function walkLiteral($literal)
410
    {
411
        foreach ($this->walkers as $walker) {
412
            $walker->walkLiteral($literal);
413
        }
414
    }
415
416
    /**
417
     * {@inheritdoc}
418
     */
419
    public function walkBetweenExpression($betweenExpr)
420
    {
421
        foreach ($this->walkers as $walker) {
422
            $walker->walkBetweenExpression($betweenExpr);
423
        }
424
    }
425
426
    /**
427
     * {@inheritdoc}
428
     */
429
    public function walkLikeExpression($likeExpr)
430
    {
431
        foreach ($this->walkers as $walker) {
432
            $walker->walkLikeExpression($likeExpr);
433
        }
434
    }
435
436
    /**
437
     * {@inheritdoc}
438
     */
439
    public function walkStateFieldPathExpression($stateFieldPathExpression)
440
    {
441
        foreach ($this->walkers as $walker) {
442
            $walker->walkStateFieldPathExpression($stateFieldPathExpression);
443
        }
444
    }
445
446
    /**
447
     * {@inheritdoc}
448
     */
449
    public function walkComparisonExpression($compExpr)
450
    {
451
        foreach ($this->walkers as $walker) {
452
            $walker->walkComparisonExpression($compExpr);
453
        }
454
    }
455
456
    /**
457
     * {@inheritdoc}
458
     */
459
    public function walkInputParameter($inputParam)
460
    {
461
        foreach ($this->walkers as $walker) {
462
            $walker->walkInputParameter($inputParam);
463
        }
464
    }
465
466
    /**
467
     * {@inheritdoc}
468
     */
469
    public function walkArithmeticExpression($arithmeticExpr)
470
    {
471
        foreach ($this->walkers as $walker) {
472
            $walker->walkArithmeticExpression($arithmeticExpr);
473
        }
474
    }
475
476
    /**
477
     * {@inheritdoc}
478
     */
479
    public function walkArithmeticTerm($term)
480
    {
481
        foreach ($this->walkers as $walker) {
482
            $walker->walkArithmeticTerm($term);
483
        }
484
    }
485
486
    /**
487
     * {@inheritdoc}
488
     */
489
    public function walkStringPrimary($stringPrimary)
490
    {
491
        foreach ($this->walkers as $walker) {
492
            $walker->walkStringPrimary($stringPrimary);
493
        }
494
    }
495
496
    /**
497
     * {@inheritdoc}
498
     */
499
    public function walkArithmeticFactor($factor)
500
    {
501
        foreach ($this->walkers as $walker) {
502
            $walker->walkArithmeticFactor($factor);
503
        }
504
    }
505
506
    /**
507
     * {@inheritdoc}
508
     */
509
    public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
510
    {
511
        foreach ($this->walkers as $walker) {
512
            $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr);
513
        }
514
    }
515
516
    /**
517
     * {@inheritdoc}
518
     */
519
    public function walkPathExpression($pathExpr)
520
    {
521
        foreach ($this->walkers as $walker) {
522
            $walker->walkPathExpression($pathExpr);
523
        }
524
    }
525
526
    /**
527
     * {@inheritdoc}
528
     */
529
    public function walkResultVariable($resultVariable)
530
    {
531
        foreach ($this->walkers as $walker) {
532
            $walker->walkResultVariable($resultVariable);
533
        }
534
    }
535
536
    /**
537
     * {@inheritdoc}
538
     */
539
    public function getExecutor($AST)
540
    {
541
    }
542
}
543