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 |
||
7 | class SmsapiSmsMessage extends SmsapiMessage |
||
8 | { |
||
9 | /** |
||
10 | * @param string|null $content |
||
11 | */ |
||
12 | 6 | public function __construct($content = null) |
|
13 | { |
||
14 | 6 | ExceptionFactory::assertArgumentTypes(1, __METHOD__, ['string', 'NULL'], $content); |
|
15 | 6 | if ($content !== null) { |
|
16 | 2 | $this->data['content'] = $content; |
|
17 | 2 | } |
|
18 | 6 | } |
|
19 | |||
20 | /** |
||
21 | * @param string $content |
||
22 | * @return self |
||
23 | */ |
||
24 | 5 | public function content($content) |
|
31 | |||
32 | /** |
||
33 | * @param string $template |
||
34 | * @return self |
||
35 | */ |
||
36 | 5 | public function template($template) |
|
43 | |||
44 | /** |
||
45 | * @param string $from |
||
46 | * @return self |
||
47 | */ |
||
48 | 5 | public function from($from) |
|
55 | |||
56 | /** |
||
57 | * @param bool $fast |
||
58 | * @return self |
||
59 | */ |
||
60 | 6 | View Code Duplication | public function fast($fast = true) |
|
|||
61 | { |
||
62 | 6 | ExceptionFactory::assertArgumentType(1, __METHOD__, 'boolean', $fast); |
|
63 | 2 | $this->data['fast'] = $fast; |
|
64 | |||
65 | 2 | return $this; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param bool $flash |
||
70 | * @return self |
||
71 | */ |
||
72 | 7 | View Code Duplication | public function flash($flash = true) |
73 | { |
||
74 | 7 | ExceptionFactory::assertArgumentType(1, __METHOD__, 'boolean', $flash); |
|
75 | 3 | $this->data['flash'] = $flash; |
|
76 | |||
77 | 3 | return $this; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $encoding |
||
82 | * @return self |
||
83 | */ |
||
84 | 5 | public function encoding($encoding) |
|
91 | |||
92 | /** |
||
93 | * @param bool $normalize |
||
94 | * @return self |
||
95 | */ |
||
96 | 7 | View Code Duplication | public function normalize($normalize = true) |
103 | |||
104 | /** |
||
105 | * @param bool $nounicode |
||
106 | * @return self |
||
107 | */ |
||
108 | 7 | View Code Duplication | public function nounicode($nounicode = true) |
115 | |||
116 | /** |
||
117 | * @param bool $single |
||
118 | * @return self |
||
119 | */ |
||
120 | 6 | View Code Duplication | public function single($single = true) |
127 | } |
||
128 |
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.