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 | class FilterManager |
||
24 | { |
||
25 | /** |
||
26 | * @var FilterConfiguration |
||
27 | */ |
||
28 | protected $filterConfig; |
||
29 | |||
30 | /** |
||
31 | * @var ImagineInterface |
||
32 | */ |
||
33 | protected $imagine; |
||
34 | |||
35 | /** |
||
36 | * @var MimeTypeGuesserInterface |
||
37 | */ |
||
38 | protected $mimeTypeGuesser; |
||
39 | |||
40 | /** |
||
41 | * @var LoaderInterface[] |
||
42 | */ |
||
43 | protected $loaders = []; |
||
44 | |||
45 | /** |
||
46 | * @var PostProcessorInterface[] |
||
47 | */ |
||
48 | protected $postProcessors = []; |
||
49 | |||
50 | /** |
||
51 | * @param FilterConfiguration $filterConfig |
||
52 | * @param ImagineInterface $imagine |
||
53 | * @param MimeTypeGuesserInterface $mimeTypeGuesser |
||
54 | */ |
||
55 | public function __construct(FilterConfiguration $filterConfig, ImagineInterface $imagine, MimeTypeGuesserInterface $mimeTypeGuesser) |
||
61 | |||
62 | /** |
||
63 | * Adds a loader to handle the given filter. |
||
64 | * |
||
65 | * @param string $filter |
||
66 | * @param LoaderInterface $loader |
||
67 | */ |
||
68 | public function addLoader(string $filter, LoaderInterface $loader): void |
||
72 | |||
73 | /** |
||
74 | * Adds a post-processor to handle binaries. |
||
75 | * |
||
76 | * @param string $name |
||
77 | * @param PostProcessorInterface $postProcessor |
||
78 | */ |
||
79 | public function addPostProcessor(string $name, PostProcessorInterface $postProcessor): void |
||
83 | |||
84 | /** |
||
85 | * @return FilterConfiguration |
||
86 | */ |
||
87 | public function getFilterConfiguration(): FilterConfiguration |
||
91 | |||
92 | /** |
||
93 | * @param BinaryInterface $binary |
||
94 | * @param array $config |
||
95 | * |
||
96 | * @throws \InvalidArgumentException |
||
97 | * |
||
98 | * @return BinaryInterface |
||
99 | */ |
||
100 | public function apply(BinaryInterface $binary, array $config): BinaryInterface |
||
109 | |||
110 | /** |
||
111 | * @param BinaryInterface $binary |
||
112 | * @param array $config |
||
113 | * |
||
114 | * @return BinaryInterface |
||
115 | */ |
||
116 | public function applyFilters(BinaryInterface $binary, array $config): BinaryInterface |
||
135 | |||
136 | /** |
||
137 | * Apply the provided filter set on the given binary. |
||
138 | * |
||
139 | * @param BinaryInterface $binary |
||
140 | * @param string $filter |
||
141 | * @param array $runtimeConfig |
||
142 | * |
||
143 | * @throws \InvalidArgumentException |
||
144 | * |
||
145 | * @return BinaryInterface |
||
146 | */ |
||
147 | public function applyFilter(BinaryInterface $binary, $filter, array $runtimeConfig = []) |
||
156 | |||
157 | /** |
||
158 | * @param BinaryInterface $binary |
||
159 | * @param array $config |
||
160 | * |
||
161 | * @throws \InvalidArgumentException |
||
162 | * |
||
163 | * @return BinaryInterface |
||
164 | */ |
||
165 | public function applyPostProcessors(BinaryInterface $binary, array $config): BinaryInterface |
||
173 | |||
174 | /** |
||
175 | * @param BinaryInterface $binary |
||
176 | * @param ImageInterface $image |
||
177 | * @param array $config |
||
178 | * |
||
179 | * @return BinaryInterface |
||
180 | */ |
||
181 | private function exportConfiguredImageBinary(BinaryInterface $binary, ImageInterface $image, array $config): BinaryInterface |
||
212 | |||
213 | /** |
||
214 | * @param array $filters |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | View Code Duplication | private function sanitizeFilters(array $filters): array |
|
232 | |||
233 | /** |
||
234 | * @param array $processors |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | View Code Duplication | private function sanitizePostProcessors(array $processors): array |
|
252 | |||
253 | /** |
||
254 | * We are done with the image object so we can destruct the this because imagick keeps consuming memory if we don't. |
||
255 | * See https://github.com/liip/LiipImagineBundle/pull/682 |
||
256 | * |
||
257 | * @param ImageInterface $image |
||
258 | */ |
||
259 | private function destroyImage(ImageInterface $image): void |
||
265 | } |
||
266 |
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.