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 Config |
||
25 | { |
||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $formatters = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $handlers = []; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $processors = []; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $channels = []; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $defaultChannel = ''; |
||
50 | |||
51 | /** |
||
52 | * Config constructor. |
||
53 | * |
||
54 | * @param array $config |
||
55 | */ |
||
56 | public function __construct(array $config) |
||
60 | |||
61 | /** |
||
62 | * @param string $name |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | public function getChannel(string $name) |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getDefaultChannel() |
||
89 | |||
90 | /** |
||
91 | * @param array $names |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | protected function getFormatters(array $names) |
||
101 | |||
102 | /** |
||
103 | * @param array $names |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | protected function getHandlers(array $names) |
||
113 | |||
114 | /** |
||
115 | * @param array $names |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function getProcessors(array $names) |
||
125 | |||
126 | /** |
||
127 | * @param string $formatterId |
||
128 | * |
||
129 | * @return \Monolog\Formatter\FormatterInterface |
||
130 | */ |
||
131 | protected function getFormatter(string $formatterId) |
||
139 | |||
140 | /** |
||
141 | * @param string $handlerId |
||
142 | * |
||
143 | * @return \Monolog\Handler\HandlerInterface |
||
144 | */ |
||
145 | protected function getHandler(string $handlerId) |
||
153 | |||
154 | /** |
||
155 | * @param string $processorId |
||
156 | * |
||
157 | * @return \Monolog\Handler\HandlerInterface |
||
158 | */ |
||
159 | protected function getProcessor(string $processorId) |
||
167 | |||
168 | /** |
||
169 | * @param array $config |
||
170 | */ |
||
171 | protected function parse(array $config) |
||
179 | |||
180 | /** |
||
181 | * @param array $formatters |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | View Code Duplication | protected function formatFormatters(array $formatters = []) |
|
198 | |||
199 | /** |
||
200 | * @param array $handlers |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function formatHandlers(array $handlers = []) |
||
237 | |||
238 | /** |
||
239 | * @param array $processors |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | View Code Duplication | protected function formatProcessors(array $processors = []) |
|
260 | |||
261 | /** |
||
262 | * @param array $channels |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | protected function formatChannels(array $channels = []) |
||
285 | } |
||
286 |
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.