| Total Complexity | 98 |
| Total Lines | 544 |
| Duplicated Lines | 1.84 % |
| Coverage | 8.08% |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 15 | class TreeWalkerChain implements TreeWalker |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The tree walkers. |
||
| 19 | * |
||
| 20 | * @var TreeWalker[] |
||
| 21 | */ |
||
| 22 | private $walkers; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The original Query. |
||
| 26 | * |
||
| 27 | * @var \Doctrine\ORM\AbstractQuery |
||
| 28 | */ |
||
| 29 | private $query; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The ParserResult of the original query that was produced by the Parser. |
||
| 33 | * |
||
| 34 | * @var \Doctrine\ORM\Query\ParserResult |
||
| 35 | */ |
||
| 36 | private $parserResult; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The query components of the original query (the "symbol table") that was produced by the Parser. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $queryComponents; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns the internal queryComponents array. |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function getQueryComponents() |
||
| 51 | { |
||
| 52 | return $this->queryComponents; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | View Code Duplication | public function setQueryComponent($dqlAlias, array $queryComponent) |
|
| 59 | { |
||
| 60 | $requiredKeys = ['metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token']; |
||
| 61 | |||
| 62 | if (array_diff($requiredKeys, array_keys($queryComponent))) { |
||
| 63 | throw QueryException::invalidQueryComponent($dqlAlias); |
||
| 64 | } |
||
| 65 | 92 | ||
| 66 | $this->queryComponents[$dqlAlias] = $queryComponent; |
||
| 67 | 92 | } |
|
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function __construct($query, $parserResult, array $queryComponents) |
||
| 73 | { |
||
| 74 | $this->query = $query; |
||
| 75 | $this->parserResult = $parserResult; |
||
| 76 | $this->queryComponents = $queryComponents; |
||
| 77 | $this->walkers = new TreeWalkerChainIterator($this, $query, $parserResult); |
||
|
|
|||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Adds a tree walker to the chain. |
||
| 82 | * |
||
| 83 | * @param string $walkerClass The class of the walker to instantiate. |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | 92 | public function addTreeWalker($walkerClass) |
|
| 88 | { |
||
| 89 | 92 | $this->walkers[] = $walkerClass; |
|
| 90 | 92 | } |
|
| 91 | 92 | ||
| 92 | 92 | /** |
|
| 93 | 92 | * {@inheritdoc} |
|
| 94 | */ |
||
| 95 | public function walkSelectStatement(AST\SelectStatement $AST) |
||
| 96 | { |
||
| 97 | foreach ($this->walkers as $walker) { |
||
| 98 | $walker->walkSelectStatement($AST); |
||
| 99 | |||
| 100 | $this->queryComponents = $walker->getQueryComponents(); |
||
| 101 | } |
||
| 102 | 92 | } |
|
| 103 | |||
| 104 | 92 | /** |
|
| 105 | 92 | * {@inheritdoc} |
|
| 106 | */ |
||
| 107 | public function walkSelectClause($selectClause) |
||
| 108 | { |
||
| 109 | foreach ($this->walkers as $walker) { |
||
| 110 | 92 | $walker->walkSelectClause($selectClause); |
|
| 111 | } |
||
| 112 | 92 | } |
|
| 113 | 92 | ||
| 114 | /** |
||
| 115 | 86 | * {@inheritdoc} |
|
| 116 | */ |
||
| 117 | 86 | public function walkFromClause($fromClause) |
|
| 118 | { |
||
| 119 | foreach ($this->walkers as $walker) { |
||
| 120 | $walker->walkFromClause($fromClause); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function walkFunction($function) |
||
| 128 | { |
||
| 129 | foreach ($this->walkers as $walker) { |
||
| 130 | $walker->walkFunction($function); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function walkOrderByClause($orderByClause) |
||
| 138 | { |
||
| 139 | foreach ($this->walkers as $walker) { |
||
| 140 | $walker->walkOrderByClause($orderByClause); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | */ |
||
| 147 | public function walkOrderByItem($orderByItem) |
||
| 148 | { |
||
| 149 | foreach ($this->walkers as $walker) { |
||
| 150 | $walker->walkOrderByItem($orderByItem); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function walkHavingClause($havingClause) |
||
| 158 | { |
||
| 159 | foreach ($this->walkers as $walker) { |
||
| 160 | $walker->walkHavingClause($havingClause); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function walkJoin($join) |
||
| 168 | { |
||
| 169 | foreach ($this->walkers as $walker) { |
||
| 170 | $walker->walkJoin($join); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function walkSelectExpression($selectExpression) |
||
| 178 | { |
||
| 179 | foreach ($this->walkers as $walker) { |
||
| 180 | $walker->walkSelectExpression($selectExpression); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public function walkQuantifiedExpression($qExpr) |
||
| 188 | { |
||
| 189 | foreach ($this->walkers as $walker) { |
||
| 190 | $walker->walkQuantifiedExpression($qExpr); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function walkSubselect($subselect) |
||
| 198 | { |
||
| 199 | foreach ($this->walkers as $walker) { |
||
| 200 | $walker->walkSubselect($subselect); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function walkSubselectFromClause($subselectFromClause) |
||
| 208 | { |
||
| 209 | foreach ($this->walkers as $walker) { |
||
| 210 | $walker->walkSubselectFromClause($subselectFromClause); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | public function walkSimpleSelectClause($simpleSelectClause) |
||
| 218 | { |
||
| 219 | foreach ($this->walkers as $walker) { |
||
| 220 | $walker->walkSimpleSelectClause($simpleSelectClause); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function walkSimpleSelectExpression($simpleSelectExpression) |
||
| 228 | { |
||
| 229 | foreach ($this->walkers as $walker) { |
||
| 230 | $walker->walkSimpleSelectExpression($simpleSelectExpression); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | public function walkAggregateExpression($aggExpression) |
||
| 238 | { |
||
| 239 | foreach ($this->walkers as $walker) { |
||
| 240 | $walker->walkAggregateExpression($aggExpression); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function walkGroupByClause($groupByClause) |
||
| 248 | { |
||
| 249 | foreach ($this->walkers as $walker) { |
||
| 250 | $walker->walkGroupByClause($groupByClause); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function walkGroupByItem($groupByItem) |
||
| 258 | { |
||
| 259 | foreach ($this->walkers as $walker) { |
||
| 260 | $walker->walkGroupByItem($groupByItem); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function walkUpdateStatement(AST\UpdateStatement $AST) |
||
| 268 | { |
||
| 269 | foreach ($this->walkers as $walker) { |
||
| 270 | $walker->walkUpdateStatement($AST); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function walkDeleteStatement(AST\DeleteStatement $AST) |
||
| 278 | { |
||
| 279 | foreach ($this->walkers as $walker) { |
||
| 280 | $walker->walkDeleteStatement($AST); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function walkDeleteClause(AST\DeleteClause $deleteClause) |
||
| 288 | { |
||
| 289 | foreach ($this->walkers as $walker) { |
||
| 290 | $walker->walkDeleteClause($deleteClause); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function walkUpdateClause($updateClause) |
||
| 298 | { |
||
| 299 | foreach ($this->walkers as $walker) { |
||
| 300 | $walker->walkUpdateClause($updateClause); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function walkUpdateItem($updateItem) |
||
| 308 | { |
||
| 309 | foreach ($this->walkers as $walker) { |
||
| 310 | $walker->walkUpdateItem($updateItem); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritdoc} |
||
| 316 | */ |
||
| 317 | public function walkWhereClause($whereClause) |
||
| 318 | { |
||
| 319 | foreach ($this->walkers as $walker) { |
||
| 320 | $walker->walkWhereClause($whereClause); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | */ |
||
| 327 | public function walkConditionalExpression($condExpr) |
||
| 328 | { |
||
| 329 | foreach ($this->walkers as $walker) { |
||
| 330 | $walker->walkConditionalExpression($condExpr); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritdoc} |
||
| 336 | */ |
||
| 337 | public function walkConditionalTerm($condTerm) |
||
| 338 | { |
||
| 339 | foreach ($this->walkers as $walker) { |
||
| 340 | $walker->walkConditionalTerm($condTerm); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | */ |
||
| 347 | public function walkConditionalFactor($factor) |
||
| 348 | { |
||
| 349 | foreach ($this->walkers as $walker) { |
||
| 350 | $walker->walkConditionalFactor($factor); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritdoc} |
||
| 356 | */ |
||
| 357 | public function walkConditionalPrimary($condPrimary) |
||
| 358 | { |
||
| 359 | foreach ($this->walkers as $walker) { |
||
| 360 | $walker->walkConditionalPrimary($condPrimary); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | public function walkExistsExpression($existsExpr) |
||
| 368 | { |
||
| 369 | foreach ($this->walkers as $walker) { |
||
| 370 | $walker->walkExistsExpression($existsExpr); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * {@inheritdoc} |
||
| 376 | */ |
||
| 377 | public function walkCollectionMemberExpression($collMemberExpr) |
||
| 378 | { |
||
| 379 | foreach ($this->walkers as $walker) { |
||
| 380 | $walker->walkCollectionMemberExpression($collMemberExpr); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * {@inheritdoc} |
||
| 386 | */ |
||
| 387 | public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr) |
||
| 388 | { |
||
| 389 | foreach ($this->walkers as $walker) { |
||
| 390 | $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * {@inheritdoc} |
||
| 396 | */ |
||
| 397 | public function walkNullComparisonExpression($nullCompExpr) |
||
| 398 | { |
||
| 399 | foreach ($this->walkers as $walker) { |
||
| 400 | $walker->walkNullComparisonExpression($nullCompExpr); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | */ |
||
| 407 | public function walkInExpression($inExpr) |
||
| 408 | { |
||
| 409 | foreach ($this->walkers as $walker) { |
||
| 410 | $walker->walkInExpression($inExpr); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | */ |
||
| 417 | public function walkInstanceOfExpression($instanceOfExpr) |
||
| 418 | { |
||
| 419 | foreach ($this->walkers as $walker) { |
||
| 420 | $walker->walkInstanceOfExpression($instanceOfExpr); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * {@inheritdoc} |
||
| 426 | */ |
||
| 427 | public function walkLiteral($literal) |
||
| 428 | { |
||
| 429 | foreach ($this->walkers as $walker) { |
||
| 430 | $walker->walkLiteral($literal); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * {@inheritdoc} |
||
| 436 | */ |
||
| 437 | public function walkBetweenExpression($betweenExpr) |
||
| 438 | { |
||
| 439 | foreach ($this->walkers as $walker) { |
||
| 440 | $walker->walkBetweenExpression($betweenExpr); |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * {@inheritdoc} |
||
| 446 | */ |
||
| 447 | public function walkLikeExpression($likeExpr) |
||
| 448 | { |
||
| 449 | foreach ($this->walkers as $walker) { |
||
| 450 | $walker->walkLikeExpression($likeExpr); |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | */ |
||
| 457 | public function walkStateFieldPathExpression($stateFieldPathExpression) |
||
| 458 | { |
||
| 459 | foreach ($this->walkers as $walker) { |
||
| 460 | $walker->walkStateFieldPathExpression($stateFieldPathExpression); |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * {@inheritdoc} |
||
| 466 | */ |
||
| 467 | public function walkComparisonExpression($compExpr) |
||
| 468 | { |
||
| 469 | foreach ($this->walkers as $walker) { |
||
| 470 | $walker->walkComparisonExpression($compExpr); |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * {@inheritdoc} |
||
| 476 | */ |
||
| 477 | public function walkInputParameter($inputParam) |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritdoc} |
||
| 486 | */ |
||
| 487 | public function walkArithmeticExpression($arithmeticExpr) |
||
| 488 | { |
||
| 489 | foreach ($this->walkers as $walker) { |
||
| 490 | $walker->walkArithmeticExpression($arithmeticExpr); |
||
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * {@inheritdoc} |
||
| 496 | */ |
||
| 497 | public function walkArithmeticTerm($term) |
||
| 498 | { |
||
| 499 | foreach ($this->walkers as $walker) { |
||
| 500 | $walker->walkArithmeticTerm($term); |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritdoc} |
||
| 506 | */ |
||
| 507 | public function walkStringPrimary($stringPrimary) |
||
| 508 | { |
||
| 509 | foreach ($this->walkers as $walker) { |
||
| 510 | $walker->walkStringPrimary($stringPrimary); |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * {@inheritdoc} |
||
| 516 | */ |
||
| 517 | public function walkArithmeticFactor($factor) |
||
| 518 | { |
||
| 519 | foreach ($this->walkers as $walker) { |
||
| 520 | $walker->walkArithmeticFactor($factor); |
||
| 521 | } |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * {@inheritdoc} |
||
| 526 | */ |
||
| 527 | public function walkSimpleArithmeticExpression($simpleArithmeticExpr) |
||
| 528 | { |
||
| 529 | foreach ($this->walkers as $walker) { |
||
| 530 | $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr); |
||
| 531 | } |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * {@inheritdoc} |
||
| 536 | */ |
||
| 537 | public function walkPathExpression($pathExpr) |
||
| 541 | } |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * {@inheritdoc} |
||
| 546 | */ |
||
| 547 | public function walkResultVariable($resultVariable) |
||
| 548 | { |
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * {@inheritdoc} |
||
| 556 | */ |
||
| 557 | public function getExecutor($AST) |
||
| 558 | { |
||
| 559 | } |
||
| 560 | } |
||
| 561 |
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..