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 |
||
30 | 5 | class LaravelQuery implements IQueryProvider |
|
31 | 5 | { |
|
32 | 5 | protected $expression; |
|
33 | protected $auth; |
||
34 | protected $reader; |
||
35 | public $queryProviderClassName; |
||
36 | private $verbMap = []; |
||
37 | protected $metadataProvider; |
||
38 | protected $controllerContainer; |
||
39 | |||
40 | public function __construct(AuthInterface $auth = null) |
||
41 | { |
||
42 | /* MySQLExpressionProvider();*/ |
||
43 | $this->expression = new LaravelExpressionProvider(); //PHPExpressionProvider('expression'); |
||
44 | $this->queryProviderClassName = get_class($this); |
||
45 | $this->auth = isset($auth) ? $auth : new NullAuthProvider(); |
||
46 | $this->reader = new LaravelReadQuery($this->auth); |
||
47 | $this->verbMap['create'] = ActionVerb::CREATE(); |
||
48 | $this->verbMap['update'] = ActionVerb::UPDATE(); |
||
49 | $this->verbMap['delete'] = ActionVerb::DELETE(); |
||
50 | $this->metadataProvider = new MetadataProvider(App::make('app')); |
||
51 | $this->controllerContainer = App::make('metadataControllers'); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
56 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
57 | * perform the ordering and paging. |
||
58 | * |
||
59 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
60 | */ |
||
61 | public function handlesOrderedPaging() |
||
62 | { |
||
63 | return true; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Gets the expression provider used by to compile OData expressions into expression used by this query provider. |
||
68 | * |
||
69 | * @return \POData\Providers\Expression\IExpressionProvider |
||
70 | 3 | */ |
|
71 | public function getExpressionProvider() |
||
72 | { |
||
73 | return $this->expression; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Gets the LaravelReadQuery instance used to handle read queries (repetitious, nyet?). |
||
78 | * |
||
79 | 3 | * @return LaravelReadQuery |
|
80 | */ |
||
81 | public function getReader() |
||
82 | 3 | { |
|
83 | 1 | return $this->reader; |
|
84 | 1 | } |
|
85 | |||
86 | 3 | /** |
|
87 | 3 | * Dig out local copy of POData-Laravel metadata provider. |
|
88 | 3 | * |
|
89 | * @return MetadataProvider |
||
90 | 3 | */ |
|
91 | public function getMetadataProvider() |
||
92 | { |
||
93 | return $this->metadataProvider; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | 1 | * Dig out local copy of controller metadata mapping. |
|
98 | * |
||
99 | * @return MetadataControllerContainer |
||
100 | 3 | */ |
|
101 | 1 | public function getControllerContainer() |
|
102 | 1 | { |
|
103 | 3 | assert(null !== $this->controllerContainer, get_class($this->controllerContainer)); |
|
104 | 1 | return $this->controllerContainer; |
|
105 | 1 | } |
|
106 | |||
107 | 3 | /** |
|
108 | * Gets collection of entities belongs to an entity set |
||
109 | 3 | * IE: http://host/EntitySet |
|
110 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value. |
||
111 | * |
||
112 | * @param QueryType $queryType Is this is a query for a count, entities, |
||
113 | * or entities-with-count? |
||
114 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
115 | * @param FilterInfo|null $filterInfo The $filter parameter of the OData query. NULL if absent |
||
116 | * @param null|InternalOrderByInfo $orderBy sorted order if we want to get the data in some |
||
117 | 3 | * specific order |
|
118 | 1 | * @param int|null $top number of records which need to be retrieved |
|
119 | 1 | * @param int|null $skip number of records which need to be skipped |
|
120 | 1 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
|
121 | 1 | * @param string[]|null $eagerLoad array of relations to eager load |
|
122 | 1 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
|
123 | 3 | * |
|
124 | 3 | * @return QueryResult |
|
125 | */ |
||
126 | public function getResourceSet( |
||
127 | 3 | QueryType $queryType, |
|
128 | 3 | ResourceSet $resourceSet, |
|
129 | 3 | $filterInfo = null, |
|
130 | $orderBy = null, |
||
131 | $top = null, |
||
132 | $skip = null, |
||
133 | $skipToken = null, |
||
134 | array $eagerLoad = null, |
||
135 | $sourceEntityInstance = null |
||
136 | ) { |
||
137 | $source = $this->unpackSourceEntity($sourceEntityInstance); |
||
138 | return $this->getReader()->getResourceSet( |
||
139 | $queryType, |
||
140 | $resourceSet, |
||
141 | $filterInfo, |
||
142 | $orderBy, |
||
143 | $top, |
||
144 | $skip, |
||
145 | $skipToken, |
||
146 | $eagerLoad, |
||
147 | $source |
||
148 | ); |
||
149 | } |
||
150 | /** |
||
151 | * Gets an entity instance from an entity set identified by a key |
||
152 | * IE: http://host/EntitySet(1L) |
||
153 | * http://host/EntitySet(KeyA=2L,KeyB='someValue'). |
||
154 | * |
||
155 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
156 | * @param KeyDescriptor|null $keyDescriptor The key identifying the entity to fetch |
||
157 | * @param string[]|null $eagerLoad array of relations to eager load |
||
158 | * |
||
159 | * @return Model|null Returns entity instance if found else null |
||
160 | */ |
||
161 | public function getResourceFromResourceSet( |
||
162 | ResourceSet $resourceSet, |
||
163 | KeyDescriptor $keyDescriptor = null, |
||
164 | array $eagerLoad = null |
||
165 | ) { |
||
166 | return $this->getReader()->getResourceFromResourceSet($resourceSet, $keyDescriptor, $eagerLoad); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Get related resource set for a resource |
||
171 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
172 | * http://host/EntitySet?$expand=NavigationPropertyToCollection. |
||
173 | * |
||
174 | * @param QueryType $queryType Is this is a query for a count, entities, or entities-with-count |
||
175 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
176 | * @param object $sourceEntityInstance The source entity instance |
||
177 | * @param ResourceSet $targetResourceSet The resource set pointed to by the navigation property |
||
178 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
179 | * @param FilterInfo|null $filter The $filter parameter of the OData query. NULL if none specified |
||
180 | * @param mixed|null $orderBy sorted order if we want to get the data in some specific order |
||
181 | * @param int|null $top number of records which need to be retrieved |
||
182 | * @param int|null $skip number of records which need to be skipped |
||
183 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
||
184 | * |
||
185 | * @return QueryResult |
||
186 | */ |
||
187 | public function getRelatedResourceSet( |
||
188 | QueryType $queryType, |
||
189 | ResourceSet $sourceResourceSet, |
||
190 | $sourceEntityInstance, |
||
191 | ResourceSet $targetResourceSet, |
||
192 | ResourceProperty $targetProperty, |
||
193 | FilterInfo $filter = null, |
||
194 | $orderBy = null, |
||
195 | $top = null, |
||
196 | $skip = null, |
||
197 | $skipToken = null |
||
198 | ) { |
||
199 | 3 | $source = $this->unpackSourceEntity($sourceEntityInstance); |
|
200 | return $this->getReader()->getRelatedResourceSet( |
||
201 | $queryType, |
||
202 | $sourceResourceSet, |
||
203 | $source, |
||
204 | $targetResourceSet, |
||
205 | $targetProperty, |
||
206 | $filter, |
||
207 | $orderBy, |
||
208 | $top, |
||
209 | $skip, |
||
210 | 3 | $skipToken |
|
211 | 3 | ); |
|
212 | } |
||
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 nav 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 | * |
||
298 | * @param ResourceSet $sourceResourceSet |
||
299 | 1 | * @param object $sourceEntityInstance |
|
300 | 1 | * |
|
301 | * return bool true if resources sucessfully deteled, otherwise false |
||
302 | 1 | */ |
|
303 | public function deleteResource( |
||
304 | 1 | ResourceSet $sourceResourceSet, |
|
327 | /** |
||
328 | 1 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
|
329 | * @param object $sourceEntityInstance The source entity instance |
||
330 | 1 | * @param object $data the New data for the entity instance |
|
331 | 1 | * |
|
332 | * @returns object|null returns the newly created model if successful, |
||
333 | * or null if model creation failed. |
||
334 | 1 | */ |
|
335 | public function createResourceforResourceSet( |
||
345 | |||
346 | /** |
||
347 | * @param $sourceEntityInstance |
||
348 | 1 | * @param $data |
|
349 | 1 | * @param $class |
|
350 | * @param string $verb |
||
351 | 1 | * |
|
352 | * @throws ODataException |
||
353 | 1 | * @throws InvalidOperationException |
|
354 | * @return array|mixed |
||
355 | 1 | */ |
|
356 | private function createUpdateDeleteCore($sourceEntityInstance, $data, $class, $verb) |
||
394 | |||
395 | /** |
||
396 | * Puts an entity instance to entity set identified by a key. |
||
397 | 3 | * |
|
398 | 3 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
|
399 | 3 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
|
400 | 3 | * @param $data |
|
401 | 3 | * |
|
402 | * @return bool|null Returns result of executing query |
||
403 | 3 | */ |
|
404 | 3 | public function putResource( |
|
412 | |||
413 | /** |
||
414 | * @param ResourceSet $sourceResourceSet |
||
415 | 2 | * @param $data |
|
416 | * @param $verb |
||
417 | * @param Model|null $source |
||
418 | 3 | * @throws InvalidOperationException |
|
419 | 3 | * @throws ODataException |
|
420 | * @return mixed |
||
421 | 3 | */ |
|
422 | private function createUpdateCoreWrapper(ResourceSet $sourceResourceSet, $data, $verb, Model $source = null) |
||
443 | |||
444 | /** |
||
445 | * @param $data |
||
446 | * @param $paramList |
||
447 | * @param Model|null $sourceEntityInstance |
||
448 | * @return array |
||
449 | */ |
||
450 | private function createUpdateDeleteProcessInput($data, $paramList, Model $sourceEntityInstance = null) |
||
471 | |||
472 | /** |
||
473 | * @param $result |
||
474 | * @throws ODataException |
||
475 | * @return array|mixed |
||
476 | */ |
||
477 | private function createUpdateDeleteProcessOutput($result) |
||
497 | |||
498 | /** |
||
499 | * @param $sourceEntityInstance |
||
500 | * @return mixed|null|\object[] |
||
501 | */ |
||
502 | private function unpackSourceEntity($sourceEntityInstance) |
||
511 | |||
512 | /** |
||
513 | * Create multiple new resources in a resource set. |
||
514 | * |
||
515 | * @param ResourceSet $sourceResourceSet The entity set containing the entity to fetch |
||
516 | * @param object[] $data The new data for the entity instance |
||
517 | * |
||
518 | * @return object[] returns the newly created model if successful, or throws an exception if model creation failed |
||
519 | * @throw \Exception |
||
520 | */ |
||
521 | public function createBulkResourceforResourceSet( |
||
551 | |||
552 | /** |
||
553 | * Updates a group of resources in a resource set. |
||
554 | * |
||
555 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
556 | * @param object $sourceEntityInstance The source entity instance |
||
557 | * @param KeyDescriptor[] $keyDescriptor The key identifying the entity to fetch |
||
558 | * @param object[] $data The new data for the entity instances |
||
559 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
560 | * |
||
561 | * @return object[] the new resource value if it is assignable, or throw exception for null |
||
562 | * @throw \Exception |
||
563 | */ |
||
564 | public function updateBulkResource( |
||
604 | |||
605 | /** |
||
606 | * Attaches child model to parent model. |
||
607 | * |
||
608 | * @param ResourceSet $sourceResourceSet |
||
609 | * @param object $sourceEntityInstance |
||
610 | * @param ResourceSet $targetResourceSet |
||
611 | * @param object $targetEntityInstance |
||
612 | * @param $navPropName |
||
613 | * |
||
614 | * @return bool |
||
615 | */ |
||
616 | public function hookSingleModel( |
||
640 | |||
641 | /** |
||
642 | * Removes child model from parent model. |
||
643 | * |
||
644 | * @param ResourceSet $sourceResourceSet |
||
645 | * @param object $sourceEntityInstance |
||
646 | * @param ResourceSet $targetResourceSet |
||
647 | * @param object $targetEntityInstance |
||
648 | * @param $navPropName |
||
649 | * |
||
650 | * @return bool |
||
651 | */ |
||
652 | public function unhookSingleModel( |
||
691 | |||
692 | /** |
||
693 | * @param $sourceEntityInstance |
||
694 | * @param $targetEntityInstance |
||
695 | * @param $navPropName |
||
696 | * @throws \InvalidArgumentException |
||
697 | * @return Relation |
||
698 | */ |
||
699 | protected function isModelHookInputsOk($sourceEntityInstance, $targetEntityInstance, $navPropName) |
||
717 | |||
718 | /** |
||
719 | * @param ResourceSet $sourceResourceSet |
||
720 | * @param $verbName |
||
721 | * @return array|null |
||
722 | */ |
||
723 | protected function getOptionalVerbMapping(ResourceSet $sourceResourceSet, $verbName) |
||
731 | |||
732 | /** |
||
733 | * Prepare bulk request from supplied data. If $keyDescriptors is not null, its elements are assumed to |
||
734 | * correspond 1-1 to those in $data. |
||
735 | * |
||
736 | * @param $paramList |
||
737 | * @param array $data |
||
738 | * @param KeyDescriptor[]|null $keyDescriptors |
||
739 | */ |
||
740 | protected function prepareBulkRequestInput($paramList, array $data, array $keyDescriptors = null) |
||
773 | |||
774 | /** |
||
775 | * @param ResourceSet $sourceResourceSet |
||
776 | * @param array $data |
||
777 | * @param $mapping |
||
778 | * @param $pastVerb |
||
779 | * @param KeyDescriptor[]|null $keyDescriptor |
||
780 | * @throws ODataException |
||
781 | * @return array |
||
782 | */ |
||
783 | protected function processBulkCustom( |
||
814 | |||
815 | /** |
||
816 | * Start database transaction. |
||
817 | */ |
||
818 | public function startTransaction() |
||
822 | |||
823 | /** |
||
824 | * Commit database transaction. |
||
825 | */ |
||
826 | public function commitTransaction() |
||
830 | |||
831 | /** |
||
832 | * Abort database transaction. |
||
833 | */ |
||
834 | public function rollBackTransaction() |
||
838 | } |
||
839 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.