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( |
||
157 | |||
158 | /** |
||
159 | * Get related resource set for a resource |
||
160 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
161 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
162 | * |
||
163 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
164 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
165 | * @param object $sourceEntityInstance The source entity instance. |
||
166 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
167 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
168 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
169 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
170 | * @param int $top number of records which need to be skip |
||
171 | * @param String $skip value indicating what records to skip |
||
172 | * |
||
173 | * @return QueryResult |
||
174 | * |
||
175 | */ |
||
176 | public function getRelatedResourceSet( |
||
209 | |||
210 | /** |
||
211 | * Gets an entity instance from an entity set identified by a key |
||
212 | * IE: http://host/EntitySet(1L) |
||
213 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
214 | * |
||
215 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
216 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
217 | * |
||
218 | * @return object|null Returns entity instance if found else null |
||
219 | */ |
||
220 | public function getResourceFromResourceSet( |
||
226 | |||
227 | |||
228 | /** |
||
229 | * Common method for getResourceFromRelatedResourceSet() and getResourceFromResourceSet() |
||
230 | * @param ResourceSet|null $resourceSet |
||
231 | * @param KeyDescriptor|null $keyDescriptor |
||
232 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
233 | */ |
||
234 | public function getResource( |
||
265 | |||
266 | /** |
||
267 | * Get related resource for a resource |
||
268 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
269 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
270 | * |
||
271 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
272 | * @param object $sourceEntityInstance The source entity instance. |
||
273 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
274 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
275 | * |
||
276 | * @return object|null The related resource if found else null |
||
277 | */ |
||
278 | View Code Duplication | public function getRelatedResourceReference( |
|
294 | |||
295 | /** |
||
296 | * Gets a related entity instance from an entity set identified by a key |
||
297 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
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 to fetch |
||
302 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
303 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
304 | * |
||
305 | * @return object|null Returns entity instance if found else null |
||
306 | */ |
||
307 | View Code Duplication | public function getResourceFromRelatedResourceSet( |
|
320 | |||
321 | |||
322 | /** |
||
323 | * @param ResourceSet $resourceSet |
||
324 | * @return mixed |
||
325 | */ |
||
326 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
331 | |||
332 | /** |
||
333 | * @param Model|Relation|null $source |
||
334 | */ |
||
335 | protected function checkSourceInstance($source) |
||
341 | |||
342 | protected function getAuth() |
||
346 | |||
347 | /** |
||
348 | * @param $sourceEntityInstance |
||
349 | * @throws ODataException |
||
350 | */ |
||
351 | private function checkAuth($sourceEntityInstance, $checkInstance = null) |
||
362 | } |
||
363 |
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.