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 |
||
26 | class Config |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $formatters = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $handlers = []; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $processors = []; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $channels = []; |
||
47 | |||
48 | /** |
||
49 | * Config constructor. |
||
50 | * |
||
51 | * @param array $config |
||
52 | */ |
||
53 | public function __construct(array $config) |
||
57 | |||
58 | /** |
||
59 | * @param string $name |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getChannel(string $name) |
||
84 | |||
85 | /** |
||
86 | * @return array |
||
87 | */ |
||
88 | public function getFormatters() |
||
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | public function getHandlers() |
||
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getProcessors() |
||
108 | |||
109 | /** |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getChannels() |
||
116 | |||
117 | /** |
||
118 | * @param string $formatterId |
||
119 | * |
||
120 | * @return \Monolog\Formatter\FormatterInterface |
||
121 | */ |
||
122 | protected function resolveFormatter(string $formatterId) |
||
130 | |||
131 | /** |
||
132 | * @param string $handlerId |
||
133 | * |
||
134 | * @return \Monolog\Handler\HandlerInterface |
||
135 | */ |
||
136 | protected function resolveHandler(string $handlerId) |
||
152 | |||
153 | /** |
||
154 | * @param string $processorId |
||
155 | * |
||
156 | * @return \Monolog\Handler\HandlerInterface |
||
157 | */ |
||
158 | protected function resolveProcessor(string $processorId) |
||
166 | |||
167 | /** |
||
168 | * @param string $option |
||
169 | * @param string $name |
||
170 | * |
||
171 | * @return object |
||
172 | */ |
||
173 | protected function makeInstance($option, $name) |
||
178 | |||
179 | /** |
||
180 | * @param array $config |
||
181 | */ |
||
182 | protected function parse(array $config): void |
||
196 | |||
197 | /** |
||
198 | * @param array $formatters |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | protected function formatFormatters(array $formatters = []) |
||
216 | |||
217 | /** |
||
218 | * @param array $handlers |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function formatHandlers(array $handlers = []) |
||
248 | |||
249 | /** |
||
250 | * @param array $processors |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | protected function formatProcessors(array $processors = []) |
||
272 | |||
273 | /** |
||
274 | * @param array $channels |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | protected function formatChannels(array $channels = []) |
||
301 | } |
||
302 |
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.