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 LaravelReadQuery 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 LaravelReadQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class LaravelReadQuery |
||
| 23 | { |
||
| 24 | protected $auth; |
||
| 25 | |||
| 26 | public function __construct(AuthInterface $auth = null) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Gets collection of entities belongs to an entity set |
||
| 33 | * IE: http://host/EntitySet |
||
| 34 | * http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value |
||
| 35 | * |
||
| 36 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
| 37 | * @param ResourceSet $resourceSet The entity set containing the entities to fetch |
||
| 38 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
| 39 | * @param null|InternalOrderByInfo $orderBy sorted order if we want to get the data in some specific order |
||
| 40 | * @param int $top number of records which need to be retrieved |
||
| 41 | * @param int $skip number of records which need to be skipped |
||
| 42 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
||
| 43 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
| 44 | * |
||
| 45 | * @return QueryResult |
||
| 46 | */ |
||
| 47 | public function getResourceSet( |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get related resource set for a resource |
||
| 175 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection |
||
| 176 | * http://host/EntitySet?$expand=NavigationPropertyToCollection |
||
| 177 | * |
||
| 178 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
| 179 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 180 | * @param object $sourceEntityInstance The source entity instance. |
||
| 181 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
| 182 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
| 183 | * @param FilterInfo $filter represents the $filter parameter of the OData query. NULL if no $filter specified |
||
| 184 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
| 185 | * @param int $top number of records which need to be skip |
||
| 186 | * @param String $skip value indicating what records to skip |
||
| 187 | * |
||
| 188 | * @return QueryResult |
||
| 189 | * |
||
| 190 | */ |
||
| 191 | public function getRelatedResourceSet( |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets an entity instance from an entity set identified by a key |
||
| 228 | * IE: http://host/EntitySet(1L) |
||
| 229 | * http://host/EntitySet(KeyA=2L,KeyB='someValue') |
||
| 230 | * |
||
| 231 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
| 232 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
| 233 | * |
||
| 234 | * @return object|null Returns entity instance if found else null |
||
| 235 | */ |
||
| 236 | public function getResourceFromResourceSet( |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Common method for getResourceFromRelatedResourceSet() and getResourceFromResourceSet() |
||
| 246 | * @param ResourceSet|null $resourceSet |
||
| 247 | * @param KeyDescriptor|null $keyDescriptor |
||
| 248 | * @param Model|Relation|null $sourceEntityInstance Starting point of query |
||
| 249 | */ |
||
| 250 | public function getResource( |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get related resource for a resource |
||
| 284 | * IE: http://host/EntitySet(1L)/NavigationPropertyToSingleEntity |
||
| 285 | * http://host/EntitySet?$expand=NavigationPropertyToSingleEntity |
||
| 286 | * |
||
| 287 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 288 | * @param object $sourceEntityInstance The source entity instance. |
||
| 289 | * @param ResourceSet $targetResourceSet The entity set containing the entity pointed to by the navigation property |
||
| 290 | * @param ResourceProperty $targetProperty The navigation property to fetch |
||
| 291 | * |
||
| 292 | * @return object|null The related resource if found else null |
||
| 293 | */ |
||
| 294 | View Code Duplication | public function getRelatedResourceReference( |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Gets a related entity instance from an entity set identified by a key |
||
| 313 | * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection(33) |
||
| 314 | * |
||
| 315 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 316 | * @param object $sourceEntityInstance The source entity instance. |
||
| 317 | * @param ResourceSet $targetResourceSet The entity set containing the entity to fetch |
||
| 318 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
| 319 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
| 320 | * |
||
| 321 | * @return object|null Returns entity instance if found else null |
||
| 322 | */ |
||
| 323 | View Code Duplication | public function getResourceFromRelatedResourceSet( |
|
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * @param ResourceSet $resourceSet |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | protected function getSourceEntityInstance(ResourceSet $resourceSet) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param Model|Relation|null $source |
||
| 350 | */ |
||
| 351 | protected function checkSourceInstance($source) |
||
| 357 | |||
| 358 | protected function getAuth() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param $sourceEntityInstance |
||
| 365 | * @throws ODataException |
||
| 366 | */ |
||
| 367 | private function checkAuth($sourceEntityInstance, $checkInstance = null) |
||
| 378 | } |
||
| 379 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.