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 |
||
19 | class BlogOverviewAction |
||
20 | { |
||
21 | /** |
||
22 | * Template renderer |
||
23 | * |
||
24 | * @var TemplateRendererInterface |
||
25 | */ |
||
26 | private $templateRenderer; |
||
27 | |||
28 | /** |
||
29 | * Content creator |
||
30 | * |
||
31 | * @var ContentCreator |
||
32 | */ |
||
33 | private $contentCreator; |
||
34 | |||
35 | /** |
||
36 | * Markdown component parser |
||
37 | * |
||
38 | * @var MarkdownDocumentParser |
||
39 | */ |
||
40 | private $markdownDocumentParser; |
||
41 | |||
42 | /** |
||
43 | * Markdown cms config |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $markdownCmsConfig; |
||
48 | |||
49 | /** |
||
50 | * Constructor |
||
51 | * |
||
52 | * @param TemplateRendererInterface $templateRenderer |
||
53 | * @param ContentCreator $contentCreator |
||
54 | * @param MarkdownDocumentParser $markdownDocumentParser |
||
55 | * @param array $markdownCmsConfig |
||
56 | */ |
||
57 | View Code Duplication | public function __construct(TemplateRendererInterface $templateRenderer, ContentCreator $contentCreator, |
|
65 | |||
66 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) |
||
92 | |||
93 | /** |
||
94 | * Get template renderer |
||
95 | * |
||
96 | * @return TemplateRendererInterface |
||
97 | */ |
||
98 | public function getTemplateRenderer() : TemplateRendererInterface |
||
102 | |||
103 | /** |
||
104 | * Get content creator |
||
105 | * |
||
106 | * @return ContentCreator |
||
107 | */ |
||
108 | public function getContentCreator() : ContentCreator |
||
112 | |||
113 | /** |
||
114 | * Get markdown document parser |
||
115 | * |
||
116 | * @return MarkdownDocumentParser |
||
117 | */ |
||
118 | public function getMarkdownDocumentParser() : MarkdownDocumentParser |
||
122 | |||
123 | /** |
||
124 | * Get markdown cms config |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public function getMarkdownCmsConfig() : array |
||
132 | } |
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.