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 |
||
34 | class LaravelQuery implements IQueryProvider |
||
35 | { |
||
36 | protected $expression; |
||
37 | protected $auth; |
||
38 | protected $reader; |
||
39 | public $queryProviderClassName; |
||
40 | private $verbMap = []; |
||
41 | protected $metadataProvider; |
||
42 | |||
43 | public function __construct(AuthInterface $auth = null) |
||
55 | |||
56 | /** |
||
57 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
58 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
59 | * perform the ordering and paging |
||
60 | * |
||
61 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
62 | */ |
||
63 | public function handlesOrderedPaging() |
||
67 | |||
68 | /** |
||
69 | * Gets the expression provider used by to compile OData expressions into expression used by this query provider. |
||
70 | 3 | * |
|
71 | * @return \POData\Providers\Expression\IExpressionProvider |
||
72 | */ |
||
73 | public function getExpressionProvider() |
||
77 | |||
78 | /** |
||
79 | 3 | * Gets the LaravelReadQuery instance used to handle read queries (repetitious, nyet?) |
|
80 | * |
||
81 | * @return LaravelReadQuery |
||
82 | 3 | */ |
|
83 | 1 | public function getReader() |
|
87 | 3 | ||
88 | 3 | /** |
|
89 | * Dig out local copy of POData-Laravel metadata provider |
||
90 | 3 | * |
|
91 | * @return MetadataProvider |
||
92 | */ |
||
93 | public function getMetadataProvider() |
||
97 | 1 | ||
98 | /** |
||
99 | * Gets collection of entities belongs to an entity set |
||
100 | 3 | * IE: http://host/EntitySet |
|
101 | 1 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
|
102 | 1 | * |
|
103 | 3 | * @param QueryType $queryType Is this is a query for a count, entities, or entities-with-count |
|
104 | 1 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
|
105 | 1 | * @param FilterInfo|null $filterInfo The $filter parameter of the OData query. NULL if none specified |
|
106 | * @param null|InternalOrderByInfo $orderBy sorted order if we want to get the data in some specific order |
||
107 | 3 | * @param integer|null $top number of records which need to be retrieved |
|
108 | * @param integer|null $skip number of records which need to be skipped |
||
109 | 3 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
|
110 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
111 | * |
||
112 | * @return QueryResult |
||
113 | */ |
||
114 | View Code Duplication | public function getResourceSet( |
|
136 | /** |
||
137 | * Gets an entity instance from an entity set identified by a key |
||
138 | * IE: http://host/EntitySet(1L) |
||
139 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
140 | * |
||
141 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
142 | * @param KeyDescriptor|null $keyDescriptor The key identifying the entity to fetch |
||
143 | * |
||
144 | * @return Model|null Returns entity instance if found else null |
||
145 | */ |
||
146 | public function getResourceFromResourceSet( |
||
152 | |||
153 | /** |
||
154 | * Get related resource set for a resource |
||
155 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
156 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
157 | * |
||
158 | * @param QueryType $queryType Is this is a query for a count, entities, or entities-with-count |
||
159 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
160 | * @param object $sourceEntityInstance The source entity instance |
||
161 | * @param ResourceSet $targetResourceSet The resource set pointed to by the navigation property |
||
162 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
163 | * @param FilterInfo|null $filter The $filter parameter of the OData query. NULL if none specified |
||
164 | * @param mixed|null $orderBy sorted order if we want to get the data in some specific order |
||
165 | * @param integer|null $top number of records which need to be retrieved |
||
166 | * @param integer|null $skip number of records which need to be skipped |
||
167 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
||
168 | * |
||
169 | * @return QueryResult |
||
170 | * |
||
171 | */ |
||
172 | View Code Duplication | public function getRelatedResourceSet( |
|
198 | |||
199 | 3 | /** |
|
200 | * Gets a related entity instance from an entity set identified by a key |
||
201 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
202 | * |
||
203 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
204 | * @param object $sourceEntityInstance The source entity instance. |
||
205 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
206 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
207 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
208 | * |
||
209 | * @return Model|null Returns entity instance if found else null |
||
210 | 3 | */ |
|
211 | 3 | public function getResourceFromRelatedResourceSet( |
|
227 | |||
228 | /** |
||
229 | * Get related resource for a resource |
||
230 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
231 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
232 | * |
||
233 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
234 | * @param object $sourceEntityInstance The source entity instance. |
||
235 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
236 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
237 | * |
||
238 | * @return object|null The related resource if found else null |
||
239 | */ |
||
240 | public function getRelatedResourceReference( |
||
256 | |||
257 | /** |
||
258 | * Updates a resource |
||
259 | * |
||
260 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
261 | * @param object $sourceEntityInstance The source entity instance |
||
262 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
263 | * @param object $data The New data for the entity instance. |
||
264 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
265 | * |
||
266 | * @return object|null The new resource value if it is assignable or throw exception for null. |
||
267 | */ |
||
268 | public function updateResource( |
||
280 | /** |
||
281 | * Delete resource from a resource set. |
||
282 | * @param ResourceSet $sourceResourceSet |
||
283 | * @param object $sourceEntityInstance |
||
284 | * |
||
285 | * return bool true if resources sucessfully deteled, otherwise false. |
||
286 | */ |
||
287 | public function deleteResource( |
||
311 | /** |
||
312 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
313 | * @param object $sourceEntityInstance The source entity instance |
||
314 | * @param object $data The New data for the entity instance. |
||
315 | * |
||
316 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
||
317 | */ |
||
318 | 1 | public function createResourceforResourceSet( |
|
328 | 1 | ||
329 | /** |
||
330 | 1 | * @param $sourceEntityInstance |
|
331 | 1 | * @param $data |
|
332 | * @param $class |
||
333 | * @param string $verb |
||
334 | 1 | * @return array|mixed |
|
335 | * @throws ODataException |
||
336 | * @throws InvalidOperationException |
||
337 | */ |
||
338 | private function createUpdateDeleteCore($sourceEntityInstance, $data, $class, $verb) |
||
376 | |||
377 | /** |
||
378 | 3 | * Puts an entity instance to entity set identified by a key. |
|
379 | 3 | * |
|
380 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
||
381 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
||
382 | * @param $data |
||
383 | * |
||
384 | * @return bool|null Returns result of executing query |
||
385 | 3 | */ |
|
386 | public function putResource( |
||
394 | |||
395 | /** |
||
396 | * @param ResourceSet $sourceResourceSet |
||
397 | 3 | * @param $data |
|
398 | 3 | * @param $verb |
|
399 | 3 | * @param Model|null $source |
|
400 | 3 | * @return mixed |
|
401 | 3 | * @throws InvalidOperationException |
|
402 | * @throws ODataException |
||
403 | 3 | */ |
|
404 | 3 | private function createUpdateCoreWrapper(ResourceSet $sourceResourceSet, $data, $verb, Model $source = null) |
|
425 | |||
426 | 3 | /** |
|
427 | 3 | * @param $data |
|
428 | 3 | * @param $paramList |
|
429 | 3 | * @param Model|null $sourceEntityInstance |
|
430 | * @return array |
||
431 | 3 | */ |
|
432 | private function createUpdateDeleteProcessInput($data, $paramList, Model $sourceEntityInstance = null) |
||
453 | |||
454 | /** |
||
455 | * @param $result |
||
456 | * @return array|mixed |
||
457 | * @throws ODataException |
||
458 | */ |
||
459 | private function createUpdateDeleteProcessOutput($result) |
||
479 | |||
480 | /** |
||
481 | * @param $sourceEntityInstance |
||
482 | * @return mixed|null|\object[] |
||
483 | */ |
||
484 | private function unpackSourceEntity($sourceEntityInstance) |
||
493 | |||
494 | /** |
||
495 | * Create multiple new resources in a resource set. |
||
496 | * @param ResourceSet $sourceResourceSet The entity set containing the entity to fetch |
||
497 | * @param object[] $data The new data for the entity instance |
||
498 | * |
||
499 | * @return object[] returns the newly created model if successful, or throws an exception if model creation failed |
||
500 | * @throw \Exception |
||
501 | */ |
||
502 | public function createBulkResourceforResourceSet( |
||
523 | |||
524 | /** |
||
525 | * Updates a group of resources in a resource set. |
||
526 | * |
||
527 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
528 | * @param object $sourceEntityInstance The source entity instance |
||
529 | * @param KeyDescriptor[] $keyDescriptor The key identifying the entity to fetch |
||
530 | * @param object[] $data The new data for the entity instances |
||
531 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
532 | * |
||
533 | * @return object[] the new resource value if it is assignable, or throw exception for null |
||
534 | * @throw \Exception |
||
535 | */ |
||
536 | public function updateBulkResource( |
||
568 | |||
569 | /** |
||
570 | * Attaches child model to parent model |
||
571 | * |
||
572 | * @param ResourceSet $sourceResourceSet |
||
573 | * @param object $sourceEntityInstance |
||
574 | * @param ResourceSet $targetResourceSet |
||
575 | * @param object $targetEntityInstance |
||
576 | * @param $navPropName |
||
577 | * |
||
578 | * @return bool |
||
579 | */ |
||
580 | public function hookSingleModel( |
||
599 | |||
600 | /** |
||
601 | * Removes child model from parent model |
||
602 | * |
||
603 | * @param ResourceSet $sourceResourceSet |
||
604 | * @param object $sourceEntityInstance |
||
605 | * @param ResourceSet $targetResourceSet |
||
606 | * @param object $targetEntityInstance |
||
607 | * @param $navPropName |
||
608 | * |
||
609 | * @return bool |
||
610 | */ |
||
611 | public function unhookSingleModel( |
||
644 | |||
645 | /** |
||
646 | * @param $sourceEntityInstance |
||
647 | * @param $targetEntityInstance |
||
648 | * @param $navPropName |
||
649 | * @return Relation |
||
650 | * @throws \InvalidArgumentException |
||
651 | */ |
||
652 | protected function isModelHookInputsOk($sourceEntityInstance, $targetEntityInstance, $navPropName) |
||
670 | } |
||
671 |