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 ArticleToIdTransformer implements DataTransformerInterface |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * @var ArticleProviderInterface |
||
27 | */ |
||
28 | private $articleProvider; |
||
29 | |||
30 | /** |
||
31 | * ArticleToIdTransformer constructor. |
||
32 | * |
||
33 | * @param ArticleProviderInterface $articleProvider |
||
34 | */ |
||
35 | 16 | public function __construct(ArticleProviderInterface $articleProvider) |
|
39 | |||
40 | /** |
||
41 | * Transforms an object (article) to a string (id). |
||
42 | * |
||
43 | * @param ArticleInterface|string $article |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 16 | public function transform($article) |
|
59 | |||
60 | /** |
||
61 | * Transforms an id to an object (article). |
||
62 | * |
||
63 | * @param string $articleId |
||
64 | * |
||
65 | * @return ArticleInterface|void |
||
66 | * |
||
67 | * @throws TransformationFailedException if object (article) is not found |
||
68 | */ |
||
69 | 15 | public function reverseTransform($articleId) |
|
86 | } |
||
87 |
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.