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 |
||
13 | class BodyOptions extends AbstractOptions |
||
14 | { |
||
15 | /** |
||
16 | * @var bool |
||
17 | */ |
||
18 | private $useTemplate = false; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $content = ''; |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $charset = MailServiceInterface::DEFAULT_CHARSET; |
||
27 | /** |
||
28 | * @var TemplateOptions |
||
29 | */ |
||
30 | private $template; |
||
31 | |||
32 | /** |
||
33 | * @return boolean |
||
34 | */ |
||
35 | 12 | public function getUseTemplate() |
|
39 | |||
40 | /** |
||
41 | * @param boolean $useTemplate |
||
42 | * @return $this |
||
43 | */ |
||
44 | 2 | public function setUseTemplate($useTemplate) |
|
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | 10 | public function getContent() |
|
57 | |||
58 | /** |
||
59 | * @param string $content |
||
60 | * @return $this |
||
61 | */ |
||
62 | 2 | public function setContent($content) |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 13 | public function getCharset() |
|
75 | |||
76 | /** |
||
77 | * @param string $charset |
||
78 | * @return $this |
||
79 | */ |
||
80 | 1 | public function setCharset($charset) |
|
85 | |||
86 | /** |
||
87 | * @return TemplateOptions |
||
88 | */ |
||
89 | 4 | public function getTemplate() |
|
97 | |||
98 | /** |
||
99 | * @param array|TemplateOptions $template |
||
100 | * @return $this |
||
101 | * @throws \AcMailer\Exception\InvalidArgumentException |
||
102 | */ |
||
103 | 5 | View Code Duplication | public function setTemplate($template) |
118 | } |
||
119 |
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.