Complex classes like ModelQueryBuilder 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 ModelQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Adapters; |
||
| 38 | class ModelQueryBuilder extends QueryBuilder |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $modelClass; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $mainTableName; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $mainAlias; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Closure |
||
| 57 | */ |
||
| 58 | private $columnMapper; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var ModelSchemeInfoInterface |
||
| 62 | */ |
||
| 63 | private $modelSchemes; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | private $aliasIdCounter = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $knownAliases = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param Connection $connection |
||
| 77 | * @param string $modelClass |
||
| 78 | * @param ModelSchemeInfoInterface $modelSchemes |
||
| 79 | * |
||
| 80 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 81 | */ |
||
| 82 | 58 | public function __construct(Connection $connection, string $modelClass, ModelSchemeInfoInterface $modelSchemes) |
|
| 83 | { |
||
| 84 | 58 | assert(!empty($modelClass)); |
|
| 85 | |||
| 86 | 58 | parent::__construct($connection); |
|
| 87 | |||
| 88 | 58 | $this->modelSchemes = $modelSchemes; |
|
| 89 | 58 | $this->modelClass = $modelClass; |
|
| 90 | |||
| 91 | 58 | $this->mainTableName = $this->getModelSchemes()->getTable($this->getModelClass()); |
|
| 92 | 58 | $this->mainAlias = $this->createAlias($this->getMainTableName()); |
|
| 93 | |||
| 94 | 58 | $this->setColumnToDatabaseMapper(Closure::fromCallable([$this, 'getQuotedMainAliasColumn'])); |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | 58 | public function getModelClass(): string |
|
| 101 | { |
||
| 102 | 58 | return $this->modelClass; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Select all fields associated with model. |
||
| 107 | * |
||
| 108 | * @param iterable|null $columns |
||
| 109 | * |
||
| 110 | * @return self |
||
| 111 | */ |
||
| 112 | 52 | public function selectModelColumns(iterable $columns = null): self |
|
| 113 | { |
||
| 114 | $selectedColumns = |
||
| 115 | 52 | $columns === null ? $this->getModelSchemes()->getAttributes($this->getModelClass()) : $columns; |
|
| 116 | |||
| 117 | 52 | $quotedColumns = []; |
|
| 118 | 52 | $columnMapper = $this->getColumnToDatabaseMapper(); |
|
| 119 | 52 | foreach ($selectedColumns as $column) { |
|
| 120 | 52 | $quotedColumns[] = call_user_func($columnMapper, $column, $this); |
|
| 121 | } |
||
| 122 | |||
| 123 | 52 | $this->select($quotedColumns); |
|
| 124 | |||
| 125 | 52 | return $this; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param Closure $columnMapper |
||
| 130 | * |
||
| 131 | * @return self |
||
| 132 | */ |
||
| 133 | 58 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 134 | { |
||
| 135 | 58 | $this->columnMapper = $columnMapper; |
|
| 136 | |||
| 137 | 58 | return $this; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return self |
||
| 142 | */ |
||
| 143 | 53 | public function fromModelTable(): self |
|
| 144 | { |
||
| 145 | 53 | $this->from( |
|
| 146 | 53 | $this->quoteTableName($this->getMainTableName()), |
|
| 147 | 53 | $this->quoteTableName($this->getMainAlias()) |
|
| 148 | ); |
||
| 149 | |||
| 150 | 53 | return $this; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param iterable $attributes |
||
| 155 | * |
||
| 156 | * @return self |
||
| 157 | * |
||
| 158 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 159 | */ |
||
| 160 | 4 | public function createModel(iterable $attributes): self |
|
| 161 | { |
||
| 162 | 4 | $this->insert($this->quoteTableName($this->getMainTableName())); |
|
| 163 | |||
| 164 | 4 | $valuesAsParams = []; |
|
| 165 | 4 | foreach ($this->bindAttributes($this->getModelClass(), $attributes) as $quotedColumn => $parameterName) { |
|
| 166 | 4 | $valuesAsParams[$quotedColumn] = $parameterName; |
|
| 167 | } |
||
| 168 | 4 | $this->values($valuesAsParams); |
|
| 169 | |||
| 170 | 4 | return $this; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param iterable $attributes |
||
| 175 | * |
||
| 176 | * @return self |
||
| 177 | * |
||
| 178 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 179 | */ |
||
| 180 | 5 | public function updateModels(iterable $attributes): self |
|
| 181 | { |
||
| 182 | 5 | $this->update($this->quoteTableName($this->getMainTableName())); |
|
| 183 | |||
| 184 | 5 | foreach ($this->bindAttributes($this->getModelClass(), $attributes) as $quotedColumn => $parameterName) { |
|
| 185 | 5 | $this->set($quotedColumn, $parameterName); |
|
| 186 | } |
||
| 187 | |||
| 188 | 5 | return $this; |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $modelClass |
||
| 193 | * @param iterable $attributes |
||
| 194 | * |
||
| 195 | * @return iterable |
||
|
|
|||
| 196 | * |
||
| 197 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 198 | */ |
||
| 199 | 9 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 200 | { |
||
| 201 | 9 | $dbPlatform = $this->getConnection()->getDatabasePlatform(); |
|
| 202 | 9 | $types = $this->getModelSchemes()->getAttributeTypes($modelClass); |
|
| 203 | |||
| 204 | 9 | foreach ($attributes as $column => $value) { |
|
| 205 | 9 | assert(is_string($column) && $this->getModelSchemes()->hasAttributeType($this->getModelClass(), $column)); |
|
| 206 | |||
| 207 | 9 | $quotedColumn = $this->quoteColumnName($column); |
|
| 208 | |||
| 209 | 9 | $type = Type::getType($types[$column]); |
|
| 210 | 9 | $pdoValue = $type->convertToDatabaseValue($value, $dbPlatform); |
|
| 211 | 9 | $parameterName = $this->createNamedParameter($pdoValue, $type->getBindingType()); |
|
| 212 | |||
| 213 | 9 | yield $quotedColumn => $parameterName; |
|
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return self |
||
| 219 | */ |
||
| 220 | 5 | public function deleteModels(): self |
|
| 221 | { |
||
| 222 | 5 | $this->delete($this->quoteTableName($this->getMainTableName())); |
|
| 223 | |||
| 224 | 5 | return $this; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @inheritdoc |
||
| 229 | */ |
||
| 230 | 4 | public function prepareCreateInToManyRelationship( |
|
| 231 | string $relationshipName, |
||
| 232 | string $identity, |
||
| 233 | string $secondaryIdBindName |
||
| 234 | ): self { |
||
| 235 | list ($intermediateTable, $primaryKey, $secondaryKey) = |
||
| 236 | 4 | $this->getModelSchemes()->getBelongsToManyRelationship($this->getModelClass(), $relationshipName); |
|
| 237 | |||
| 238 | $this |
||
| 239 | 4 | ->insert($this->quoteTableName($intermediateTable)) |
|
| 240 | 4 | ->values([ |
|
| 241 | 4 | $this->quoteColumnName($primaryKey) => $this->createNamedParameter($identity), |
|
| 242 | 4 | $this->quoteColumnName($secondaryKey) => $secondaryIdBindName, |
|
| 243 | ]); |
||
| 244 | |||
| 245 | 4 | return $this; |
|
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @inheritdoc |
||
| 250 | */ |
||
| 251 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 252 | { |
||
| 253 | list ($intermediateTable, $primaryKey) = |
||
| 254 | 2 | $this->getModelSchemes()->getBelongsToManyRelationship($this->getModelClass(), $relationshipName); |
|
| 255 | |||
| 256 | 2 | $filters = [$primaryKey => [FilterParameterInterface::OPERATION_EQUALS => [$identity]]]; |
|
| 257 | $this |
||
| 258 | 2 | ->delete($this->quoteTableName($intermediateTable)) |
|
| 259 | 2 | ->addFilters($intermediateTable, $this->expr()->andX(), $filters); |
|
| 260 | |||
| 261 | 2 | return $this; |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param iterable $filters |
||
| 266 | * |
||
| 267 | * @return self |
||
| 268 | */ |
||
| 269 | 9 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 270 | { |
||
| 271 | 9 | return $this->addFilters($this->getMainTableName(), $this->expr()->andX(), $filters); |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param iterable $filters |
||
| 276 | * |
||
| 277 | * @return self |
||
| 278 | */ |
||
| 279 | 1 | public function addFiltersWithOrToTable(iterable $filters): self |
|
| 280 | { |
||
| 281 | 1 | return $this->addFilters($this->getMainTableName(), $this->expr()->orX(), $filters); |
|
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param iterable $filters |
||
| 286 | * |
||
| 287 | * @return self |
||
| 288 | */ |
||
| 289 | 38 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 290 | { |
||
| 291 | 38 | return $this->addFilters($this->getMainAlias(), $this->expr()->andX(), $filters); |
|
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param iterable $filters |
||
| 296 | * |
||
| 297 | * @return self |
||
| 298 | */ |
||
| 299 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 300 | { |
||
| 301 | 2 | return $this->addFilters($this->getMainAlias(), $this->expr()->orX(), $filters); |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $relationshipName |
||
| 306 | * @param iterable $relationshipFilters |
||
| 307 | * @param iterable|null $relationshipSorts |
||
| 308 | * |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | 26 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 312 | string $relationshipName, |
||
| 313 | iterable $relationshipFilters, |
||
| 314 | ?iterable $relationshipSorts |
||
| 315 | ): self { |
||
| 316 | 26 | $joinWith = $this->expr()->andX(); |
|
| 317 | |||
| 318 | 26 | return $this->addRelationshipFiltersAndSorts( |
|
| 319 | 26 | $relationshipName, |
|
| 320 | 26 | $joinWith, |
|
| 321 | 26 | $relationshipFilters, |
|
| 322 | 26 | $relationshipSorts |
|
| 323 | ); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | 14 | public function distinct(): self |
|
| 330 | { |
||
| 331 | // emulate SELECT DISTINCT with grouping by primary key |
||
| 332 | 14 | $primaryColumn = $this->getModelSchemes()->getPrimaryKey($this->getModelClass()); |
|
| 333 | 14 | $this->addGroupBy($this->getQuotedMainAliasColumn($primaryColumn)); |
|
| 334 | |||
| 335 | 14 | return $this; |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param string $relationshipName |
||
| 340 | * @param iterable $relationshipFilters |
||
| 341 | * @param iterable|null $relationshipSorts |
||
| 342 | * |
||
| 343 | * @return self |
||
| 344 | */ |
||
| 345 | 2 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 346 | string $relationshipName, |
||
| 347 | iterable $relationshipFilters, |
||
| 348 | ?iterable $relationshipSorts |
||
| 349 | ): self { |
||
| 350 | 2 | $joinWith = $this->expr()->orX(); |
|
| 351 | |||
| 352 | 2 | return $this->addRelationshipFiltersAndSorts( |
|
| 353 | 2 | $relationshipName, |
|
| 354 | 2 | $joinWith, |
|
| 355 | 2 | $relationshipFilters, |
|
| 356 | 2 | $relationshipSorts |
|
| 357 | ); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param iterable $sortParameters |
||
| 362 | * |
||
| 363 | * @return self |
||
| 364 | */ |
||
| 365 | 8 | public function addSorts(iterable $sortParameters): self |
|
| 366 | { |
||
| 367 | 8 | foreach ($sortParameters as $columnName => $isAsc) { |
|
| 368 | 8 | assert(is_string($columnName) === true && is_bool($isAsc) === true); |
|
| 369 | 8 | $fullColumnName = $this->getQuotedMainAliasColumn($columnName); |
|
| 370 | 8 | assert($this->getModelSchemes()->hasAttributeType($this->getModelClass(), $columnName)); |
|
| 371 | 8 | $this->addOrderBy($fullColumnName, $isAsc === true ? 'ASC' : 'DESC'); |
|
| 372 | } |
||
| 373 | |||
| 374 | 8 | return $this; |
|
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $tableOrAlias |
||
| 379 | * @param CompositeExpression $filterLink |
||
| 380 | * @param iterable $filters |
||
| 381 | * |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | 43 | private function addFilters(string $tableOrAlias, CompositeExpression $filterLink, iterable $filters): self |
|
| 385 | { |
||
| 386 | 43 | foreach ($filters as $columnName => $operationsWithArgs) { |
|
| 387 | 43 | $fullColumnName = $this->buildColumnName($tableOrAlias, $columnName); |
|
| 388 | 43 | $this->applyFilter($filterLink, $fullColumnName, $operationsWithArgs); |
|
| 389 | } |
||
| 390 | 42 | if ($filterLink->count() > 0) { |
|
| 391 | 42 | $this->andWhere($filterLink); |
|
| 392 | } |
||
| 393 | |||
| 394 | 42 | return $this; |
|
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $relationshipName |
||
| 399 | * @param CompositeExpression $filterLink |
||
| 400 | * @param iterable $relationshipFilters |
||
| 401 | * @param iterable|null $relationshipSorts |
||
| 402 | * |
||
| 403 | * @return self |
||
| 404 | */ |
||
| 405 | 28 | private function addRelationshipFiltersAndSorts( |
|
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | 58 | private function getMainTableName(): string |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @return ModelSchemeInfoInterface |
||
| 456 | */ |
||
| 457 | 58 | private function getModelSchemes(): ModelSchemeInfoInterface |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | 54 | private function getMainAlias(): string |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $relationshipName |
||
| 472 | * @param CompositeExpression $filterLink |
||
| 473 | * @param iterable $relationshipFilters |
||
| 474 | * @param iterable|null $relationshipSorts |
||
| 475 | * |
||
| 476 | * @return self |
||
| 477 | */ |
||
| 478 | 17 | private function addBelongsToFiltersAndSorts( |
|
| 479 | string $relationshipName, |
||
| 480 | CompositeExpression $filterLink, |
||
| 481 | iterable $relationshipFilters, |
||
| 482 | ?iterable $relationshipSorts |
||
| 483 | ): self { |
||
| 484 | 17 | $foreignKey = $this->getModelSchemes()->getForeignKey($this->getModelClass(), $relationshipName); |
|
| 485 | list($onePrimaryKey, $oneTable) = |
||
| 486 | 17 | $this->getModelSchemes()->getReversePrimaryKey($this->getModelClass(), $relationshipName); |
|
| 487 | |||
| 488 | 17 | $this->innerJoinOneTable( |
|
| 489 | 17 | $this->getMainAlias(), |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $relationshipName |
||
| 506 | * @param CompositeExpression $filterLink |
||
| 507 | * @param iterable $relationshipFilters |
||
| 508 | * @param iterable|null $relationshipSorts |
||
| 509 | * |
||
| 510 | * @return self |
||
| 511 | */ |
||
| 512 | 12 | private function addHasManyFiltersAndSorts( |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $relationshipName |
||
| 540 | * @param CompositeExpression $targetFilterLink |
||
| 541 | * @param iterable $relationshipFilters |
||
| 542 | * @param iterable|null $relationshipSorts |
||
| 543 | * |
||
| 544 | * @return self |
||
| 545 | */ |
||
| 546 | 9 | private function addBelongsToManyFiltersAndSorts( |
|
| 581 | |||
| 582 | /** |
||
| 583 | * @param string $fromAlias |
||
| 584 | * @param string $fromColumn |
||
| 585 | * @param string $targetTable |
||
| 586 | * @param string $targetColumn |
||
| 587 | * @param CompositeExpression|null $targetFilterLink |
||
| 588 | * @param iterable|null $targetFilterParams |
||
| 589 | * @param iterable|null $relationshipSorts |
||
| 590 | * |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | 28 | private function innerJoinOneTable( |
|
| 630 | |||
| 631 | /** @noinspection PhpTooManyParametersInspection |
||
| 632 | * @param string $fromAlias |
||
| 633 | * @param string $fromColumn |
||
| 634 | * @param string $intTable |
||
| 635 | * @param string $intToFromColumn |
||
| 636 | * @param string $intToTargetColumn |
||
| 637 | * @param string $targetTable |
||
| 638 | * @param string $targetColumn |
||
| 639 | * @param CompositeExpression|null $intFilterLink |
||
| 640 | * @param iterable|null $intFilterParams |
||
| 641 | * @param CompositeExpression|null $targetFilterLink |
||
| 642 | * @param iterable|null $targetFilterParams |
||
| 643 | * @param iterable|null $targetSortParams |
||
| 644 | * |
||
| 645 | * @return string |
||
| 646 | * |
||
| 647 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 648 | */ |
||
| 649 | 9 | private function innerJoinTwoSequentialTables( |
|
| 685 | |||
| 686 | /** |
||
| 687 | * @param string $tableName |
||
| 688 | * |
||
| 689 | * @return string |
||
| 690 | */ |
||
| 691 | 58 | private function createAlias(string $tableName): string |
|
| 698 | |||
| 699 | /** |
||
| 700 | * @inheritdoc |
||
| 701 | */ |
||
| 702 | 57 | private function quoteTableName(string $tableName): string |
|
| 706 | |||
| 707 | /** |
||
| 708 | * @inheritdoc |
||
| 709 | */ |
||
| 710 | 9 | private function quoteColumnName(string $columnName): string |
|
| 714 | |||
| 715 | /** |
||
| 716 | * @inheritdoc |
||
| 717 | */ |
||
| 718 | 58 | private function buildColumnName(string $table, string $column): string |
|
| 722 | |||
| 723 | /** |
||
| 724 | * @param string $column |
||
| 725 | * |
||
| 726 | * @return string |
||
| 727 | */ |
||
| 728 | 1 | public function getQuotedMainTableColumn(string $column): string |
|
| 732 | |||
| 733 | /** |
||
| 734 | * @param string $column |
||
| 735 | * |
||
| 736 | * @return string |
||
| 737 | */ |
||
| 738 | 53 | public function getQuotedMainAliasColumn(string $column): string |
|
| 742 | |||
| 743 | /** |
||
| 744 | * @param CompositeExpression $filterLink |
||
| 745 | * @param string $fullColumnName |
||
| 746 | * @param iterable $operationsWithArgs |
||
| 747 | * |
||
| 748 | * @return void |
||
| 749 | * |
||
| 750 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 751 | */ |
||
| 752 | 54 | private function applyFilter( |
|
| 807 | |||
| 808 | /** |
||
| 809 | * @param iterable $arguments |
||
| 810 | * |
||
| 811 | * @return string |
||
| 812 | */ |
||
| 813 | 52 | private function createSingleNamedParameter(iterable $arguments): string |
|
| 824 | |||
| 825 | /** |
||
| 826 | * @param iterable $arguments |
||
| 827 | * |
||
| 828 | * @return string[] |
||
| 829 | */ |
||
| 830 | 6 | private function createNamedParameterArray(iterable $arguments): array |
|
| 840 | |||
| 841 | /** |
||
| 842 | * @return Closure |
||
| 843 | */ |
||
| 844 | 52 | private function getColumnToDatabaseMapper(): Closure |
|
| 848 | |||
| 849 | /** |
||
| 850 | * @param mixed $value |
||
| 851 | * |
||
| 852 | * @return int |
||
| 853 | * |
||
| 854 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 855 | */ |
||
| 856 | 53 | private function getPdoType($value): int |
|
| 874 | } |
||
| 875 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.