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 |
||
22 | class FilterService |
||
23 | { |
||
24 | /** |
||
25 | * @var DataManager |
||
26 | */ |
||
27 | private $dataManager; |
||
28 | |||
29 | /** |
||
30 | * @var FilterManager |
||
31 | */ |
||
32 | private $filterManager; |
||
33 | |||
34 | /** |
||
35 | * @var CacheManager |
||
36 | */ |
||
37 | private $cacheManager; |
||
38 | |||
39 | /** |
||
40 | * @var LoggerInterface |
||
41 | */ |
||
42 | private $logger; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $webpGenerate; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $webpQuality; |
||
53 | |||
54 | /** |
||
55 | * @param DataManager $dataManager |
||
56 | * @param FilterManager $filterManager |
||
57 | * @param CacheManager $cacheManager |
||
58 | * @param bool $webpGenerate |
||
59 | * @param int $webpQuality |
||
60 | * @param LoggerInterface $logger |
||
61 | */ |
||
62 | public function __construct( |
||
77 | |||
78 | /** |
||
79 | * @param string $path |
||
80 | * @param string $filter |
||
81 | */ |
||
82 | public function bustCache($path, $filter) |
||
90 | |||
91 | /** |
||
92 | * @param string $path |
||
93 | * @param string $filter |
||
94 | * @param string $resolver |
||
95 | * @param bool $webp |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getUrlOfFilteredImage($path, $filter, $resolver = null, $webp = false) |
||
129 | |||
130 | /** |
||
131 | * @param string $path |
||
132 | * @param string $filter |
||
133 | * @param array $runtimeFilters |
||
134 | * @param string|null $resolver |
||
135 | * @param bool $webp |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getUrlOfFilteredImageWithRuntimeFilters( |
||
176 | |||
177 | /** |
||
178 | * @param string $path |
||
179 | * @param string $filter |
||
180 | * @param array $runtimeFilters |
||
181 | * |
||
182 | * @throws NonExistingFilterException |
||
183 | * |
||
184 | * @return BinaryInterface |
||
185 | */ |
||
186 | private function createFilteredBinary($path, $filter, array $runtimeFilters = []) |
||
202 | |||
203 | /** |
||
204 | * @param string $path |
||
205 | * @param string $filter |
||
206 | * @param array $runtimeFilters |
||
207 | * |
||
208 | * @throws NonExistingFilterException |
||
209 | * |
||
210 | * @return BinaryInterface |
||
211 | */ |
||
212 | private function createFilteredWebpBinary($path, $filter, array $runtimeFilters = []) |
||
230 | } |
||
231 |
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.