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 |
||
23 | View Code Duplication | final class RouteToIdTransformer implements DataTransformerInterface |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * @var RouteProviderInterface |
||
27 | */ |
||
28 | private $routeProvider; |
||
29 | |||
30 | /** |
||
31 | * RouteToIdTransformer constructor. |
||
32 | * |
||
33 | * @param RouteProviderInterface $routeProvider |
||
34 | */ |
||
35 | 9 | public function __construct(RouteProviderInterface $routeProvider) |
|
39 | |||
40 | /** |
||
41 | * Transforms an object (route) to a string (id). |
||
42 | * |
||
43 | * @param RouteInterface|string $route |
||
44 | * |
||
45 | * @return string |
||
46 | * |
||
47 | * @throws TransformationFailedException if object (route) is of wrong type |
||
48 | */ |
||
49 | 9 | public function transform($route) |
|
61 | |||
62 | /** |
||
63 | * Transforms an id to an object (route). |
||
64 | * |
||
65 | * @param string $routeId |
||
66 | * |
||
67 | * @return RouteInterface |
||
68 | * |
||
69 | * @throws TransformationFailedException if object (route) is not found |
||
70 | */ |
||
71 | 4 | public function reverseTransform($routeId) |
|
88 | } |
||
89 |
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.