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 namespace Limoncello\Flute\Http\Traits; |
||
| 32 | trait FluteRoutesTrait |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @param GroupInterface $group |
||
| 36 | * @param string $resourceName |
||
| 37 | * @param string $controllerClass |
||
| 38 | * |
||
| 39 | * @return GroupInterface |
||
| 40 | */ |
||
| 41 | 1 | protected static function resource( |
|
| 42 | GroupInterface $group, |
||
| 43 | string $resourceName, |
||
| 44 | string $controllerClass |
||
| 45 | ): GroupInterface { |
||
| 46 | 1 | assert(class_exists($controllerClass) === true); |
|
| 47 | |||
| 48 | 1 | $indexSlug = '/{' . CI::ROUTE_KEY_INDEX . '}'; |
|
| 49 | 1 | $params = function (string $method) use ($resourceName): array { |
|
| 50 | 1 | return [RouteInterface::PARAM_NAME => static::routeName($resourceName, $method)]; |
|
| 51 | 1 | }; |
|
| 52 | 1 | $handler = function (string $method) use ($controllerClass): array { |
|
| 53 | 1 | return [$controllerClass, $method]; |
|
| 54 | 1 | }; |
|
| 55 | |||
| 56 | // if the class implements any of CRUD methods a corresponding route will be added |
||
| 57 | 1 | $classInterfaces = class_implements($controllerClass); |
|
| 58 | 1 | View Code Duplication | if (in_array(ControllerIndexInterface::class, $classInterfaces) === true) { |
|
|
|||
| 59 | 1 | $group->get($resourceName, $handler(CI::METHOD_INDEX), $params(CI::METHOD_INDEX)); |
|
| 60 | } |
||
| 61 | 1 | if (in_array(ControllerCreateInterface::class, $classInterfaces) === true) { |
|
| 62 | 1 | $group->post($resourceName, $handler(CI::METHOD_CREATE), $params(CI::METHOD_CREATE)); |
|
| 63 | } |
||
| 64 | 1 | if (in_array(ControllerReadInterface::class, $classInterfaces) === true) { |
|
| 65 | 1 | $group->get($resourceName . $indexSlug, $handler(CI::METHOD_READ), $params(CI::METHOD_READ)); |
|
| 66 | } |
||
| 67 | 1 | if (in_array(ControllerUpdateInterface::class, $classInterfaces) === true) { |
|
| 68 | 1 | $group->patch($resourceName . $indexSlug, $handler(CI::METHOD_UPDATE), $params(CI::METHOD_UPDATE)); |
|
| 69 | } |
||
| 70 | 1 | if (in_array(ControllerDeleteInterface::class, $classInterfaces) === true) { |
|
| 71 | 1 | $group->delete($resourceName . $indexSlug, $handler(CI::METHOD_DELETE), $params(CI::METHOD_DELETE)); |
|
| 72 | } |
||
| 73 | |||
| 74 | 1 | return $group; |
|
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param GroupInterface $group |
||
| 79 | * @param string $subUri |
||
| 80 | * @param string $controllerClass |
||
| 81 | * |
||
| 82 | * @return GroupInterface |
||
| 83 | */ |
||
| 84 | 1 | protected static function controller(GroupInterface $group, string $subUri, string $controllerClass): GroupInterface |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param GroupInterface $group |
||
| 120 | * @param string $resourceName |
||
| 121 | * @param string $relationshipName |
||
| 122 | * @param string $controllerClass |
||
| 123 | * @param string $selfGetMethod |
||
| 124 | * |
||
| 125 | * @return GroupInterface |
||
| 126 | */ |
||
| 127 | 1 | protected static function relationship( |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $name |
||
| 149 | * @param string $method |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 2 | protected static function routeName(string $name, string $method): string |
|
| 159 | } |
||
| 160 |
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.