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 LaravelQuery 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 LaravelQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class LaravelQuery implements IQueryProvider |
||
37 | { |
||
38 | protected $expression; |
||
39 | protected $auth; |
||
40 | protected $reader; |
||
41 | public $queryProviderClassName; |
||
42 | private $verbMap = []; |
||
43 | protected $metadataProvider; |
||
44 | protected $controllerContainer; |
||
45 | |||
46 | public function __construct(AuthInterface $auth = null) |
||
59 | |||
60 | /** |
||
61 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
62 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
63 | * perform the ordering and paging |
||
64 | * |
||
65 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
66 | */ |
||
67 | public function handlesOrderedPaging() |
||
71 | |||
72 | /** |
||
73 | * Gets the expression provider used by to compile OData expressions into expression used by this query provider. |
||
74 | * |
||
75 | * @return \POData\Providers\Expression\IExpressionProvider |
||
76 | */ |
||
77 | public function getExpressionProvider() |
||
81 | |||
82 | 3 | /** |
|
83 | 1 | * Gets the LaravelReadQuery instance used to handle read queries (repetitious, nyet?) |
|
84 | 1 | * |
|
85 | * @return LaravelReadQuery |
||
86 | 3 | */ |
|
87 | 3 | public function getReader() |
|
91 | |||
92 | /** |
||
93 | * Dig out local copy of POData-Laravel metadata provider |
||
94 | * |
||
95 | * @return MetadataProvider |
||
96 | */ |
||
97 | 1 | public function getMetadataProvider() |
|
101 | 1 | ||
102 | 1 | /** |
|
103 | 3 | * Dig out local copy of controller metadata mapping |
|
104 | 1 | * |
|
105 | 1 | * @return MetadataControllerContainer |
|
106 | */ |
||
107 | 3 | public function getControllerContainer() |
|
112 | |||
113 | /** |
||
114 | * Gets collection of entities belongs to an entity set |
||
115 | * IE: http://host/EntitySet |
||
116 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
117 | 3 | * |
|
118 | 1 | * @param QueryType $queryType Is this is a query for a count, entities, or entities-with-count |
|
119 | 1 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
|
120 | 1 | * @param FilterInfo|null $filterInfo The $filter parameter of the OData query. NULL if none specified |
|
121 | 1 | * @param null|InternalOrderByInfo $orderBy sorted order if we want to get the data in some specific order |
|
122 | 1 | * @param integer|null $top number of records which need to be retrieved |
|
123 | 3 | * @param integer|null $skip number of records which need to be skipped |
|
124 | 3 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
|
125 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
126 | * |
||
127 | 3 | * @return QueryResult |
|
128 | 3 | */ |
|
129 | 3 | View Code Duplication | public function getResourceSet( |
151 | /** |
||
152 | * Gets an entity instance from an entity set identified by a key |
||
153 | * IE: http://host/EntitySet(1L) |
||
154 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
155 | * |
||
156 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
157 | * @param KeyDescriptor|null $keyDescriptor The key identifying the entity to fetch |
||
158 | * |
||
159 | * @return Model|null Returns entity instance if found else null |
||
160 | */ |
||
161 | public function getResourceFromResourceSet( |
||
167 | |||
168 | /** |
||
169 | * Get related resource set for a resource |
||
170 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
171 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
172 | * |
||
173 | * @param QueryType $queryType Is this is a query for a count, entities, or entities-with-count |
||
174 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
175 | * @param object $sourceEntityInstance The source entity instance |
||
176 | * @param ResourceSet $targetResourceSet The resource set pointed to by the navigation property |
||
177 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
178 | * @param FilterInfo|null $filter The $filter parameter of the OData query. NULL if none specified |
||
179 | * @param mixed|null $orderBy sorted order if we want to get the data in some specific order |
||
180 | * @param integer|null $top number of records which need to be retrieved |
||
181 | * @param integer|null $skip number of records which need to be skipped |
||
182 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
||
183 | * |
||
184 | * @return QueryResult |
||
185 | * |
||
186 | */ |
||
187 | View Code Duplication | public function getRelatedResourceSet( |
|
213 | 3 | ||
214 | 3 | /** |
|
215 | 3 | * Gets a related entity instance from an entity set identified by a key |
|
216 | 3 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
|
217 | 3 | * |
|
218 | 3 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
|
219 | 3 | * @param object $sourceEntityInstance The source entity instance. |
|
220 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
221 | 3 | * @param ResourceProperty $targetProperty The metadata of the target property. |
|
222 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
223 | * |
||
224 | * @return Model|null Returns entity instance if found else null |
||
225 | */ |
||
226 | public function getResourceFromRelatedResourceSet( |
||
242 | |||
243 | /** |
||
244 | * Get related resource for a resource |
||
245 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
246 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
247 | * |
||
248 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
249 | * @param object $sourceEntityInstance The source entity instance. |
||
250 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
251 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
252 | * |
||
253 | * @return object|null The related resource if found else null |
||
254 | */ |
||
255 | public function getRelatedResourceReference( |
||
271 | |||
272 | /** |
||
273 | * Updates a resource |
||
274 | * |
||
275 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
276 | * @param object $sourceEntityInstance The source entity instance |
||
277 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
278 | * @param object $data The New data for the entity instance. |
||
279 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
280 | * |
||
281 | * @return object|null The new resource value if it is assignable or throw exception for null. |
||
282 | */ |
||
283 | public function updateResource( |
||
295 | /** |
||
296 | * Delete resource from a resource set. |
||
297 | * @param ResourceSet $sourceResourceSet |
||
298 | * @param object $sourceEntityInstance |
||
299 | 1 | * |
|
300 | 1 | * return bool true if resources sucessfully deteled, otherwise false. |
|
301 | */ |
||
302 | 1 | public function deleteResource( |
|
326 | 1 | /** |
|
327 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
328 | 1 | * @param object $sourceEntityInstance The source entity instance |
|
329 | * @param object $data The New data for the entity instance. |
||
330 | 1 | * |
|
331 | 1 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
|
332 | */ |
||
333 | public function createResourceforResourceSet( |
||
343 | 1 | ||
344 | /** |
||
345 | * @param $sourceEntityInstance |
||
346 | * @param $data |
||
347 | * @param $class |
||
348 | 1 | * @param string $verb |
|
349 | 1 | * @return array|mixed |
|
350 | * @throws ODataException |
||
351 | 1 | * @throws InvalidOperationException |
|
352 | */ |
||
353 | 1 | private function createUpdateDeleteCore($sourceEntityInstance, $data, $class, $verb) |
|
391 | 3 | ||
392 | /** |
||
393 | * Puts an entity instance to entity set identified by a key. |
||
394 | * |
||
395 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
||
396 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
||
397 | 3 | * @param $data |
|
398 | 3 | * |
|
399 | 3 | * @return bool|null Returns result of executing query |
|
400 | 3 | */ |
|
401 | 3 | public function putResource( |
|
409 | 2 | ||
410 | 2 | /** |
|
411 | 2 | * @param ResourceSet $sourceResourceSet |
|
412 | * @param $data |
||
413 | * @param $verb |
||
414 | * @param Model|null $source |
||
415 | 2 | * @return mixed |
|
416 | * @throws InvalidOperationException |
||
417 | * @throws ODataException |
||
418 | 3 | */ |
|
419 | 3 | private function createUpdateCoreWrapper(ResourceSet $sourceResourceSet, $data, $verb, Model $source = null) |
|
440 | |||
441 | /** |
||
442 | * @param $data |
||
443 | * @param $paramList |
||
444 | * @param Model|null $sourceEntityInstance |
||
445 | * @return array |
||
446 | */ |
||
447 | private function createUpdateDeleteProcessInput($data, $paramList, Model $sourceEntityInstance = null) |
||
468 | |||
469 | /** |
||
470 | * @param $result |
||
471 | * @return array|mixed |
||
472 | * @throws ODataException |
||
473 | */ |
||
474 | private function createUpdateDeleteProcessOutput($result) |
||
494 | |||
495 | /** |
||
496 | * @param $sourceEntityInstance |
||
497 | * @return mixed|null|\object[] |
||
498 | */ |
||
499 | private function unpackSourceEntity($sourceEntityInstance) |
||
508 | |||
509 | /** |
||
510 | * Create multiple new resources in a resource set. |
||
511 | * @param ResourceSet $sourceResourceSet The entity set containing the entity to fetch |
||
512 | * @param object[] $data The new data for the entity instance |
||
513 | * |
||
514 | * @return object[] returns the newly created model if successful, or throws an exception if model creation failed |
||
515 | * @throw \Exception |
||
516 | */ |
||
517 | public function createBulkResourceforResourceSet( |
||
547 | |||
548 | /** |
||
549 | * Updates a group of resources in a resource set. |
||
550 | * |
||
551 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
552 | * @param object $sourceEntityInstance The source entity instance |
||
553 | * @param KeyDescriptor[] $keyDescriptor The key identifying the entity to fetch |
||
554 | * @param object[] $data The new data for the entity instances |
||
555 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
556 | * |
||
557 | * @return object[] the new resource value if it is assignable, or throw exception for null |
||
558 | * @throw \Exception |
||
559 | */ |
||
560 | public function updateBulkResource( |
||
600 | |||
601 | /** |
||
602 | * Attaches child model to parent model |
||
603 | * |
||
604 | * @param ResourceSet $sourceResourceSet |
||
605 | * @param object $sourceEntityInstance |
||
606 | * @param ResourceSet $targetResourceSet |
||
607 | * @param object $targetEntityInstance |
||
608 | * @param $navPropName |
||
609 | * |
||
610 | * @return bool |
||
611 | */ |
||
612 | public function hookSingleModel( |
||
634 | |||
635 | /** |
||
636 | * Removes child model from parent model |
||
637 | * |
||
638 | * @param ResourceSet $sourceResourceSet |
||
639 | * @param object $sourceEntityInstance |
||
640 | * @param ResourceSet $targetResourceSet |
||
641 | * @param object $targetEntityInstance |
||
642 | * @param $navPropName |
||
643 | * |
||
644 | * @return bool |
||
645 | */ |
||
646 | public function unhookSingleModel( |
||
682 | |||
683 | /** |
||
684 | * @param $sourceEntityInstance |
||
685 | * @param $targetEntityInstance |
||
686 | * @param $navPropName |
||
687 | * @return Relation |
||
688 | * @throws \InvalidArgumentException |
||
689 | */ |
||
690 | protected function isModelHookInputsOk($sourceEntityInstance, $targetEntityInstance, $navPropName) |
||
708 | |||
709 | /** |
||
710 | * @param ResourceSet $sourceResourceSet |
||
711 | * @return array|null |
||
712 | * @param $verbName |
||
713 | */ |
||
714 | protected function getOptionalVerbMapping(ResourceSet $sourceResourceSet, $verbName) |
||
722 | |||
723 | /** |
||
724 | * Prepare bulk request from supplied data. If $keyDescriptors is not null, its elements are assumed to |
||
725 | * correspond 1-1 to those in $data. |
||
726 | * |
||
727 | * @param $paramList |
||
728 | * @param array $data |
||
729 | * @param KeyDescriptor[]|null $keyDescriptors |
||
730 | */ |
||
731 | protected function prepareBulkRequestInput($paramList, array $data, array $keyDescriptors = null) |
||
760 | |||
761 | /** |
||
762 | * @param ResourceSet $sourceResourceSet |
||
763 | * @param array $data |
||
764 | * @param $mapping |
||
765 | * @param $pastVerb |
||
766 | * @param array $keyDescriptor |
||
767 | * @return array |
||
768 | * @throws ODataException |
||
769 | */ |
||
770 | protected function processBulkCustom( |
||
799 | } |
||
800 |