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 |
||
15 | class EmailParser |
||
16 | { |
||
17 | const EMAIL_MAX_LENGTH = 254; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $warnings = []; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $domainPart = ''; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $localPart = ''; |
||
33 | /** |
||
34 | * @var EmailLexer |
||
35 | */ |
||
36 | protected $lexer; |
||
37 | |||
38 | /** |
||
39 | * @var LocalPart |
||
40 | */ |
||
41 | protected $localPartParser; |
||
42 | |||
43 | /** |
||
44 | * @var DomainPart |
||
45 | */ |
||
46 | protected $domainPartParser; |
||
47 | |||
48 | public function __construct(EmailLexer $lexer) |
||
52 | |||
53 | /** |
||
54 | * @param string $str |
||
55 | * @return Result |
||
56 | */ |
||
57 | public function parse($str) : Result |
||
85 | |||
86 | View Code Duplication | private function processLocalPart() : Result |
|
97 | |||
98 | View Code Duplication | private function processDomainPart() : Result |
|
110 | |||
111 | /** |
||
112 | * @return Warning\Warning[] |
||
113 | */ |
||
114 | public function getWarnings() : array |
||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getDomainPart() : string |
||
126 | |||
127 | public function getLocalPart() : string |
||
131 | |||
132 | /** |
||
133 | * @return bool |
||
134 | */ |
||
135 | protected function hasAtToken() : bool |
||
145 | |||
146 | /** |
||
147 | * @param string $localPart |
||
148 | * @param string $parsedDomainPart |
||
149 | */ |
||
150 | protected function addLongEmailWarning($localPart, $parsedDomainPart) : void |
||
156 | } |
||
157 |
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.