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:
1 | <?php |
||
22 | class LaravelQuery implements IQueryProvider |
||
23 | { |
||
24 | protected $expression; |
||
25 | protected $auth; |
||
26 | 5 | protected $reader; |
|
27 | public $queryProviderClassName; |
||
28 | |||
29 | 5 | public function __construct(AuthInterface $auth = null) |
|
37 | |||
38 | /** |
||
39 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
40 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
41 | * perform the ordering and paging |
||
42 | * |
||
43 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
44 | */ |
||
45 | public function handlesOrderedPaging() |
||
49 | |||
50 | /** |
||
51 | * Gets the expression provider used by to compile OData expressions into expression used by this query provider. |
||
52 | * |
||
53 | * @return \POData\Providers\Expression\IExpressionProvider |
||
54 | */ |
||
55 | public function getExpressionProvider() |
||
59 | |||
60 | /** |
||
61 | * Gets the LaravelReadQuery instance used to handle read queries (repetitious, nyet?) |
||
62 | * |
||
63 | * @return LaravelReadQuery |
||
64 | */ |
||
65 | public function getReader() |
||
69 | |||
70 | 3 | /** |
|
71 | * Gets collection of entities belongs to an entity set |
||
72 | * IE: http://host/EntitySet |
||
73 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
74 | * |
||
75 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
76 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
77 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
78 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
79 | 3 | * @param int $top number of records which need to be skip |
|
80 | * @param String $skipToken value indicating what records to skip |
||
81 | * |
||
82 | 3 | * @return QueryResult |
|
83 | 1 | */ |
|
84 | 1 | View Code Duplication | public function getResourceSet( |
103 | 3 | /** |
|
104 | 1 | * Gets an entity instance from an entity set identified by a key |
|
105 | 1 | * IE: http://host/EntitySet(1L) |
|
106 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
107 | 3 | * |
|
108 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
109 | 3 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
|
110 | * |
||
111 | * @return object|null Returns entity instance if found else null |
||
112 | */ |
||
113 | public function getResourceFromResourceSet( |
||
119 | 1 | ||
120 | 1 | /** |
|
121 | 1 | * Get related resource set for a resource |
|
122 | 1 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
|
123 | 3 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
|
124 | 3 | * |
|
125 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
126 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
127 | 3 | * @param object $sourceEntityInstance The source entity instance. |
|
128 | 3 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
|
129 | 3 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
|
130 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
131 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
132 | * @param int $top number of records which need to be skip |
||
133 | * @param String $skip value indicating what records to skip |
||
134 | * |
||
135 | * @return QueryResult |
||
136 | * |
||
137 | */ |
||
138 | View Code Duplication | public function getRelatedResourceSet( |
|
161 | |||
162 | /** |
||
163 | * Gets a related entity instance from an entity set identified by a key |
||
164 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
165 | * |
||
166 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
167 | * @param object $sourceEntityInstance The source entity instance. |
||
168 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
169 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
170 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
171 | * |
||
172 | * @return object|null Returns entity instance if found else null |
||
173 | */ |
||
174 | public function getResourceFromRelatedResourceSet( |
||
189 | |||
190 | /** |
||
191 | * Get related resource for a resource |
||
192 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
193 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
194 | * |
||
195 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
196 | * @param object $sourceEntityInstance The source entity instance. |
||
197 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
198 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
199 | 3 | * |
|
200 | * @return object|null The related resource if found else null |
||
201 | */ |
||
202 | public function getRelatedResourceReference( |
||
215 | 3 | ||
216 | 3 | /** |
|
217 | 3 | * Updates a resource |
|
218 | 3 | * |
|
219 | 3 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
|
220 | * @param object $sourceEntityInstance The source entity instance |
||
221 | 3 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
|
222 | * @param object $data The New data for the entity instance. |
||
223 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
224 | * |
||
225 | * @return object|null The new resource value if it is assignable or throw exception for null. |
||
226 | */ |
||
227 | public function updateResource( |
||
237 | /** |
||
238 | * Delete resource from a resource set. |
||
239 | * @param ResourceSet|null $sourceResourceSet |
||
240 | * @param object $sourceEntityInstance |
||
241 | * |
||
242 | * return bool true if resources sucessfully deteled, otherwise false. |
||
243 | */ |
||
244 | public function deleteResource( |
||
266 | /** |
||
267 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
268 | * @param object $sourceEntityInstance The source entity instance |
||
269 | * @param object $data The New data for the entity instance. |
||
270 | * |
||
271 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
||
272 | */ |
||
273 | public function createResourceforResourceSet( |
||
281 | |||
282 | /** |
||
283 | * @param $sourceEntityInstance |
||
284 | * @param $data |
||
285 | * @param $class |
||
286 | * @param string $verb |
||
287 | * @return array|mixed |
||
288 | * @throws ODataException |
||
289 | * @throws \POData\Common\InvalidOperationException |
||
290 | */ |
||
291 | private function createUpdateDeleteCore($sourceEntityInstance, $data, $class, $verb) |
||
327 | |||
328 | 1 | /** |
|
329 | * Puts an entity instance to entity set identified by a key. |
||
330 | 1 | * |
|
331 | 1 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
|
332 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
||
333 | * @param $data |
||
334 | 1 | * |
|
335 | * @return bool|null Returns result of executiong query |
||
336 | */ |
||
337 | public function putResource( |
||
344 | |||
345 | /** |
||
346 | * @param ResourceSet $sourceResourceSet |
||
347 | * @param $sourceEntityInstance |
||
348 | 1 | * @param $data |
|
349 | 1 | * @param $verb |
|
350 | * @return mixed |
||
351 | 1 | * @throws ODataException |
|
352 | * @throws \POData\Common\InvalidOperationException |
||
353 | 1 | */ |
|
354 | private function createUpdateCoreWrapper(ResourceSet $sourceResourceSet, $sourceEntityInstance, $data, $verb) |
||
376 | |||
377 | /** |
||
378 | 3 | * @param $sourceEntityInstance |
|
379 | 3 | * @param $data |
|
380 | * @param $paramList |
||
381 | * @return array |
||
382 | */ |
||
383 | private function createUpdateDeleteProcessInput($sourceEntityInstance, $data, $paramList) |
||
404 | 3 | ||
405 | 3 | /** |
|
406 | 3 | * @param $result |
|
407 | 2 | * @return array|mixed |
|
408 | 2 | * @throws ODataException |
|
409 | 2 | */ |
|
410 | 2 | private function createUpdateDeleteProcessOutput($result) |
|
430 | } |
||
431 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.