Complex classes like Crud 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 Crud, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Api; |
||
| 59 | class Crud implements CrudInterface |
||
| 60 | { |
||
| 61 | use HasContainerTrait; |
||
| 62 | |||
| 63 | /** Internal constant. Path constant. */ |
||
| 64 | protected const ROOT_PATH = ''; |
||
| 65 | |||
| 66 | /** Internal constant. Path constant. */ |
||
| 67 | protected const PATH_SEPARATOR = DocumentInterface::PATH_SEPARATOR; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var FactoryInterface |
||
| 71 | */ |
||
| 72 | private $factory; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $modelClass; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var ModelSchemaInfoInterface |
||
| 81 | */ |
||
| 82 | private $modelSchemas; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var RelationshipPaginationStrategyInterface |
||
| 86 | */ |
||
| 87 | private $relPagingStrategy; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var Connection |
||
| 91 | */ |
||
| 92 | private $connection; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var iterable|null |
||
| 96 | */ |
||
| 97 | private $filterParameters = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | private $areFiltersWithAnd = true; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var iterable|null |
||
| 106 | */ |
||
| 107 | private $sortingParameters = null; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private $relFiltersAndSorts = []; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var iterable|null |
||
| 116 | */ |
||
| 117 | private $includePaths = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var int|null |
||
| 121 | */ |
||
| 122 | private $pagingOffset = null; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var Closure|null |
||
| 126 | */ |
||
| 127 | private $columnMapper = null; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | private $isFetchTyped; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var int|null |
||
| 136 | */ |
||
| 137 | private $pagingLimit = null; |
||
| 138 | |||
| 139 | /** internal constant */ |
||
| 140 | private const REL_FILTERS_AND_SORTS__FILTERS = 0; |
||
| 141 | |||
| 142 | /** internal constant */ |
||
| 143 | private const REL_FILTERS_AND_SORTS__SORTS = 1; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param ContainerInterface $container |
||
| 147 | * @param string $modelClass |
||
| 148 | * |
||
| 149 | * @throws ContainerExceptionInterface |
||
| 150 | * @throws NotFoundExceptionInterface |
||
| 151 | */ |
||
| 152 | 38 | public function __construct(ContainerInterface $container, string $modelClass) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param Closure $mapper |
||
| 167 | * |
||
| 168 | * @return self |
||
| 169 | */ |
||
| 170 | 1 | public function withColumnMapper(Closure $mapper): self |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritdoc |
||
| 179 | */ |
||
| 180 | 25 | public function withFilters(iterable $filterParameters): CrudInterface |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | */ |
||
| 190 | 16 | public function withIndexFilter($index): CrudInterface |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @inheritdoc |
||
| 208 | */ |
||
| 209 | 3 | public function withIndexesFilter(array $indexes): CrudInterface |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @inheritdoc |
||
| 237 | */ |
||
| 238 | 2 | public function withRelationshipFilters(string $name, iterable $filters): CrudInterface |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @inheritdoc |
||
| 249 | */ |
||
| 250 | public function withRelationshipSorts(string $name, iterable $sorts): CrudInterface |
||
| 251 | { |
||
| 252 | assert($this->getModelSchemas()->hasRelationship($this->getModelClass(), $name) === true); |
||
| 253 | |||
| 254 | $this->relFiltersAndSorts[$name][self::REL_FILTERS_AND_SORTS__SORTS] = $sorts; |
||
| 255 | |||
| 256 | return $this; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @inheritdoc |
||
| 261 | */ |
||
| 262 | 2 | public function combineWithAnd(): CrudInterface |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @inheritdoc |
||
| 271 | */ |
||
| 272 | 1 | public function combineWithOr(): CrudInterface |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 24 | private function hasColumnMapper(): bool |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return Closure |
||
| 289 | */ |
||
| 290 | 1 | private function getColumnMapper(): Closure |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | 29 | private function hasFilters(): bool |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @return iterable |
||
| 305 | */ |
||
| 306 | 25 | private function getFilters(): iterable |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 25 | private function areFiltersWithAnd(): bool |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @inheritdoc |
||
| 321 | */ |
||
| 322 | 3 | public function withSorts(iterable $sortingParameters): CrudInterface |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | 24 | private function hasSorts(): bool |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @return iterable |
||
| 339 | */ |
||
| 340 | 7 | private function getSorts(): ?iterable |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @inheritdoc |
||
| 347 | */ |
||
| 348 | 7 | public function withIncludes(iterable $includePaths): CrudInterface |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | 19 | private function hasIncludes(): bool |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @return iterable |
||
| 365 | */ |
||
| 366 | 7 | private function getIncludes(): iterable |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @inheritdoc |
||
| 373 | */ |
||
| 374 | 5 | public function withPaging(int $offset, int $limit): CrudInterface |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @inheritdoc |
||
| 384 | */ |
||
| 385 | 1 | public function withoutPaging(): CrudInterface |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @return self |
||
| 395 | */ |
||
| 396 | 38 | public function shouldBeTyped(): self |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @return self |
||
| 405 | */ |
||
| 406 | 4 | public function shouldBeUntyped(): self |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | 28 | private function hasPaging(): bool |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @return int |
||
| 423 | */ |
||
| 424 | 4 | private function getPagingOffset(): int |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @return int |
||
| 431 | */ |
||
| 432 | 4 | private function getPagingLimit(): int |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | 26 | private function isFetchTyped(): bool |
|
| 444 | |||
| 445 | /** |
||
| 446 | * @return Connection |
||
| 447 | */ |
||
| 448 | 29 | protected function getConnection(): Connection |
|
| 452 | |||
| 453 | /** |
||
| 454 | * @param string $modelClass |
||
| 455 | * |
||
| 456 | * @return ModelQueryBuilder |
||
| 457 | */ |
||
| 458 | 29 | protected function createBuilder(string $modelClass): ModelQueryBuilder |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @param Connection $connection |
||
| 465 | * @param string $modelClass |
||
| 466 | * |
||
| 467 | * @return ModelQueryBuilder |
||
| 468 | */ |
||
| 469 | 29 | private function createBuilderFromConnection(Connection $connection, string $modelClass): ModelQueryBuilder |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @param ModelQueryBuilder $builder |
||
| 476 | * |
||
| 477 | * @return Crud |
||
| 478 | */ |
||
| 479 | 24 | protected function applyColumnMapper(ModelQueryBuilder $builder): self |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @param ModelQueryBuilder $builder |
||
| 490 | * |
||
| 491 | * @return Crud |
||
| 492 | * |
||
| 493 | * @throws DBALException |
||
| 494 | */ |
||
| 495 | 24 | protected function applyAliasFilters(ModelQueryBuilder $builder): self |
|
| 505 | |||
| 506 | /** |
||
| 507 | * @param ModelQueryBuilder $builder |
||
| 508 | * |
||
| 509 | * @return self |
||
| 510 | * |
||
| 511 | * @throws DBALException |
||
| 512 | */ |
||
| 513 | 3 | protected function applyTableFilters(ModelQueryBuilder $builder): self |
|
| 523 | |||
| 524 | /** |
||
| 525 | * @param ModelQueryBuilder $builder |
||
| 526 | * |
||
| 527 | * @return self |
||
| 528 | * |
||
| 529 | * @throws DBALException |
||
| 530 | */ |
||
| 531 | 24 | protected function applyRelationshipFiltersAndSorts(ModelQueryBuilder $builder): self |
|
| 552 | |||
| 553 | /** |
||
| 554 | * @param ModelQueryBuilder $builder |
||
| 555 | * |
||
| 556 | * @return self |
||
| 557 | */ |
||
| 558 | 24 | protected function applySorts(ModelQueryBuilder $builder): self |
|
| 566 | |||
| 567 | /** |
||
| 568 | * @param ModelQueryBuilder $builder |
||
| 569 | * |
||
| 570 | * @return self |
||
| 571 | */ |
||
| 572 | 28 | protected function applyPaging(ModelQueryBuilder $builder): self |
|
| 581 | |||
| 582 | /** |
||
| 583 | * @return self |
||
| 584 | */ |
||
| 585 | 38 | protected function clearBuilderParameters(): self |
|
| 597 | |||
| 598 | /** |
||
| 599 | * @return self |
||
| 600 | */ |
||
| 601 | 38 | private function clearFetchParameters(): self |
|
| 608 | |||
| 609 | /** |
||
| 610 | * @param ModelQueryBuilder $builder |
||
| 611 | * |
||
| 612 | * @return ModelQueryBuilder |
||
| 613 | */ |
||
| 614 | 2 | protected function builderOnCount(ModelQueryBuilder $builder): ModelQueryBuilder |
|
| 618 | |||
| 619 | /** |
||
| 620 | * @param ModelQueryBuilder $builder |
||
| 621 | * |
||
| 622 | * @return ModelQueryBuilder |
||
| 623 | */ |
||
| 624 | 24 | protected function builderOnIndex(ModelQueryBuilder $builder): ModelQueryBuilder |
|
| 628 | |||
| 629 | /** |
||
| 630 | * @param ModelQueryBuilder $builder |
||
| 631 | * |
||
| 632 | * @return ModelQueryBuilder |
||
| 633 | */ |
||
| 634 | 4 | protected function builderOnReadRelationship(ModelQueryBuilder $builder): ModelQueryBuilder |
|
| 638 | |||
| 639 | /** |
||
| 640 | * @param ModelQueryBuilder $builder |
||
| 641 | * |
||
| 642 | * @return ModelQueryBuilder |
||
| 643 | */ |
||
| 644 | 3 | protected function builderSaveResourceOnCreate(ModelQueryBuilder $builder): ModelQueryBuilder |
|
| 648 | |||
| 649 | /** |
||
| 650 | * @param ModelQueryBuilder $builder |
||
| 651 | * |
||
| 652 | * @return ModelQueryBuilder |
||
| 653 | */ |
||
| 654 | 1 | protected function builderSaveResourceOnUpdate(ModelQueryBuilder $builder): ModelQueryBuilder |
|
| 658 | |||
| 659 | /** |
||
| 660 | * @param string $relationshipName |
||
| 661 | * @param ModelQueryBuilder $builder |
||
| 662 | * |
||
| 663 | * @return ModelQueryBuilder |
||
| 664 | * |
||
| 665 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 666 | */ |
||
| 667 | 1 | protected function builderSaveRelationshipOnCreate(/** @noinspection PhpUnusedParameterInspection */ |
|
| 673 | |||
| 674 | /** |
||
| 675 | * @param string $relationshipName |
||
| 676 | * @param ModelQueryBuilder $builder |
||
| 677 | * |
||
| 678 | * @return ModelQueryBuilder |
||
| 679 | * |
||
| 680 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 681 | */ |
||
| 682 | 1 | protected function builderSaveRelationshipOnUpdate(/** @noinspection PhpUnusedParameterInspection */ |
|
| 688 | |||
| 689 | /** |
||
| 690 | * @param string $relationshipName |
||
| 691 | * @param ModelQueryBuilder $builder |
||
| 692 | * |
||
| 693 | * @return ModelQueryBuilder |
||
| 694 | * |
||
| 695 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 696 | */ |
||
| 697 | protected function builderOnCreateInBelongsToManyRelationship(/** @noinspection PhpUnusedParameterInspection */ |
||
| 698 | $relationshipName, |
||
| 699 | ModelQueryBuilder $builder |
||
| 700 | ): ModelQueryBuilder { |
||
| 701 | return $builder; |
||
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param string $relationshipName |
||
| 706 | * @param ModelQueryBuilder $builder |
||
| 707 | * |
||
| 708 | * @return ModelQueryBuilder |
||
| 709 | * |
||
| 710 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 711 | */ |
||
| 712 | protected function builderOnRemoveInBelongsToManyRelationship(/** @noinspection PhpUnusedParameterInspection */ |
||
| 713 | $relationshipName, |
||
| 714 | ModelQueryBuilder $builder |
||
| 715 | ): ModelQueryBuilder { |
||
| 716 | return $builder; |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @param string $relationshipName |
||
| 721 | * @param ModelQueryBuilder $builder |
||
| 722 | * |
||
| 723 | * @return ModelQueryBuilder |
||
| 724 | * |
||
| 725 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 726 | */ |
||
| 727 | 1 | protected function builderCleanRelationshipOnUpdate(/** @noinspection PhpUnusedParameterInspection */ |
|
| 733 | |||
| 734 | /** |
||
| 735 | * @param ModelQueryBuilder $builder |
||
| 736 | * |
||
| 737 | * @return ModelQueryBuilder |
||
| 738 | */ |
||
| 739 | 3 | protected function builderOnDelete(ModelQueryBuilder $builder): ModelQueryBuilder |
|
| 743 | |||
| 744 | /** |
||
| 745 | * @param PaginatedDataInterface|mixed|null $data |
||
| 746 | * |
||
| 747 | * @return void |
||
| 748 | * |
||
| 749 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 750 | * |
||
| 751 | * @throws DBALException |
||
| 752 | */ |
||
| 753 | 7 | private function loadRelationships($data): void |
|
| 802 | |||
| 803 | /** |
||
| 804 | * A helper to remember all model related data. Helps to ensure we consistently handle models in CRUD. |
||
| 805 | * |
||
| 806 | * @param mixed $model |
||
| 807 | * @param string $path |
||
| 808 | * @param ModelSchemaInfoInterface $modelSchemas |
||
| 809 | * @param ModelStorageInterface $modelStorage |
||
| 810 | * @param TagStorageInterface $modelsAtPath |
||
| 811 | * @param ArrayObject $idsAtPath |
||
| 812 | * |
||
| 813 | * @return mixed |
||
| 814 | */ |
||
| 815 | 7 | private static function registerModelAtPath( |
|
| 833 | |||
| 834 | /** |
||
| 835 | * @param iterable $paths (string[]) |
||
| 836 | * |
||
| 837 | * @return iterable |
||
| 838 | */ |
||
| 839 | 7 | private static function getPaths(iterable $paths): iterable |
|
| 876 | |||
| 877 | /** |
||
| 878 | * @inheritdoc |
||
| 879 | * |
||
| 880 | * @throws DBALException |
||
| 881 | */ |
||
| 882 | 3 | public function createIndexBuilder(iterable $columns = null): QueryBuilder |
|
| 886 | |||
| 887 | /** |
||
| 888 | * @inheritdoc |
||
| 889 | * |
||
| 890 | * @throws DBALException |
||
| 891 | */ |
||
| 892 | 3 | public function createDeleteBuilder(): QueryBuilder |
|
| 896 | |||
| 897 | /** |
||
| 898 | * @param iterable|null $columns |
||
| 899 | * |
||
| 900 | * @return ModelQueryBuilder |
||
| 901 | * |
||
| 902 | * @throws DBALException |
||
| 903 | */ |
||
| 904 | 24 | protected function createIndexModelBuilder(iterable $columns = null): ModelQueryBuilder |
|
| 927 | |||
| 928 | /** |
||
| 929 | * @return ModelQueryBuilder |
||
| 930 | * |
||
| 931 | * @throws DBALException |
||
| 932 | */ |
||
| 933 | 3 | protected function createDeleteModelBuilder(): ModelQueryBuilder |
|
| 947 | |||
| 948 | /** |
||
| 949 | * @inheritdoc |
||
| 950 | * |
||
| 951 | * @throws DBALException |
||
| 952 | */ |
||
| 953 | 7 | public function index(): PaginatedDataInterface |
|
| 960 | |||
| 961 | /** |
||
| 962 | * @inheritdoc |
||
| 963 | * |
||
| 964 | * @throws DBALException |
||
| 965 | */ |
||
| 966 | 4 | public function indexIdentities(): array |
|
| 976 | |||
| 977 | /** |
||
| 978 | * @inheritdoc |
||
| 979 | * |
||
| 980 | * @throws DBALException |
||
| 981 | */ |
||
| 982 | 11 | public function read($index) |
|
| 991 | |||
| 992 | /** |
||
| 993 | * @inheritdoc |
||
| 994 | * |
||
| 995 | * @throws DBALException |
||
| 996 | */ |
||
| 997 | 2 | public function count(): ?int |
|
| 1005 | |||
| 1006 | /** |
||
| 1007 | * @param string $relationshipName |
||
| 1008 | * @param iterable|null $relationshipFilters |
||
| 1009 | * @param iterable|null $relationshipSorts |
||
| 1010 | * @param iterable|null $columns |
||
| 1011 | * |
||
| 1012 | * @return ModelQueryBuilder |
||
| 1013 | * |
||
| 1014 | * @throws DBALException |
||
| 1015 | */ |
||
| 1016 | 4 | public function createReadRelationshipBuilder( |
|
| 1060 | |||
| 1061 | /** |
||
| 1062 | * @inheritdoc |
||
| 1063 | * |
||
| 1064 | * @throws DBALException |
||
| 1065 | */ |
||
| 1066 | 3 | public function indexRelationship( |
|
| 1089 | |||
| 1090 | /** |
||
| 1091 | * @inheritdoc |
||
| 1092 | * |
||
| 1093 | * @throws DBALException |
||
| 1094 | */ |
||
| 1095 | 2 | public function indexRelationshipIdentities( |
|
| 1125 | |||
| 1126 | /** |
||
| 1127 | * @inheritdoc |
||
| 1128 | */ |
||
| 1129 | public function readRelationship( |
||
| 1130 | $index, |
||
| 1131 | string $name, |
||
| 1132 | iterable $relationshipFilters = null, |
||
| 1133 | iterable $relationshipSorts = null |
||
| 1134 | ) { |
||
| 1135 | return $this->withIndexFilter($index)->indexRelationship($name, $relationshipFilters, $relationshipSorts); |
||
| 1136 | } |
||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * @inheritdoc |
||
| 1140 | */ |
||
| 1141 | 3 | public function hasInRelationship($parentId, string $name, $childId): bool |
|
| 1166 | |||
| 1167 | /** |
||
| 1168 | * @inheritdoc |
||
| 1169 | * |
||
| 1170 | * @throws DBALException |
||
| 1171 | */ |
||
| 1172 | 1 | public function delete(): int |
|
| 1180 | |||
| 1181 | /** |
||
| 1182 | * @inheritdoc |
||
| 1183 | * |
||
| 1184 | * @throws DBALException |
||
| 1185 | */ |
||
| 1186 | 4 | public function remove($index): bool |
|
| 1196 | |||
| 1197 | /** |
||
| 1198 | * @inheritdoc |
||
| 1199 | * |
||
| 1200 | * @throws DBALException |
||
| 1201 | * |
||
| 1202 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 1203 | */ |
||
| 1204 | 4 | public function create($index, array $attributes, array $toMany): string |
|
| 1234 | |||
| 1235 | /** |
||
| 1236 | * @inheritdoc |
||
| 1237 | * |
||
| 1238 | * @throws DBALException |
||
| 1239 | * |
||
| 1240 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 1241 | */ |
||
| 1242 | 2 | public function update($index, array $attributes, array $toMany): int |
|
| 1293 | |||
| 1294 | /** |
||
| 1295 | * @param string $parentId |
||
| 1296 | * @param string $name |
||
| 1297 | * @param iterable $childIds |
||
| 1298 | * |
||
| 1299 | * @return int |
||
| 1300 | * |
||
| 1301 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 1302 | */ |
||
| 1303 | public function createInBelongsToManyRelationship(string $parentId, string $name, iterable $childIds): int |
||
| 1304 | { |
||
| 1305 | // Check that relationship is `BelongsToMany` |
||
| 1306 | assert(call_user_func(function () use ($name): bool { |
||
| 1307 | $relType = $this->getModelSchemas()->getRelationshipType($this->getModelClass(), $name); |
||
| 1308 | $errMsg = "Relationship `$name` of class `" . $this->getModelClass() . |
||
| 1309 | '` either is not `belongsToMany` or do not exist in the class.'; |
||
| 1310 | $isOk = $relType === RelationshipTypes::BELONGS_TO_MANY; |
||
| 1311 | |||
| 1312 | assert($isOk, $errMsg); |
||
| 1313 | |||
| 1314 | return $isOk; |
||
| 1315 | })); |
||
| 1316 | |||
| 1317 | $builderHook = Closure::fromCallable([$this, 'builderOnCreateInBelongsToManyRelationship']); |
||
| 1318 | |||
| 1319 | return $this->addInToManyRelationship($this->getConnection(), $parentId, $name, $childIds, $builderHook); |
||
| 1320 | } |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * @inheritdoc |
||
| 1324 | * |
||
| 1325 | * @throws DBALException |
||
| 1326 | */ |
||
| 1327 | public function removeInBelongsToManyRelationship(string $parentId, string $name, iterable $childIds): int |
||
| 1328 | { |
||
| 1329 | // Check that relationship is `BelongsToMany` |
||
| 1330 | assert(call_user_func(function () use ($name): bool { |
||
| 1331 | $relType = $this->getModelSchemas()->getRelationshipType($this->getModelClass(), $name); |
||
| 1332 | $errMsg = "Relationship `$name` of class `" . $this->getModelClass() . |
||
| 1333 | '` either is not `belongsToMany` or do not exist in the class.'; |
||
| 1334 | $isOk = $relType === RelationshipTypes::BELONGS_TO_MANY; |
||
| 1335 | |||
| 1336 | assert($isOk, $errMsg); |
||
| 1337 | |||
| 1338 | return $isOk; |
||
| 1339 | })); |
||
| 1340 | |||
| 1341 | return $this->removeInToManyRelationship($this->getConnection(), $parentId, $name, $childIds); |
||
| 1342 | } |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * @return FactoryInterface |
||
| 1346 | */ |
||
| 1347 | 29 | protected function getFactory(): FactoryInterface |
|
| 1351 | |||
| 1352 | /** |
||
| 1353 | * @return string |
||
| 1354 | */ |
||
| 1355 | 30 | protected function getModelClass(): string |
|
| 1359 | |||
| 1360 | /** |
||
| 1361 | * @return ModelSchemaInfoInterface |
||
| 1362 | */ |
||
| 1363 | 30 | protected function getModelSchemas(): ModelSchemaInfoInterface |
|
| 1367 | |||
| 1368 | /** |
||
| 1369 | * @return RelationshipPaginationStrategyInterface |
||
| 1370 | */ |
||
| 1371 | 6 | protected function getRelationshipPagingStrategy(): RelationshipPaginationStrategyInterface |
|
| 1375 | |||
| 1376 | /** |
||
| 1377 | * @param Closure $closure |
||
| 1378 | * |
||
| 1379 | * @return void |
||
| 1380 | * |
||
| 1381 | * @throws DBALException |
||
| 1382 | */ |
||
| 1383 | 4 | public function inTransaction(Closure $closure): void |
|
| 1393 | |||
| 1394 | /** |
||
| 1395 | * @inheritdoc |
||
| 1396 | * |
||
| 1397 | * @throws DBALException |
||
| 1398 | */ |
||
| 1399 | 10 | public function fetchResources(QueryBuilder $builder, string $modelClass): PaginatedDataInterface |
|
| 1410 | |||
| 1411 | /** |
||
| 1412 | * @inheritdoc |
||
| 1413 | * |
||
| 1414 | * @throws DBALException |
||
| 1415 | */ |
||
| 1416 | 9 | public function fetchResource(QueryBuilder $builder, string $modelClass) |
|
| 1427 | |||
| 1428 | /** |
||
| 1429 | * @inheritdoc |
||
| 1430 | * |
||
| 1431 | * @throws DBALException |
||
| 1432 | * |
||
| 1433 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 1434 | */ |
||
| 1435 | 2 | public function fetchRow(QueryBuilder $builder, string $modelClass): ?array |
|
| 1456 | |||
| 1457 | /** |
||
| 1458 | * @inheritdoc |
||
| 1459 | * |
||
| 1460 | * @throws DBALException |
||
| 1461 | * |
||
| 1462 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 1463 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 1464 | */ |
||
| 1465 | 5 | public function fetchColumn(QueryBuilder $builder, string $modelClass, string $columnName): iterable |
|
| 1490 | |||
| 1491 | /** |
||
| 1492 | * @param QueryBuilder $builder |
||
| 1493 | * |
||
| 1494 | * @return ModelQueryBuilder |
||
| 1495 | */ |
||
| 1496 | 2 | protected function createCountBuilderFromBuilder(QueryBuilder $builder): ModelQueryBuilder |
|
| 1504 | |||
| 1505 | /** |
||
| 1506 | * @param Connection $connection |
||
| 1507 | * @param string $primaryIdentity |
||
| 1508 | * @param string $name |
||
| 1509 | * @param iterable $secondaryIdentities |
||
| 1510 | * @param Closure $builderHook |
||
| 1511 | * |
||
| 1512 | * @return int |
||
| 1513 | */ |
||
| 1514 | 2 | private function addInToManyRelationship( |
|
| 1515 | Connection $connection, |
||
| 1516 | string $primaryIdentity, |
||
| 1517 | string $name, |
||
| 1518 | iterable $secondaryIdentities, |
||
| 1519 | Closure $builderHook |
||
| 1520 | ): int { |
||
| 1521 | 2 | $inserted = 0; |
|
| 1522 | |||
| 1523 | 2 | $secondaryIdBindName = ':secondaryId'; |
|
| 1524 | $saveToMany = $this |
||
| 1525 | 2 | ->createBuilderFromConnection($connection, $this->getModelClass()) |
|
| 1526 | 2 | ->prepareCreateInToManyRelationship($name, $primaryIdentity, $secondaryIdBindName); |
|
| 1527 | |||
| 1528 | 2 | $saveToMany = call_user_func($builderHook, $name, $saveToMany); |
|
| 1529 | |||
| 1530 | 2 | foreach ($secondaryIdentities as $secondaryId) { |
|
| 1531 | try { |
||
| 1532 | 2 | $inserted += (int)$saveToMany->setParameter($secondaryIdBindName, $secondaryId)->execute(); |
|
| 1533 | } /** @noinspection PhpRedundantCatchClauseInspection */ catch (UcvException $exception) { |
||
| 1534 | // Spec: If all of the specified resources can be added to, or are already present in, |
||
| 1535 | // the relationship then the server MUST return a successful response. |
||
| 1536 | // |
||
| 1537 | // Currently DBAL cannot do insert or update in the same request. |
||
| 1538 | // https://github.com/doctrine/dbal/issues/1320 |
||
| 1539 | 2 | continue; |
|
| 1540 | } |
||
| 1541 | } |
||
| 1542 | |||
| 1543 | 2 | return $inserted; |
|
| 1544 | } |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * @param Connection $connection |
||
| 1548 | * @param string $primaryIdentity |
||
| 1549 | * @param string $name |
||
| 1550 | * @param iterable $secondaryIdentities |
||
| 1551 | * |
||
| 1552 | * @return int |
||
| 1553 | * |
||
| 1554 | * @throws DBALException |
||
| 1555 | */ |
||
| 1556 | private function removeInToManyRelationship( |
||
| 1557 | Connection $connection, |
||
| 1558 | string $primaryIdentity, |
||
| 1559 | string $name, |
||
| 1560 | iterable $secondaryIdentities |
||
| 1561 | ): int { |
||
| 1562 | $removeToMany = $this->builderOnRemoveInBelongsToManyRelationship( |
||
| 1563 | $name, |
||
| 1564 | $this |
||
| 1565 | ->createBuilderFromConnection($connection, $this->getModelClass()) |
||
| 1566 | ->prepareDeleteInToManyRelationship($name, $primaryIdentity, $secondaryIdentities) |
||
| 1567 | ); |
||
| 1568 | $removed = $removeToMany->execute(); |
||
| 1569 | |||
| 1570 | return $removed; |
||
| 1571 | } |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * @param QueryBuilder $builder |
||
| 1575 | * @param string $modelClass |
||
| 1576 | * |
||
| 1577 | * @return mixed|null |
||
| 1578 | * |
||
| 1579 | * @throws DBALException |
||
| 1580 | * |
||
| 1581 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 1582 | */ |
||
| 1583 | 9 | private function fetchResourceWithoutRelationships(QueryBuilder $builder, string $modelClass) |
|
| 1604 | |||
| 1605 | /** |
||
| 1606 | * @param QueryBuilder $builder |
||
| 1607 | * @param string $modelClass |
||
| 1608 | * @param string $keyColumnName |
||
| 1609 | * |
||
| 1610 | * @return iterable |
||
| 1611 | * |
||
| 1612 | * @throws DBALException |
||
| 1613 | * |
||
| 1614 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 1615 | */ |
||
| 1616 | 7 | private function fetchResourcesWithoutRelationships( |
|
| 1638 | |||
| 1639 | /** |
||
| 1640 | * @param QueryBuilder $builder |
||
| 1641 | * @param string $modelClass |
||
| 1642 | * |
||
| 1643 | * @return PaginatedDataInterface |
||
| 1644 | * |
||
| 1645 | * @throws DBALException |
||
| 1646 | */ |
||
| 1647 | 15 | private function fetchPaginatedResourcesWithoutRelationships( |
|
| 1663 | |||
| 1664 | /** |
||
| 1665 | * @param QueryBuilder $builder |
||
| 1666 | * @param string $modelClass |
||
| 1667 | * |
||
| 1668 | * @return array |
||
| 1669 | * |
||
| 1670 | * @throws DBALException |
||
| 1671 | * |
||
| 1672 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 1673 | */ |
||
| 1674 | 15 | private function fetchResourceCollection(QueryBuilder $builder, string $modelClass): array |
|
| 1709 | |||
| 1710 | /** |
||
| 1711 | * @param null|string $index |
||
| 1712 | * @param iterable $attributes |
||
| 1713 | * |
||
| 1714 | * @return iterable |
||
| 1715 | */ |
||
| 1716 | 3 | protected function filterAttributesOnCreate(?string $index, iterable $attributes): iterable |
|
| 1730 | |||
| 1731 | /** |
||
| 1732 | * @param iterable $attributes |
||
| 1733 | * |
||
| 1734 | * @return iterable |
||
| 1735 | */ |
||
| 1736 | 1 | protected function filterAttributesOnUpdate(iterable $attributes): iterable |
|
| 1745 | |||
| 1746 | /** |
||
| 1747 | * @param TagStorageInterface $modelsAtPath |
||
| 1748 | * @param ArrayObject $classAtPath |
||
| 1749 | * @param ArrayObject $idsAtPath |
||
| 1750 | * @param ModelStorageInterface $deDup |
||
| 1751 | * @param string $parentsPath |
||
| 1752 | * @param array $childRelationships |
||
| 1753 | * |
||
| 1754 | * @return void |
||
| 1755 | * |
||
| 1756 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 1757 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 1758 | * |
||
| 1759 | * @throws DBALException |
||
| 1760 | */ |
||
| 1761 | 7 | private function loadRelationshipsLayer( |
|
| 1870 | |||
| 1871 | /** |
||
| 1872 | * @param string $message |
||
| 1873 | * |
||
| 1874 | * @return string |
||
| 1875 | * |
||
| 1876 | * @throws ContainerExceptionInterface |
||
| 1877 | * @throws NotFoundExceptionInterface |
||
| 1878 | */ |
||
| 1879 | 9 | private function getMessage(string $message): string |
|
| 1888 | |||
| 1889 | /** |
||
| 1890 | * @param string $class |
||
| 1891 | * @param array $attributes |
||
| 1892 | * @param Type[] $typeNames |
||
| 1893 | * @param AbstractPlatform $platform |
||
| 1894 | * |
||
| 1895 | * @return mixed |
||
| 1896 | * |
||
| 1897 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 1898 | * |
||
| 1899 | * @throws DBALException |
||
| 1900 | */ |
||
| 1901 | 17 | private function readResourceFromAssoc( |
|
| 1914 | |||
| 1915 | /** |
||
| 1916 | * @param array $attributes |
||
| 1917 | * @param Type[] $typeNames |
||
| 1918 | * @param AbstractPlatform $platform |
||
| 1919 | * |
||
| 1920 | * @return array |
||
| 1921 | * |
||
| 1922 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 1923 | * |
||
| 1924 | * @throws DBALException |
||
| 1925 | */ |
||
| 1926 | 1 | private function readRowFromAssoc(array $attributes, array $typeNames, AbstractPlatform $platform): array |
|
| 1935 | |||
| 1936 | /** |
||
| 1937 | * @param iterable $attributes |
||
| 1938 | * @param array $typeNames |
||
| 1939 | * @param AbstractPlatform $platform |
||
| 1940 | * |
||
| 1941 | * @return iterable |
||
| 1942 | * |
||
| 1943 | * @throws DBALException |
||
| 1944 | */ |
||
| 1945 | 18 | private function readTypedAttributes(iterable $attributes, array $typeNames, AbstractPlatform $platform): iterable |
|
| 1952 | } |
||
| 1953 |
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..