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 |
||
10 | class MediaExtension extends \Twig_Extension |
||
11 | { |
||
12 | /** |
||
13 | * @var ProviderPool |
||
14 | */ |
||
15 | private $providerPool; |
||
16 | |||
17 | /** |
||
18 | * @var UrlGenerator |
||
19 | */ |
||
20 | private $urlGenerator; |
||
21 | |||
22 | /** |
||
23 | * @param ProviderPool $providerPool |
||
24 | * @param UrlGenerator $urlGenerator |
||
25 | */ |
||
26 | public function __construct(ProviderPool $providerPool, UrlGenerator $urlGenerator) |
||
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | public function getName() |
||
39 | |||
40 | /** |
||
41 | * @return array |
||
42 | */ |
||
43 | public function getFilters() |
||
66 | |||
67 | /** |
||
68 | * @param \Twig_Environment $environment |
||
69 | * @param MediaInterface $media |
||
70 | * @param $width |
||
71 | * @param $height |
||
72 | * @param array $parameters |
||
73 | * @return string |
||
74 | */ |
||
75 | public function media( |
||
92 | |||
93 | /** |
||
94 | * @param \Twig_Environment $environment |
||
95 | * @param MediaInterface $media |
||
96 | * @param $width |
||
97 | * @param $height |
||
98 | * @param array $parameters |
||
99 | * @param null $routeName |
||
100 | * @return string |
||
101 | */ |
||
102 | View Code Duplication | public function mediaImage( |
|
125 | |||
126 | /** |
||
127 | * @param \Twig_Environment $environment |
||
128 | * @param MediaInterface $media |
||
129 | * @param $width |
||
130 | * @param $height |
||
131 | * @param array $parameters |
||
132 | * @param null $routeName |
||
133 | * @return string |
||
134 | */ |
||
135 | View Code Duplication | public function mediaDownload( |
|
158 | |||
159 | /** |
||
160 | * @param MediaInterface $media |
||
161 | * @return ProviderInterface |
||
162 | */ |
||
163 | private function getProviderByMedia(MediaInterface $media) |
||
167 | } |
||
168 |
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.