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 Schema 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 Schema, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Schema; |
||
| 32 | abstract class Schema extends SchemaProvider implements SchemaInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var ModelSchemeInfoInterface |
||
| 36 | */ |
||
| 37 | private $modelSchemes; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param FactoryInterface $factory |
||
| 41 | * @param ModelSchemeInfoInterface $modelSchemes |
||
| 42 | */ |
||
| 43 | 22 | public function __construct(FactoryInterface $factory, ModelSchemeInfoInterface $modelSchemes) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @inheritdoc |
||
| 55 | */ |
||
| 56 | 11 | public static function getAttributeMapping(string $jsonName): string |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @inheritdoc |
||
| 63 | */ |
||
| 64 | 13 | public static function getRelationshipMapping(string $jsonName): string |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | 14 | View Code Duplication | public static function hasAttributeMapping(string $jsonName): bool |
| 80 | |||
| 81 | /** |
||
| 82 | * @inheritdoc |
||
| 83 | */ |
||
| 84 | 11 | View Code Duplication | public static function hasRelationshipMapping(string $jsonName): bool |
| 92 | |||
| 93 | /** |
||
| 94 | * @inheritdoc |
||
| 95 | */ |
||
| 96 | 17 | public function getAttributes($model) |
|
| 113 | |||
| 114 | /** @noinspection PhpMissingParentCallCommonInspection |
||
| 115 | * @inheritdoc |
||
| 116 | * |
||
| 117 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 118 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 119 | */ |
||
| 120 | 18 | public function getRelationships($model, $isPrimary, array $includeRelationships) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @return ModelSchemeInfoInterface |
||
| 197 | */ |
||
| 198 | 18 | protected function getModelSchemes(): ModelSchemeInfoInterface |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param PaginatedDataInterface $data |
||
| 205 | * @param string $uri |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | 2 | protected function getRelationshipDescription(PaginatedDataInterface $data, string $uri): array |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param mixed $model |
||
| 242 | * @param string $jsonRelationship |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | 18 | protected function getRelationshipLinkRepresentation($model, string $jsonRelationship): array |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param mixed $model |
||
| 256 | * @param string $jsonRelationship |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | 18 | protected function getRelationshipLinks($model, string $jsonRelationship): array |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Gets excludes from default 'show `self` link in relationships' rule. |
||
| 275 | * |
||
| 276 | * @return array Should be in ['jsonRelationship' => true] format. |
||
| 277 | */ |
||
| 278 | 8 | protected function getExcludesFromDefaultShowSelfLinkInRelationships(): array |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Gets excludes from default 'show `related` link in relationships' rule. |
||
| 285 | * |
||
| 286 | * @return array Should be in ['jsonRelationship' => true] format. |
||
| 287 | */ |
||
| 288 | 8 | protected function getExcludesFromDefaultShowRelatedLinkInRelationships(): array |
|
| 292 | |||
| 293 | /** |
||
| 294 | * If `self` link should be shown in relationships by default. |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | 18 | protected function isShowSelfLinkInRelationships(): bool |
|
| 302 | |||
| 303 | /** |
||
| 304 | * If `related` link should be shown in relationships by default. |
||
| 305 | * |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | 18 | protected function isShowRelatedLinkInRelationships(): bool |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param mixed $model |
||
| 315 | * @param string $name |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | 18 | private function hasRelationship($model, string $name): bool |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $jsonRelationship |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | 18 | private function showSelfInRelationship(string $jsonRelationship): bool |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $jsonRelationship |
||
| 342 | * |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | 18 | private function showRelatedInRelationship(string $jsonRelationship): bool |
|
| 353 | } |
||
| 354 |
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.