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 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 |
||
23 | class LaravelQuery implements IQueryProvider |
||
24 | { |
||
25 | protected $expression; |
||
26 | protected $auth; |
||
27 | public $queryProviderClassName; |
||
28 | |||
29 | 5 | public function __construct(AuthInterface $auth = null) |
|
36 | |||
37 | /** |
||
38 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
39 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
40 | * perform the ordering and paging |
||
41 | * |
||
42 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
43 | */ |
||
44 | public function handlesOrderedPaging() |
||
48 | |||
49 | /** |
||
50 | * Gets the expression provider used by to compile OData expressions into expression used by this query provider. |
||
51 | * |
||
52 | * @return \POData\Providers\Expression\IExpressionProvider |
||
53 | */ |
||
54 | public function getExpressionProvider() |
||
58 | |||
59 | /** |
||
60 | * Gets collection of entities belongs to an entity set |
||
61 | * IE: http://host/EntitySet |
||
62 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
63 | * |
||
64 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
65 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
66 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
67 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
68 | * @param int $top number of records which need to be skip |
||
69 | * @param String $skipToken value indicating what records to skip |
||
70 | * |
||
71 | * @return QueryResult |
||
72 | */ |
||
73 | 3 | public function getResourceSet( |
|
134 | /** |
||
135 | * Gets an entity instance from an entity set identified by a key |
||
136 | * IE: http://host/EntitySet(1L) |
||
137 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
138 | * |
||
139 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
140 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
141 | * |
||
142 | * @return object|null Returns entity instance if found else null |
||
143 | */ |
||
144 | public function getResourceFromResourceSet( |
||
150 | |||
151 | /** |
||
152 | * Common method for getResourceFromRelatedResourceSet() and getResourceFromResourceSet() |
||
153 | * @param ResourceSet|null $resourceSet |
||
154 | * @param null|KeyDescriptor $keyDescriptor |
||
155 | */ |
||
156 | protected function getResource( |
||
183 | |||
184 | /** |
||
185 | * Get related resource set for a resource |
||
186 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
187 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
188 | * |
||
189 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
190 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
191 | * @param object $sourceEntityInstance The source entity instance. |
||
192 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
193 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
194 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
195 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
196 | * @param int $top number of records which need to be skip |
||
197 | * @param String $skip value indicating what records to skip |
||
198 | * |
||
199 | * @return QueryResult |
||
200 | * |
||
201 | */ |
||
202 | 3 | public function getRelatedResourceSet( |
|
227 | |||
228 | /** |
||
229 | * Gets a related entity instance from an entity set identified by a key |
||
230 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
231 | * |
||
232 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
233 | * @param object $sourceEntityInstance The source entity instance. |
||
234 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
235 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
236 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
237 | * |
||
238 | * @return object|null Returns entity instance if found else null |
||
239 | */ |
||
240 | public function getResourceFromRelatedResourceSet( |
||
250 | |||
251 | /** |
||
252 | * Get related resource for a resource |
||
253 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
254 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
255 | * |
||
256 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
257 | * @param object $sourceEntityInstance The source entity instance. |
||
258 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
259 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
260 | * |
||
261 | * @return object|null The related resource if found else null |
||
262 | */ |
||
263 | public function getRelatedResourceReference( |
||
272 | |||
273 | /** |
||
274 | * @param ResourceSet $resourceSet |
||
275 | * @return mixed |
||
276 | */ |
||
277 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
283 | |||
284 | /** |
||
285 | * Updates a resource |
||
286 | * |
||
287 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
288 | * @param object $sourceEntityInstance The source entity instance |
||
289 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
290 | * @param object $data The New data for the entity instance. |
||
291 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
292 | * |
||
293 | * @return object|null The new resource value if it is assignable or throw exception for null. |
||
294 | */ |
||
295 | 1 | View Code Duplication | public function updateResource( |
314 | /** |
||
315 | * Delete resource from a resource set. |
||
316 | * @param ResourceSet|null $resourceSet |
||
317 | * @param object $sourceEntityInstance |
||
318 | * |
||
319 | * return bool true if resources sucessfully deteled, otherwise false. |
||
320 | */ |
||
321 | 1 | View Code Duplication | public function deleteResource( |
336 | /** |
||
337 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
338 | * @param object $sourceEntityInstance The source entity instance |
||
339 | * @param object $data The New data for the entity instance. |
||
340 | * |
||
341 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
||
342 | */ |
||
343 | 1 | View Code Duplication | public function createResourceforResourceSet( |
360 | |||
361 | /** |
||
362 | * @param $sourceEntityInstance |
||
363 | * @param $data |
||
364 | * @param $class |
||
365 | * @param $verb |
||
366 | * @return array|mixed |
||
367 | * @throws ODataException |
||
368 | * @throws \POData\Common\InvalidOperationException |
||
369 | */ |
||
370 | 3 | private function createUpdateDeleteCore($sourceEntityInstance, $data, $class, $verb) |
|
441 | } |
||
442 |
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.