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 LaravelReadQuery 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 LaravelReadQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class LaravelReadQuery |
||
21 | { |
||
22 | protected $auth; |
||
23 | |||
24 | public function __construct(AuthInterface $auth = null) |
||
28 | |||
29 | /** |
||
30 | * Gets collection of entities belongs to an entity set |
||
31 | * IE: http://host/EntitySet |
||
32 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
33 | * |
||
34 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
35 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
36 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
37 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
38 | * @param int $top number of records which need to be skip |
||
39 | * @param String $skipToken value indicating what records to skip |
||
40 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
41 | * |
||
42 | * @return QueryResult |
||
43 | */ |
||
44 | public function getResourceSet( |
||
159 | |||
160 | /** |
||
161 | * Get related resource set for a resource |
||
162 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
163 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
164 | * |
||
165 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
166 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
167 | * @param object $sourceEntityInstance The source entity instance. |
||
168 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
169 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
170 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
171 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
172 | * @param int $top number of records which need to be skip |
||
173 | * @param String $skip value indicating what records to skip |
||
174 | * |
||
175 | * @return QueryResult |
||
176 | * |
||
177 | */ |
||
178 | public function getRelatedResourceSet( |
||
213 | |||
214 | /** |
||
215 | * Gets an entity instance from an entity set identified by a key |
||
216 | * IE: http://host/EntitySet(1L) |
||
217 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
218 | * |
||
219 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
220 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
221 | * |
||
222 | * @return object|null Returns entity instance if found else null |
||
223 | */ |
||
224 | public function getResourceFromResourceSet( |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Common method for getResourceFromRelatedResourceSet() and getResourceFromResourceSet() |
||
234 | * @param ResourceSet|null $resourceSet |
||
235 | * @param KeyDescriptor|null $keyDescriptor |
||
236 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
237 | */ |
||
238 | public function getResource( |
||
271 | |||
272 | /** |
||
273 | * Get related resource for a resource |
||
274 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
275 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
276 | * |
||
277 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
278 | * @param object $sourceEntityInstance The source entity instance. |
||
279 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
280 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
281 | * |
||
282 | * @return object|null The related resource if found else null |
||
283 | */ |
||
284 | public function getRelatedResourceReference( |
||
302 | |||
303 | /** |
||
304 | * Gets a related entity instance from an entity set identified by a key |
||
305 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
306 | * |
||
307 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
308 | * @param object $sourceEntityInstance The source entity instance. |
||
309 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
310 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
311 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
312 | * |
||
313 | * @return object|null Returns entity instance if found else null |
||
314 | */ |
||
315 | public function getResourceFromRelatedResourceSet( |
||
328 | |||
329 | |||
330 | /** |
||
331 | * @param ResourceSet $resourceSet |
||
332 | * @return mixed |
||
333 | */ |
||
334 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
339 | |||
340 | /** |
||
341 | * @param Model|Relation|null $source |
||
342 | */ |
||
343 | protected function checkSourceInstance($source) |
||
349 | |||
350 | protected function getAuth() |
||
354 | } |
||
355 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.