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 |
||
| 16 | class LaravelReadQuery |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Gets collection of entities belongs to an entity set |
||
| 21 | * IE: http://host/EntitySet |
||
| 22 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
| 23 | * |
||
| 24 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
| 25 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
| 26 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
| 27 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
| 28 | * @param int $top number of records which need to be skip |
||
| 29 | * @param String $skipToken value indicating what records to skip |
||
| 30 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
| 31 | * |
||
| 32 | * @return QueryResult |
||
| 33 | */ |
||
| 34 | public function getResourceSet( |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get related resource set for a resource |
||
| 101 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
| 102 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
| 103 | * |
||
| 104 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
| 105 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 106 | * @param object $sourceEntityInstance The source entity instance. |
||
| 107 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
| 108 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
| 109 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
| 110 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
| 111 | * @param int $top number of records which need to be skip |
||
| 112 | * @param String $skip value indicating what records to skip |
||
| 113 | * |
||
| 114 | * @return QueryResult |
||
| 115 | * |
||
| 116 | */ |
||
| 117 | public function getRelatedResourceSet( |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Gets an entity instance from an entity set identified by a key |
||
| 151 | * IE: http://host/EntitySet(1L) |
||
| 152 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
| 153 | * |
||
| 154 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
| 155 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
| 156 | * |
||
| 157 | * @return object|null Returns entity instance if found else null |
||
| 158 | */ |
||
| 159 | public function getResourceFromResourceSet( |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Common method for getResourceFromRelatedResourceSet() and getResourceFromResourceSet() |
||
| 169 | * @param ResourceSet|null $resourceSet |
||
| 170 | * @param KeyDescriptor|null $keyDescriptor |
||
| 171 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
| 172 | */ |
||
| 173 | public function getResource( |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get related resource for a resource |
||
| 205 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
| 206 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
| 207 | * |
||
| 208 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 209 | * @param object $sourceEntityInstance The source entity instance. |
||
| 210 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
| 211 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
| 212 | * |
||
| 213 | * @return object|null The related resource if found else null |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function getRelatedResourceReference( |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Gets a related entity instance from an entity set identified by a key |
||
| 232 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
| 233 | * |
||
| 234 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 235 | * @param object $sourceEntityInstance The source entity instance. |
||
| 236 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
| 237 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
| 238 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
| 239 | * |
||
| 240 | * @return object|null Returns entity instance if found else null |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function getResourceFromRelatedResourceSet( |
|
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * @param ResourceSet $resourceSet |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param Model|Relation|null $source |
||
| 269 | */ |
||
| 270 | protected function checkSourceInstance($source) |
||
| 276 | } |
||
| 277 |
create_functioncan 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.