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