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( |
||
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 indicates if this is a query for a count, entities, or entities with a 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 of containing the target of the navigation property |
||
178 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
179 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
180 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
181 | * @param int $top number of records which need to be skip |
||
182 | * @param String $skip value indicating what records to skip |
||
183 | * |
||
184 | * @return QueryResult |
||
185 | * |
||
186 | */ |
||
187 | public function getRelatedResourceSet( |
||
220 | |||
221 | /** |
||
222 | * Gets an entity instance from an entity set identified by a key |
||
223 | * IE: http://host/EntitySet(1L) |
||
224 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
225 | * |
||
226 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
227 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
228 | * |
||
229 | * @return object|null Returns entity instance if found else null |
||
230 | */ |
||
231 | public function getResourceFromResourceSet( |
||
237 | |||
238 | |||
239 | /** |
||
240 | * Common method for getResourceFromRelatedResourceSet() and getResourceFromResourceSet() |
||
241 | * @param ResourceSet|null $resourceSet |
||
242 | * @param KeyDescriptor|null $keyDescriptor |
||
243 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
244 | */ |
||
245 | public function getResource( |
||
276 | |||
277 | /** |
||
278 | * Get related resource for a resource |
||
279 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
280 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
281 | * |
||
282 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
283 | * @param object $sourceEntityInstance The source entity instance. |
||
284 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
285 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
286 | * |
||
287 | * @return object|null The related resource if found else null |
||
288 | */ |
||
289 | View Code Duplication | public function getRelatedResourceReference( |
|
305 | |||
306 | /** |
||
307 | * Gets a related entity instance from an entity set identified by a key |
||
308 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
309 | * |
||
310 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
311 | * @param object $sourceEntityInstance The source entity instance. |
||
312 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
313 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
314 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
315 | * |
||
316 | * @return object|null Returns entity instance if found else null |
||
317 | */ |
||
318 | View Code Duplication | public function getResourceFromRelatedResourceSet( |
|
331 | |||
332 | |||
333 | /** |
||
334 | * @param ResourceSet $resourceSet |
||
335 | * @return mixed |
||
336 | */ |
||
337 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
342 | |||
343 | /** |
||
344 | * @param Model|Relation|null $source |
||
345 | */ |
||
346 | protected function checkSourceInstance($source) |
||
352 | |||
353 | protected function getAuth() |
||
357 | |||
358 | /** |
||
359 | * @param $sourceEntityInstance |
||
360 | * @throws ODataException |
||
361 | */ |
||
362 | private function checkAuth($sourceEntityInstance, $checkInstance = null) |
||
373 | } |
||
374 |
create_function
can pose a great security vulnerability as it is similar toeval
, and could be used for arbitrary code execution. We highly recommend to use a closure instead.