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 LimitSubqueryOutputWalker 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LimitSubqueryOutputWalker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class LimitSubqueryOutputWalker extends SqlWalker |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var \Doctrine\DBAL\Platforms\AbstractPlatform |
||
| 48 | */ |
||
| 49 | private $platform; |
||
|
|
|||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Doctrine\ORM\Query\ResultSetMapping |
||
| 53 | */ |
||
| 54 | private $rsm; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $queryComponents; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | private $firstResult; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $maxResults; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \Doctrine\ORM\EntityManager |
||
| 73 | */ |
||
| 74 | private $em; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The quote strategy. |
||
| 78 | * |
||
| 79 | * @var \Doctrine\ORM\Mapping\QuoteStrategy |
||
| 80 | */ |
||
| 81 | private $quoteStrategy; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $orderByPathExpressions = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool We don't want to add path expressions from sub-selects into the select clause of the containing query. |
||
| 90 | * This state flag simply keeps track on whether we are walking on a subquery or not |
||
| 91 | */ |
||
| 92 | private $inSubSelect = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Constructor. |
||
| 96 | * |
||
| 97 | * Stores various parameters that are otherwise unavailable |
||
| 98 | * because Doctrine\ORM\Query\SqlWalker keeps everything private without |
||
| 99 | * accessors. |
||
| 100 | * |
||
| 101 | * @param \Doctrine\ORM\Query $query |
||
| 102 | * @param \Doctrine\ORM\Query\ParserResult $parserResult |
||
| 103 | * @param array $queryComponents |
||
| 104 | */ |
||
| 105 | 65 | public function __construct($query, $parserResult, array $queryComponents) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Check if the platform supports the ROW_NUMBER window function. |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | 65 | private function platformSupportsRowNumber() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Rebuilds a select statement's order by clause for use in a |
||
| 140 | * ROW_NUMBER() OVER() expression. |
||
| 141 | * |
||
| 142 | * @param SelectStatement $AST |
||
| 143 | */ |
||
| 144 | 11 | private function rebuildOrderByForRowNumber(SelectStatement $AST) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT. |
||
| 169 | * |
||
| 170 | * @param SelectStatement $AST |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | * |
||
| 174 | * @throws \RuntimeException |
||
| 175 | */ |
||
| 176 | 65 | public function walkSelectStatement(SelectStatement $AST) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT. |
||
| 186 | * This method is for use with platforms which support ROW_NUMBER. |
||
| 187 | * |
||
| 188 | * @param SelectStatement $AST |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | * |
||
| 192 | * @throws \RuntimeException |
||
| 193 | */ |
||
| 194 | 13 | public function walkSelectStatementWithRowNumber(SelectStatement $AST) |
|
| 195 | { |
||
| 196 | 13 | $hasOrderBy = false; |
|
| 197 | 13 | $outerOrderBy = ' ORDER BY dctrn_minrownum ASC'; |
|
| 198 | 13 | $orderGroupBy = ''; |
|
| 199 | 13 | if ($AST->orderByClause instanceof OrderByClause) { |
|
| 200 | 11 | $hasOrderBy = true; |
|
| 201 | 11 | $this->rebuildOrderByForRowNumber($AST); |
|
| 202 | } |
||
| 203 | |||
| 204 | 13 | $innerSql = $this->getInnerSQL($AST); |
|
| 205 | |||
| 206 | 13 | $sqlIdentifier = $this->getSQLIdentifier($AST); |
|
| 207 | |||
| 208 | 13 | if ($hasOrderBy) { |
|
| 209 | 11 | $orderGroupBy = ' GROUP BY ' . implode(', ', $sqlIdentifier); |
|
| 210 | 11 | $sqlIdentifier[] = 'MIN(' . $this->walkResultVariable('dctrn_rownum') . ') AS dctrn_minrownum'; |
|
| 211 | } |
||
| 212 | |||
| 213 | // Build the counter query |
||
| 214 | 13 | $sql = sprintf( |
|
| 215 | 13 | 'SELECT DISTINCT %s FROM (%s) dctrn_result', |
|
| 216 | 13 | implode(', ', $sqlIdentifier), |
|
| 217 | 13 | $innerSql |
|
| 218 | ); |
||
| 219 | |||
| 220 | 13 | if ($hasOrderBy) { |
|
| 221 | 11 | $sql .= $orderGroupBy . $outerOrderBy; |
|
| 222 | } |
||
| 223 | |||
| 224 | // Apply the limit and offset. |
||
| 225 | 13 | $sql = $this->platform->modifyLimitQuery( |
|
| 226 | 13 | $sql, |
|
| 227 | 13 | $this->maxResults, |
|
| 228 | 13 | $this->firstResult |
|
| 229 | ); |
||
| 230 | |||
| 231 | // Add the columns to the ResultSetMapping. It's not really nice but |
||
| 232 | // it works. Preferably I'd clear the RSM or simply create a new one |
||
| 233 | // but that is not possible from inside the output walker, so we dirty |
||
| 234 | // up the one we have. |
||
| 235 | 13 | foreach ($sqlIdentifier as $property => $alias) { |
|
| 236 | 13 | $this->rsm->addScalarResult($alias, $property); |
|
| 237 | } |
||
| 238 | |||
| 239 | 13 | return $sql; |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT. |
||
| 244 | * This method is for platforms which DO NOT support ROW_NUMBER. |
||
| 245 | * |
||
| 246 | * @param SelectStatement $AST |
||
| 247 | * @param bool $addMissingItemsFromOrderByToSelect |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | * |
||
| 251 | * @throws \RuntimeException |
||
| 252 | */ |
||
| 253 | 52 | public function walkSelectStatementWithoutRowNumber(SelectStatement $AST, $addMissingItemsFromOrderByToSelect = true) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Finds all PathExpressions in an AST's OrderByClause, and ensures that |
||
| 300 | * the referenced fields are present in the SelectClause of the passed AST. |
||
| 301 | * |
||
| 302 | * @param SelectStatement $AST |
||
| 303 | */ |
||
| 304 | 44 | private function addMissingItemsFromOrderByToSelect(SelectStatement $AST) |
|
| 305 | { |
||
| 306 | 44 | $this->orderByPathExpressions = []; |
|
| 307 | |||
| 308 | // We need to do this in another walker because otherwise we'll end up |
||
| 309 | // polluting the state of this one. |
||
| 310 | 44 | $walker = clone $this; |
|
| 311 | |||
| 312 | // This will populate $orderByPathExpressions via |
||
| 313 | // LimitSubqueryOutputWalker::walkPathExpression, which will be called |
||
| 314 | // as the select statement is walked. We'll end up with an array of all |
||
| 315 | // path expressions referenced in the query. |
||
| 316 | 44 | $walker->walkSelectStatementWithoutRowNumber($AST, false); |
|
| 317 | 44 | $orderByPathExpressions = $walker->getOrderByPathExpressions(); |
|
| 318 | |||
| 319 | // Get a map of referenced identifiers to field names. |
||
| 320 | 44 | $selects = []; |
|
| 321 | 44 | foreach ($orderByPathExpressions as $pathExpression) { |
|
| 322 | 40 | $idVar = $pathExpression->identificationVariable; |
|
| 323 | 40 | $field = $pathExpression->field; |
|
| 324 | 40 | if (!isset($selects[$idVar])) { |
|
| 325 | 40 | $selects[$idVar] = []; |
|
| 326 | } |
||
| 327 | 40 | $selects[$idVar][$field] = true; |
|
| 328 | } |
||
| 329 | |||
| 330 | // Loop the select clause of the AST and exclude items from $select |
||
| 331 | // that are already being selected in the query. |
||
| 332 | 44 | foreach ($AST->selectClause->selectExpressions as $selectExpression) { |
|
| 333 | 44 | if ($selectExpression instanceof SelectExpression) { |
|
| 334 | 44 | $idVar = $selectExpression->expression; |
|
| 335 | 44 | if (!is_string($idVar)) { |
|
| 336 | 4 | continue; |
|
| 337 | } |
||
| 338 | 44 | $field = $selectExpression->fieldIdentificationVariable; |
|
| 339 | 44 | if ($field === null) { |
|
| 340 | // No need to add this select, as we're already fetching the whole object. |
||
| 341 | 44 | unset($selects[$idVar]); |
|
| 342 | } else { |
||
| 343 | unset($selects[$idVar][$field]); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | // Add select items which were not excluded to the AST's select clause. |
||
| 349 | 44 | foreach ($selects as $idVar => $fields) { |
|
| 350 | 9 | $AST->selectClause->selectExpressions[] = new SelectExpression(new PartialObjectExpression($idVar, array_keys($fields)), null, true); |
|
| 351 | } |
||
| 352 | 44 | } |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Generates new SQL for statements with an order by clause |
||
| 356 | * |
||
| 357 | * @param array $sqlIdentifier |
||
| 358 | * @param string $innerSql |
||
| 359 | * @param string $sql |
||
| 360 | * @param OrderByClause $orderByClause |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | 52 | private function preserveSqlOrdering(array $sqlIdentifier, $innerSql, $sql, $orderByClause) |
|
| 365 | { |
||
| 366 | // If the sql statement has an order by clause, we need to wrap it in a new select distinct |
||
| 367 | // statement |
||
| 368 | 52 | if (! $orderByClause instanceof OrderByClause) { |
|
| 369 | 8 | return $sql; |
|
| 370 | } |
||
| 371 | |||
| 372 | // Rebuild the order by clause to work in the scope of the new select statement |
||
| 373 | /* @var array $orderBy an array of rebuilt order by items */ |
||
| 374 | 44 | $orderBy = $this->rebuildOrderByClauseForOuterScope($orderByClause); |
|
| 375 | |||
| 376 | 44 | $innerSqlIdentifier = $sqlIdentifier; |
|
| 377 | |||
| 378 | 44 | foreach ($orderBy as $field) { |
|
| 379 | 44 | $field = preg_replace('/((\S+)\s+(ASC|DESC)\s*,?)*/', '${2}', $field); |
|
| 380 | |||
| 381 | // skip fields that are selected by identifiers, |
||
| 382 | // if those are ordered by in the query |
||
| 383 | 44 | if (in_array($field, $sqlIdentifier, true)) { |
|
| 384 | 6 | continue; |
|
| 385 | } |
||
| 386 | 39 | $innerSqlIdentifier[] = $field; |
|
| 387 | } |
||
| 388 | |||
| 389 | // Build the innner select statement |
||
| 390 | 44 | $sql = sprintf( |
|
| 391 | 44 | 'SELECT DISTINCT %s FROM (%s) dctrn_result_inner ORDER BY %s', |
|
| 392 | 44 | implode(', ', $innerSqlIdentifier), |
|
| 393 | 44 | $innerSql, |
|
| 394 | 44 | implode(', ', $orderBy) |
|
| 395 | ); |
||
| 396 | |||
| 397 | // now only select distinct identifier |
||
| 398 | 44 | $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', implode(', ', $sqlIdentifier), $sql); |
|
| 399 | |||
| 400 | 44 | return $sql; |
|
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Generates a new order by clause that works in the scope of a select query wrapping the original |
||
| 405 | * |
||
| 406 | * @param OrderByClause $orderByClause |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | 44 | private function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause) |
|
| 410 | { |
||
| 411 | $dqlAliasToSqlTableAliasMap |
||
| 412 | = $searchPatterns |
||
| 413 | = $replacements |
||
| 414 | = $dqlAliasToClassMap |
||
| 415 | = $selectListAdditions |
||
| 416 | = $orderByItems |
||
| 417 | 44 | = []; |
|
| 418 | |||
| 419 | // Generate DQL alias -> SQL table alias mapping |
||
| 420 | 44 | foreach(array_keys($this->rsm->aliasMap) as $dqlAlias) { |
|
| 421 | 44 | $dqlAliasToClassMap[$dqlAlias] = $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 422 | 44 | $dqlAliasToSqlTableAliasMap[$dqlAlias] = $this->getSQLTableAlias($class->getTableName(), $dqlAlias); |
|
| 423 | } |
||
| 424 | |||
| 425 | // Pattern to find table path expressions in the order by clause |
||
| 426 | 44 | $fieldSearchPattern = '/(?<![a-z0-9_])%s\.%s(?![a-z0-9_])/i'; |
|
| 427 | |||
| 428 | // Generate search patterns for each field's path expression in the order by clause |
||
| 429 | 44 | foreach($this->rsm->fieldMappings as $fieldAlias => $fieldName) { |
|
| 430 | 44 | $dqlAliasForFieldAlias = $this->rsm->columnOwnerMap[$fieldAlias]; |
|
| 431 | 44 | $class = $dqlAliasToClassMap[$dqlAliasForFieldAlias]; |
|
| 432 | |||
| 433 | // If the field is from a joined child table, we won't be ordering |
||
| 434 | // on it. |
||
| 435 | 44 | if (!isset($class->fieldMappings[$fieldName])) { |
|
| 436 | 1 | continue; |
|
| 437 | } |
||
| 438 | |||
| 439 | 44 | $fieldMapping = $class->fieldMappings[$fieldName]; |
|
| 440 | |||
| 441 | // Get the proper column name as will appear in the select list |
||
| 442 | 44 | $columnName = $this->quoteStrategy->getColumnName( |
|
| 443 | 44 | $fieldName, |
|
| 444 | 44 | $dqlAliasToClassMap[$dqlAliasForFieldAlias], |
|
| 445 | 44 | $this->em->getConnection()->getDatabasePlatform() |
|
| 446 | ); |
||
| 447 | |||
| 448 | // Get the SQL table alias for the entity and field |
||
| 449 | 44 | $sqlTableAliasForFieldAlias = $dqlAliasToSqlTableAliasMap[$dqlAliasForFieldAlias]; |
|
| 450 | 44 | if (isset($fieldMapping['declared']) && $fieldMapping['declared'] !== $class->name) { |
|
| 451 | // Field was declared in a parent class, so we need to get the proper SQL table alias |
||
| 452 | // for the joined parent table. |
||
| 453 | 2 | $otherClassMetadata = $this->em->getClassMetadata($fieldMapping['declared']); |
|
| 454 | 2 | if (!$otherClassMetadata->isMappedSuperclass) { |
|
| 455 | 1 | $sqlTableAliasForFieldAlias = $this->getSQLTableAlias($otherClassMetadata->getTableName(), $dqlAliasForFieldAlias); |
|
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | // Compose search/replace patterns |
||
| 460 | 44 | $searchPatterns[] = sprintf($fieldSearchPattern, $sqlTableAliasForFieldAlias, $columnName); |
|
| 461 | 44 | $replacements[] = $fieldAlias; |
|
| 462 | } |
||
| 463 | |||
| 464 | 44 | foreach($orderByClause->orderByItems as $orderByItem) { |
|
| 465 | // Walk order by item to get string representation of it |
||
| 466 | 44 | $orderByItemString = $this->walkOrderByItem($orderByItem); |
|
| 467 | |||
| 468 | // Replace path expressions in the order by clause with their column alias |
||
| 469 | 44 | $orderByItemString = preg_replace($searchPatterns, $replacements, $orderByItemString); |
|
| 470 | |||
| 471 | 44 | $orderByItems[] = $orderByItemString; |
|
| 472 | } |
||
| 473 | |||
| 474 | 44 | return $orderByItems; |
|
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * getter for $orderByPathExpressions |
||
| 479 | * |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | 44 | public function getOrderByPathExpressions() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @param SelectStatement $AST |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | * |
||
| 492 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 493 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 494 | */ |
||
| 495 | 65 | private function getInnerSQL(SelectStatement $AST) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * @param SelectStatement $AST |
||
| 519 | * |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | 65 | private function getSQLIdentifier(SelectStatement $AST) |
|
| 523 | { |
||
| 524 | // Find out the SQL alias of the identifier column of the root entity. |
||
| 525 | // It may be possible to make this work with multiple root entities but that |
||
| 526 | // would probably require issuing multiple queries or doing a UNION SELECT. |
||
| 527 | // So for now, it's not supported. |
||
| 528 | |||
| 529 | // Get the root entity and alias from the AST fromClause. |
||
| 530 | 65 | $from = $AST->fromClause->identificationVariableDeclarations; |
|
| 531 | 65 | if (count($from) !== 1) { |
|
| 532 | throw new \RuntimeException('Cannot count query which selects two FROM components, cannot make distinction'); |
||
| 533 | } |
||
| 534 | |||
| 535 | 65 | $fromRoot = reset($from); |
|
| 536 | 65 | $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; |
|
| 537 | 65 | $rootClass = $this->queryComponents[$rootAlias]['metadata']; |
|
| 538 | 65 | $rootIdentifier = $rootClass->identifier; |
|
| 539 | |||
| 540 | // For every identifier, find out the SQL alias by combing through the ResultSetMapping |
||
| 541 | 65 | $sqlIdentifier = []; |
|
| 542 | 65 | View Code Duplication | foreach ($rootIdentifier as $property) { |
| 543 | 65 | if (isset($rootClass->fieldMappings[$property])) { |
|
| 544 | 64 | foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) { |
|
| 545 | 64 | if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) { |
|
| 546 | 64 | $sqlIdentifier[$property] = $alias; |
|
| 547 | } |
||
| 548 | } |
||
| 549 | } |
||
| 550 | |||
| 551 | 65 | if (isset($rootClass->associationMappings[$property])) { |
|
| 552 | 1 | $joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name']; |
|
| 553 | |||
| 554 | 1 | foreach (array_keys($this->rsm->metaMappings, $joinColumn) as $alias) { |
|
| 555 | 1 | if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) { |
|
| 556 | 1 | $sqlIdentifier[$property] = $alias; |
|
| 557 | } |
||
| 558 | } |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | 65 | if (count($sqlIdentifier) === 0) { |
|
| 563 | throw new \RuntimeException('The Paginator does not support Queries which only yield ScalarResults.'); |
||
| 564 | } |
||
| 565 | |||
| 566 | 65 | View Code Duplication | if (count($rootIdentifier) != count($sqlIdentifier)) { |
| 567 | throw new \RuntimeException(sprintf( |
||
| 568 | 'Not all identifier properties can be found in the ResultSetMapping: %s', |
||
| 569 | implode(', ', array_diff($rootIdentifier, array_keys($sqlIdentifier))) |
||
| 570 | )); |
||
| 571 | } |
||
| 572 | |||
| 573 | 65 | return $sqlIdentifier; |
|
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * {@inheritdoc} |
||
| 578 | */ |
||
| 579 | 55 | public function walkPathExpression($pathExpr) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * {@inheritdoc} |
||
| 590 | */ |
||
| 591 | 7 | public function walkSubSelect($subselect) |
|
| 601 | } |
||
| 602 |