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 |
||
| 27 | class LaravelQuery implements IQueryProvider |
||
| 28 | { |
||
| 29 | 5 | protected $expression; |
|
| 30 | 5 | protected $auth; |
|
| 31 | 5 | protected $reader; |
|
| 32 | 5 | public $queryProviderClassName; |
|
| 33 | private $verbMap = []; |
||
| 34 | |||
| 35 | public function __construct(AuthInterface $auth = null) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
| 49 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
| 50 | * perform the ordering and paging |
||
| 51 | * |
||
| 52 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
| 53 | */ |
||
| 54 | public function handlesOrderedPaging() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Gets the expression provider used by to compile OData expressions into expression used by this query provider. |
||
| 61 | * |
||
| 62 | * @return \POData\Providers\Expression\IExpressionProvider |
||
| 63 | */ |
||
| 64 | public function getExpressionProvider() |
||
| 68 | |||
| 69 | /** |
||
| 70 | 3 | * Gets the LaravelReadQuery instance used to handle read queries (repetitious, nyet?) |
|
| 71 | * |
||
| 72 | * @return LaravelReadQuery |
||
| 73 | */ |
||
| 74 | public function getReader() |
||
| 78 | |||
| 79 | 3 | /** |
|
| 80 | * Gets collection of entities belongs to an entity set |
||
| 81 | * IE: http://host/EntitySet |
||
| 82 | 3 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
|
| 83 | 1 | * |
|
| 84 | 1 | * @param QueryType $queryType Is this is a query for a count, entities, or entities-with-count |
|
| 85 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
| 86 | 3 | * @param FilterInfo|null $filterInfo The $filter parameter of the OData query. NULL if none specified |
|
| 87 | 3 | * @param null|InternalOrderByInfo $orderBy sorted order if we want to get the data in some specific order |
|
| 88 | 3 | * @param integer|null $top number of records which need to be retrieved |
|
| 89 | * @param integer|null $skip number of records which need to be skipped |
||
| 90 | 3 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
|
| 91 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
| 92 | * |
||
| 93 | * @return QueryResult |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function getResourceSet( |
|
| 117 | 3 | /** |
|
| 118 | 1 | * Gets an entity instance from an entity set identified by a key |
|
| 119 | 1 | * IE: http://host/EntitySet(1L) |
|
| 120 | 1 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
|
| 121 | 1 | * |
|
| 122 | 1 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
|
| 123 | 3 | * @param KeyDescriptor|null $keyDescriptor The key identifying the entity to fetch |
|
| 124 | 3 | * |
|
| 125 | * @return Model|null Returns entity instance if found else null |
||
| 126 | */ |
||
| 127 | 3 | public function getResourceFromResourceSet( |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get related resource set for a resource |
||
| 136 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
| 137 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
| 138 | * |
||
| 139 | * @param QueryType $queryType Is this is a query for a count, entities, or entities-with-count |
||
| 140 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 141 | * @param object $sourceEntityInstance The source entity instance |
||
| 142 | * @param ResourceSet $targetResourceSet The resource set pointed to by the navigation property |
||
| 143 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
| 144 | * @param FilterInfo|null $filter The $filter parameter of the OData query. NULL if none specified |
||
| 145 | * @param mixed|null $orderBy sorted order if we want to get the data in some specific order |
||
| 146 | * @param integer|null $top number of records which need to be retrieved |
||
| 147 | * @param integer|null $skip number of records which need to be skipped |
||
| 148 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
||
| 149 | * |
||
| 150 | * @return QueryResult |
||
| 151 | * |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function getRelatedResourceSet( |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Gets a related entity instance from an entity set identified by a key |
||
| 182 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
| 183 | * |
||
| 184 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 185 | * @param object $sourceEntityInstance The source entity instance. |
||
| 186 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
| 187 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
| 188 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
| 189 | * |
||
| 190 | * @return Model|null Returns entity instance if found else null |
||
| 191 | */ |
||
| 192 | public function getResourceFromRelatedResourceSet( |
||
| 208 | |||
| 209 | /** |
||
| 210 | 3 | * Get related resource for a resource |
|
| 211 | 3 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
|
| 212 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
| 213 | 3 | * |
|
| 214 | 3 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
|
| 215 | 3 | * @param object $sourceEntityInstance The source entity instance. |
|
| 216 | 3 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
|
| 217 | 3 | * @param ResourceProperty $targetProperty The navigation property to fetch |
|
| 218 | 3 | * |
|
| 219 | 3 | * @return object|null The related resource if found else null |
|
| 220 | */ |
||
| 221 | 3 | public function getRelatedResourceReference( |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Updates a resource |
||
| 240 | * |
||
| 241 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 242 | * @param object $sourceEntityInstance The source entity instance |
||
| 243 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
| 244 | * @param object $data The New data for the entity instance. |
||
| 245 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
| 246 | * |
||
| 247 | * @return object|null The new resource value if it is assignable or throw exception for null. |
||
| 248 | */ |
||
| 249 | public function updateResource( |
||
| 261 | /** |
||
| 262 | * Delete resource from a resource set. |
||
| 263 | * @param ResourceSet $sourceResourceSet |
||
| 264 | * @param object $sourceEntityInstance |
||
| 265 | * |
||
| 266 | * return bool true if resources sucessfully deteled, otherwise false. |
||
| 267 | */ |
||
| 268 | public function deleteResource( |
||
| 292 | 1 | /** |
|
| 293 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
| 294 | * @param object $sourceEntityInstance The source entity instance |
||
| 295 | * @param object $data The New data for the entity instance. |
||
| 296 | * |
||
| 297 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
||
| 298 | */ |
||
| 299 | 1 | public function createResourceforResourceSet( |
|
| 309 | 1 | ||
| 310 | /** |
||
| 311 | * @param $sourceEntityInstance |
||
| 312 | * @param $data |
||
| 313 | * @param $class |
||
| 314 | * @param string $verb |
||
| 315 | * @return array|mixed |
||
| 316 | * @throws ODataException |
||
| 317 | * @throws InvalidOperationException |
||
| 318 | 1 | */ |
|
| 319 | private function createUpdateDeleteCore($sourceEntityInstance, $data, $class, $verb) |
||
| 357 | |||
| 358 | 1 | /** |
|
| 359 | * Puts an entity instance to entity set identified by a key. |
||
| 360 | * |
||
| 361 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
||
| 362 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
||
| 363 | * @param $data |
||
| 364 | * |
||
| 365 | * @return bool|null Returns result of executing query |
||
| 366 | */ |
||
| 367 | public function putResource( |
||
| 375 | 3 | ||
| 376 | /** |
||
| 377 | * @param ResourceSet $sourceResourceSet |
||
| 378 | 3 | * @param $data |
|
| 379 | 3 | * @param $verb |
|
| 380 | * @param Model|null $source |
||
| 381 | * @return mixed |
||
| 382 | * @throws InvalidOperationException |
||
| 383 | * @throws ODataException |
||
| 384 | */ |
||
| 385 | 3 | private function createUpdateCoreWrapper(ResourceSet $sourceResourceSet, $data, $verb, Model $source = null) |
|
| 406 | 3 | ||
| 407 | 2 | /** |
|
| 408 | 2 | * @param $sourceEntityInstance |
|
| 409 | 2 | * @param $data |
|
| 410 | 2 | * @param $paramList |
|
| 411 | 2 | * @return array |
|
| 412 | */ |
||
| 413 | private function createUpdateDeleteProcessInput(Model $sourceEntityInstance, $data, $paramList) |
||
| 434 | 3 | ||
| 435 | /** |
||
| 436 | * @param $result |
||
| 437 | * @return array|mixed |
||
| 438 | * @throws ODataException |
||
| 439 | 3 | */ |
|
| 440 | private function createUpdateDeleteProcessOutput($result) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param $sourceEntityInstance |
||
| 463 | * @return mixed|null|\object[] |
||
| 464 | */ |
||
| 465 | private function unpackSourceEntity($sourceEntityInstance) |
||
| 474 | } |
||
| 475 |