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 |
||
22 | class LaravelQuery implements IQueryProvider |
||
23 | { |
||
24 | protected $expression; |
||
25 | protected $auth; |
||
26 | 5 | public $queryProviderClassName; |
|
27 | |||
28 | public function __construct(AuthInterface $auth = null) |
||
29 | 5 | { |
|
30 | 5 | /* MySQLExpressionProvider();*/ |
|
31 | 5 | $this->expression = new LaravelExpressionProvider(); //PHPExpressionProvider('expression'); |
|
32 | 5 | $this->queryProviderClassName = get_class($this); |
|
33 | $this->auth = isset($auth) ? $auth : new NullAuthProvider(); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
38 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
39 | * perform the ordering and paging |
||
40 | * |
||
41 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
42 | */ |
||
43 | public function handlesOrderedPaging() |
||
44 | { |
||
45 | return true; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Gets the expression provider used by to compile OData expressions into expression used by this query provider. |
||
50 | * |
||
51 | * @return \POData\Providers\Expression\IExpressionProvider |
||
52 | */ |
||
53 | public function getExpressionProvider() |
||
54 | { |
||
55 | return $this->expression; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Gets collection of entities belongs to an entity set |
||
60 | * IE: http://host/EntitySet |
||
61 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
62 | * |
||
63 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
64 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
65 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
66 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
67 | * @param int $top number of records which need to be skip |
||
68 | * @param String $skipToken value indicating what records to skip |
||
69 | * |
||
70 | 3 | * @return QueryResult |
|
71 | */ |
||
72 | public function getResourceSet( |
||
73 | QueryType $queryType, |
||
74 | ResourceSet $resourceSet, |
||
75 | $filterInfo = null, |
||
76 | $orderBy = null, |
||
77 | $top = null, |
||
78 | $skipToken = null, |
||
79 | 3 | Model $sourceEntityInstance = null |
|
80 | ) { |
||
81 | if (null != $filterInfo && !($filterInfo instanceof FilterInfo)) { |
||
82 | 3 | throw new InvalidArgumentException('Filter info must be either null or instance of FilterInfo.'); |
|
83 | 1 | } |
|
84 | 1 | ||
85 | if (null == $sourceEntityInstance) { |
||
86 | 3 | $sourceEntityInstance = $this->getSourceEntityInstance($resourceSet); |
|
87 | 3 | } |
|
88 | 3 | ||
89 | $result = new QueryResult(); |
||
90 | 3 | $result->results = null; |
|
91 | $result->count = null; |
||
92 | |||
93 | if (null != $orderBy) { |
||
94 | foreach ($orderBy->getOrderByInfo()->getOrderByPathSegments() as $order) { |
||
95 | foreach ($order->getSubPathSegments() as $subOrder) { |
||
96 | $sourceEntityInstance = $sourceEntityInstance->orderBy( |
||
97 | 1 | $subOrder->getName(), |
|
98 | $order->isAscending() ? 'asc' : 'desc' |
||
99 | ); |
||
100 | 3 | } |
|
101 | 1 | } |
|
102 | 1 | } |
|
103 | 3 | ||
104 | 1 | $resultSet = $sourceEntityInstance->get(); |
|
105 | 1 | ||
106 | if (isset($filterInfo)) { |
||
107 | 3 | $method = "return ".$filterInfo->getExpressionAsString().";"; |
|
108 | $clln = "$".$resourceSet->getResourceType()->getName(); |
||
109 | 3 | $isvalid = create_function($clln, $method); |
|
|
|||
110 | $resultSet = $resultSet->filter($isvalid); |
||
111 | } |
||
112 | |||
113 | $resultCount = $resultSet->count(); |
||
114 | |||
115 | if (isset($skipToken)) { |
||
116 | $resultSet = $resultSet->slice($skipToken); |
||
117 | 3 | } |
|
118 | 1 | if (isset($top)) { |
|
119 | 1 | $resultSet = $resultSet->take($top); |
|
120 | 1 | } |
|
121 | 1 | ||
122 | 1 | ||
123 | 3 | if (QueryType::ENTITIES() == $queryType || QueryType::ENTITIES_WITH_COUNT() == $queryType) { |
|
124 | 3 | $result->results = array(); |
|
125 | foreach ($resultSet as $res) { |
||
126 | $result->results[] = $res; |
||
127 | 3 | } |
|
128 | 3 | } |
|
129 | 3 | if (QueryType::COUNT() == $queryType || QueryType::ENTITIES_WITH_COUNT() == $queryType) { |
|
130 | $result->count = $resultCount; |
||
131 | } |
||
132 | return $result; |
||
133 | } |
||
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 | 3 | * @return QueryResult |
|
200 | * |
||
201 | */ |
||
202 | public function getRelatedResourceSet( |
||
232 | |||
233 | /** |
||
234 | * Gets a related entity instance from an entity set identified by a key |
||
235 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
236 | * |
||
237 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
238 | * @param object $sourceEntityInstance The source entity instance. |
||
239 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
240 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
241 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
242 | * |
||
243 | * @return object|null Returns entity instance if found else null |
||
244 | */ |
||
245 | public function getResourceFromRelatedResourceSet( |
||
258 | |||
259 | /** |
||
260 | * Get related resource for a resource |
||
261 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
262 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
263 | * |
||
264 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
265 | * @param object $sourceEntityInstance The source entity instance. |
||
266 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
267 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
268 | * |
||
269 | * @return object|null The related resource if found else null |
||
270 | */ |
||
271 | public function getRelatedResourceReference( |
||
283 | |||
284 | /** |
||
285 | * @param ResourceSet $resourceSet |
||
286 | * @return mixed |
||
287 | */ |
||
288 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
294 | |||
295 | /** |
||
296 | * Updates a resource |
||
297 | * |
||
298 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
299 | 1 | * @param object $sourceEntityInstance The source entity instance |
|
300 | 1 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
|
301 | * @param object $data The New data for the entity instance. |
||
302 | 1 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
|
303 | * |
||
304 | 1 | * @return object|null The new resource value if it is assignable or throw exception for null. |
|
305 | */ |
||
306 | 1 | View Code Duplication | public function updateResource( |
328 | 1 | /** |
|
329 | * Delete resource from a resource set. |
||
330 | 1 | * @param ResourceSet|null $sourceResourceSet |
|
331 | 1 | * @param object $sourceEntityInstance |
|
332 | * |
||
333 | * return bool true if resources sucessfully deteled, otherwise false. |
||
334 | 1 | */ |
|
335 | public function deleteResource( |
||
356 | /** |
||
357 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
358 | 1 | * @param object $sourceEntityInstance The source entity instance |
|
359 | * @param object $data The New data for the entity instance. |
||
360 | * |
||
361 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
||
362 | */ |
||
363 | View Code Duplication | public function createResourceforResourceSet( |
|
383 | |||
384 | /** |
||
385 | 3 | * @param $sourceEntityInstance |
|
386 | * @param $data |
||
387 | * @param $class |
||
388 | 3 | * @param string $verb |
|
389 | 2 | * @return array|mixed |
|
390 | 2 | * @throws ODataException |
|
391 | 3 | * @throws \POData\Common\InvalidOperationException |
|
392 | */ |
||
393 | private function createUpdateDeleteCore($sourceEntityInstance, $data, $class, $verb) |
||
464 | |||
465 | /** |
||
466 | * Puts an entity instance to entity set identified by a key. |
||
467 | * |
||
468 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
||
469 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
||
470 | * @param $data |
||
471 | * |
||
472 | * @return bool|null Returns result of executiong query |
||
473 | */ |
||
474 | public function putResource( |
||
481 | } |
||
482 |
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.