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 |
||
5 | class SmsManager |
||
6 | { |
||
7 | /** |
||
8 | * Sms Configuration. |
||
9 | * |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $config; |
||
13 | |||
14 | /** |
||
15 | * Sms Driver Settings. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $settings; |
||
20 | |||
21 | /** |
||
22 | * Sms Driver Name. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $driver; |
||
27 | |||
28 | /** |
||
29 | * @var SmsBuilder |
||
30 | */ |
||
31 | protected $builder; |
||
32 | |||
33 | /** |
||
34 | * SmsManager constructor. |
||
35 | * |
||
36 | * @param array $config |
||
37 | */ |
||
38 | public function __construct($config) |
||
44 | |||
45 | /** |
||
46 | * @param string|array $recipients |
||
47 | * @return self |
||
48 | */ |
||
49 | public function to($recipients) |
||
55 | |||
56 | /** |
||
57 | * Change the driver on the fly. |
||
58 | * |
||
59 | * @param $driver |
||
60 | * @return $this |
||
61 | * @throws \Exception |
||
62 | */ |
||
63 | View Code Duplication | public function via($driver) |
|
72 | |||
73 | /** |
||
74 | * Send message. |
||
75 | * |
||
76 | * @param $message |
||
77 | * @param $callback |
||
78 | * @return mixed |
||
79 | * @throws \Exception |
||
80 | */ |
||
81 | public function send($message, $callback = null) |
||
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public function dispatch() |
||
114 | |||
115 | /** |
||
116 | * @param SmsBuilder $builder |
||
117 | * @return self |
||
118 | */ |
||
119 | protected function setBuilder(SmsBuilder $builder) |
||
125 | |||
126 | /** |
||
127 | * Generate driver instance. |
||
128 | * |
||
129 | * @return mixed |
||
130 | */ |
||
131 | View Code Duplication | protected function getDriverInstance() |
|
138 | |||
139 | /** |
||
140 | * Validate Parameters before sending. |
||
141 | * |
||
142 | * @throws \Exception |
||
143 | */ |
||
144 | protected function validateDriver() |
||
164 | } |
||
165 |
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.