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 |
||
10 | class MonologHandlerFactory |
||
11 | { |
||
12 | /** |
||
13 | * Returns an instance of \Monolog\Handler\HandlerInterface |
||
14 | * @param string $name Then name of the handler you want to create |
||
15 | * @param array $config An array of config values to use |
||
16 | * @param string $title |
||
17 | * @return \Monolog\Handler\HandlerInterface |
||
18 | */ |
||
19 | 39 | public static function create($name, array $config = [], $title = null){ |
|
20 | 39 | $handler = call_user_func([MonologHandlerFactory::class,$name], $config, $title); |
|
21 | |||
22 | // Keep newline characters |
||
23 | 39 | $format = ['mail', 'mailgun']; |
|
24 | |||
25 | 39 | if(in_array($name, $format)) { |
|
26 | 15 | $handler->setFormatter(new LineFormatter(null, null, true)); |
|
27 | 15 | } |
|
28 | |||
29 | 39 | if ($handler instanceof \Monolog\Handler\NativeMailerHandler) { |
|
30 | 3 | $handler->setContentType('text/html'); |
|
31 | 3 | } |
|
32 | |||
33 | 39 | return $handler; |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * Returns a PushoverHandler |
||
38 | * @param array $config An array of config values to use |
||
39 | * @param string $title The title/subject to use |
||
40 | * @return \Monolog\Handler\PushoverHandler |
||
41 | */ |
||
42 | 12 | protected static function pushover(array $config = [], $title = null){ |
|
70 | |||
71 | /** |
||
72 | * Returns a PushoverHandler |
||
73 | * @param array $config An array of config values to use |
||
74 | * @param string $title The title/subject to use |
||
75 | * @return \Tylercd100\Monolog\Handler\MailgunHandler |
||
76 | */ |
||
77 | 6 | protected static function mailgun(array $config = [], $title = null){ |
|
101 | |||
102 | /** |
||
103 | * Returns a FlowdockHandler |
||
104 | * @param array $config An array of config values to use |
||
105 | * @param string $title The title/subject to use |
||
106 | * @return \Monolog\Handler\FlowdockHandler |
||
107 | */ |
||
108 | 3 | View Code Duplication | protected static function flowdock(array $config = [], $title = null){ |
121 | |||
122 | /** |
||
123 | * Returns a FleepHookHandler |
||
124 | * @param array $config An array of config values to use |
||
125 | * @param string $title The title/subject to use |
||
126 | * @return \Monolog\Handler\FleepHookHandler |
||
127 | */ |
||
128 | 3 | View Code Duplication | protected static function fleephook(array $config = [], $title = null){ |
141 | |||
142 | /** |
||
143 | * Returns a PlivoHandler |
||
144 | * @param array $config An array of config values to use |
||
145 | * @param string $title The title/subject to use |
||
146 | * @return \Tylercd100\Monolog\Handler\PlivoHandler |
||
147 | */ |
||
148 | 3 | View Code Duplication | protected static function plivo(array $config = [], $title = null){ |
172 | |||
173 | /** |
||
174 | * Returns a TwilioHandler |
||
175 | * @param array $config An array of config values to use |
||
176 | * @param string $title The title/subject to use |
||
177 | * @return \Tylercd100\Monolog\Handler\TwilioHandler |
||
178 | */ |
||
179 | 3 | View Code Duplication | protected static function twilio(array $config = [], $title = null){ |
203 | |||
204 | /** |
||
205 | * Returns a RavenHandler |
||
206 | * @param array $config An array of config values to use |
||
207 | * @param string $title The title/subject to use |
||
208 | * @return \Monolog\Handler\RavenHandler |
||
209 | */ |
||
210 | 3 | protected static function raven(array $config = [], $title = null){ |
|
225 | |||
226 | /** |
||
227 | * Returns a SlackHandler |
||
228 | * @param array $config An array of config values to use |
||
229 | * @param string $title The title/subject to use |
||
230 | * @return \Monolog\Handler\SlackHandler |
||
231 | */ |
||
232 | 9 | protected static function slack(array $config = [], $title = null){ |
|
256 | |||
257 | /** |
||
258 | * Returns a HipChatHandler |
||
259 | * @param array $config An array of config values to use |
||
260 | * @param string $title The title/subject to use |
||
261 | * @return \Monolog\Handler\HipChatHandler |
||
262 | */ |
||
263 | 9 | protected static function hipchat(array $config = [], $title = null){ |
|
289 | |||
290 | /** |
||
291 | * Creates Mail Monolog Handler |
||
292 | * @param array $config An array of config values to use |
||
293 | * @param string $title The title/subject to use |
||
294 | * @return \Monolog\Handler\MailHandler |
||
295 | */ |
||
296 | 12 | protected static function mail(array $config = [], $title = null) |
|
304 | |||
305 | /** |
||
306 | * Creates Mail Monolog Handler |
||
307 | * @param array $config An array of config values to use |
||
308 | * @param string $title The title/subject to use |
||
309 | * @return \Monolog\Handler\SwiftMailerHandler |
||
310 | */ |
||
311 | 9 | protected static function swiftMail(array $config, $title = null) |
|
329 | |||
330 | /** |
||
331 | * Creates Mail Monolog Handler |
||
332 | * @param array $config An array of config values to use |
||
333 | * @param string $title The title/subject to use |
||
334 | * @return \Monolog\Handler\NativeMailerHandler |
||
335 | */ |
||
336 | 3 | protected static function nativeMail(array $config, $title = null) |
|
357 | } |
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.