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 |
||
24 | class ResponsiveImageExtension extends \Twig_Extension |
||
25 | { |
||
26 | /** |
||
27 | * @var StyleManager |
||
28 | */ |
||
29 | private $styleManager; |
||
30 | /** |
||
31 | * @var UrlBuilder |
||
32 | */ |
||
33 | private $urlBuilder; |
||
34 | /** |
||
35 | * @var ImageManager |
||
36 | */ |
||
37 | private $imageManager; |
||
38 | |||
39 | /** |
||
40 | * ResponsiveImageExtension constructor. |
||
41 | * |
||
42 | * @param StyleManager $styleManager |
||
43 | * @param UrlBuilder $urlBuilder |
||
44 | * @param ImageManager|null $imageManager |
||
45 | */ |
||
46 | public function __construct(StyleManager $styleManager, UrlBuilder $urlBuilder, ImageManager $imageManager = null) |
||
52 | |||
53 | /** |
||
54 | * @return array |
||
55 | */ |
||
56 | public function getFunctions() |
||
97 | |||
98 | /** |
||
99 | * @param \Twig_Environment $environment |
||
100 | * @param ResponsiveImageInterface $image |
||
101 | * @param $pictureSetName |
||
102 | * @param $selector |
||
103 | * |
||
104 | * @return mixed|string |
||
105 | */ |
||
106 | View Code Duplication | public function generateBackgroundImage(\Twig_Environment $environment, ResponsiveImageInterface $image, $pictureSetName, $selector) |
|
121 | |||
122 | /** |
||
123 | * @param \Twig_Environment $environment |
||
124 | * @param ResponsiveImageInterface $image |
||
125 | * @param $pictureSetName |
||
126 | * @param bool $generate |
||
127 | * |
||
128 | * @return mixed|string |
||
129 | */ |
||
130 | View Code Duplication | public function generatePictureImage(\Twig_Environment $environment, ResponsiveImageInterface $image, $pictureSetName, $generate = false) |
|
144 | |||
145 | /** |
||
146 | * @internal |
||
147 | * |
||
148 | * @param array $data |
||
149 | * @param array $keys |
||
150 | */ |
||
151 | private function convertPathsToUrls(array &$data, array $keys) |
||
166 | |||
167 | /** |
||
168 | * Some path strings could contain more than one path, eg 1x and 2x paths. |
||
169 | * This function breaks them up and converts to the full public url. |
||
170 | * |
||
171 | * @param $path |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | private function createPublicFileUrl($path) |
||
184 | |||
185 | /** |
||
186 | * @param \Twig_Environment $environment |
||
187 | * @param ResponsiveImageInterface $image |
||
188 | * @param $pictureSetName |
||
189 | * @param bool $generate |
||
190 | * |
||
191 | * @return mixed|string |
||
192 | */ |
||
193 | public function generateSizesImage(\Twig_Environment $environment, ResponsiveImageInterface $image, $pictureSetName, $generate = false) |
||
212 | |||
213 | /** |
||
214 | * @param \Twig_Environment $environment |
||
215 | * @param ResponsiveImageInterface $image |
||
216 | * @param null $styleName |
||
217 | * @param bool $generate |
||
218 | * |
||
219 | * @return mixed|string |
||
220 | */ |
||
221 | public function generateStyledImage(\Twig_Environment $environment, ResponsiveImageInterface $image, $styleName = null, $generate = false) |
||
227 | |||
228 | /** |
||
229 | * @param \Twig_Environment $environment |
||
230 | * @param ResponsiveImageInterface $image |
||
231 | * @param string $width |
||
232 | * @param null $height |
||
233 | * |
||
234 | * @return mixed|string |
||
235 | */ |
||
236 | View Code Duplication | public function cropImage(\Twig_Environment $environment, ResponsiveImageInterface $image, $width = '', $height = null) |
|
246 | |||
247 | /** |
||
248 | * @param \Twig_Environment $environment |
||
249 | * @param ResponsiveImageInterface $image |
||
250 | * @param string $width |
||
251 | * @param string $height |
||
252 | * |
||
253 | * @return mixed|string |
||
254 | */ |
||
255 | View Code Duplication | public function scaleImage(\Twig_Environment $environment, ResponsiveImageInterface $image, $width = '', $height = '') |
|
265 | |||
266 | /** |
||
267 | * @param \Twig_Environment $environment |
||
268 | * @param ResponsiveImageInterface $image |
||
269 | * @param null $styleName |
||
270 | * |
||
271 | * @return mixed|string |
||
272 | */ |
||
273 | protected function renderImage(\Twig_Environment $environment, ResponsiveImageInterface $image, $styleName = null) |
||
293 | |||
294 | /** |
||
295 | * @return string |
||
296 | */ |
||
297 | public function getName() |
||
301 | } |
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.