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 |
||
22 | class LaravelReadQuery |
||
23 | { |
||
24 | protected $auth; |
||
25 | |||
26 | public function __construct(AuthInterface $auth = null) |
||
30 | |||
31 | /** |
||
32 | * Gets collection of entities belongs to an entity set |
||
33 | * IE: http://host/EntitySet |
||
34 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
35 | * |
||
36 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
37 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
38 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
39 | * @param null|InternalOrderByInfo $orderBy sorted order if we want to get the data in some specific order |
||
40 | * @param int $top number of records which need to be retrieved |
||
41 | * @param int $skip number of records which need to be skipped |
||
42 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
||
43 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
44 | * |
||
45 | * @return QueryResult |
||
46 | */ |
||
47 | public function getResourceSet( |
||
184 | |||
185 | /** |
||
186 | * Get related resource set for a resource |
||
187 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
188 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
189 | * |
||
190 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
191 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
192 | * @param object $sourceEntityInstance The source entity instance. |
||
193 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
194 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
195 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
196 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
197 | * @param int $top number of records which need to be skip |
||
198 | * @param String $skip value indicating what records to skip |
||
199 | * |
||
200 | * @return QueryResult |
||
201 | * |
||
202 | */ |
||
203 | public function getRelatedResourceSet( |
||
237 | |||
238 | /** |
||
239 | * Gets an entity instance from an entity set identified by a key |
||
240 | * IE: http://host/EntitySet(1L) |
||
241 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
242 | * |
||
243 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
244 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
245 | * |
||
246 | * @return object|null Returns entity instance if found else null |
||
247 | */ |
||
248 | public function getResourceFromResourceSet( |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Common method for getResourceFromRelatedResourceSet() and getResourceFromResourceSet() |
||
258 | * @param ResourceSet|null $resourceSet |
||
259 | * @param KeyDescriptor|null $keyDescriptor |
||
260 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
261 | */ |
||
262 | public function getResource( |
||
293 | |||
294 | /** |
||
295 | * Get related resource for a resource |
||
296 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
297 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
298 | * |
||
299 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
300 | * @param object $sourceEntityInstance The source entity instance. |
||
301 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
302 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
303 | * |
||
304 | * @return object|null The related resource if found else null |
||
305 | */ |
||
306 | View Code Duplication | public function getRelatedResourceReference( |
|
322 | |||
323 | /** |
||
324 | * Gets a related entity instance from an entity set identified by a key |
||
325 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
326 | * |
||
327 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
328 | * @param object $sourceEntityInstance The source entity instance. |
||
329 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
330 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
331 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
332 | * |
||
333 | * @return object|null Returns entity instance if found else null |
||
334 | */ |
||
335 | View Code Duplication | public function getResourceFromRelatedResourceSet( |
|
348 | |||
349 | |||
350 | /** |
||
351 | * @param ResourceSet $resourceSet |
||
352 | * @return mixed |
||
353 | */ |
||
354 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
359 | |||
360 | /** |
||
361 | * @param Model|Relation|null $source |
||
362 | */ |
||
363 | protected function checkSourceInstance($source) |
||
369 | |||
370 | protected function getAuth() |
||
374 | |||
375 | /** |
||
376 | * @param $sourceEntityInstance |
||
377 | * @throws ODataException |
||
378 | */ |
||
379 | private function checkAuth($sourceEntityInstance, $checkInstance = null) |
||
390 | } |
||
391 |
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.