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 |
||
12 | class MailboxCheckValidation implements EmailValidation |
||
13 | { |
||
14 | const END_OF_LINE = "\r\n"; |
||
15 | |||
16 | /** |
||
17 | * @var InvalidEmail |
||
18 | */ |
||
19 | private $error; |
||
20 | |||
21 | /** |
||
22 | * @var Warning[] |
||
23 | */ |
||
24 | private $warnings = []; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $lastResponseCode; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | private $port = 25; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $timeout = 10; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $fromEmail = '[email protected]'; |
||
45 | |||
46 | /** |
||
47 | * MailboxCheckValidation constructor. |
||
48 | */ |
||
49 | 4 | public function __construct() |
|
55 | |||
56 | /** |
||
57 | * @inheritDoc |
||
58 | */ |
||
59 | 1 | public function getError() |
|
63 | |||
64 | /** |
||
65 | * @inheritDoc |
||
66 | */ |
||
67 | 1 | public function getWarnings() |
|
71 | |||
72 | /** |
||
73 | * @return int |
||
74 | */ |
||
75 | public function getLastResponseCode() |
||
79 | |||
80 | /** |
||
81 | * @return int |
||
82 | */ |
||
83 | public function getPort() |
||
87 | |||
88 | /** |
||
89 | * @param int $port |
||
90 | */ |
||
91 | public function setPort($port) |
||
95 | |||
96 | /** |
||
97 | * @return int |
||
98 | */ |
||
99 | public function getTimeout() |
||
103 | |||
104 | /** |
||
105 | * @param int $timeout |
||
106 | */ |
||
107 | public function setTimeout($timeout) |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getFromEmail() |
||
119 | |||
120 | /** |
||
121 | * @param string $fromEmail |
||
122 | */ |
||
123 | public function setFromEmail($fromEmail) |
||
127 | |||
128 | /** |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | 4 | public function isValid($email, EmailLexer $emailLexer) |
|
148 | |||
149 | /** |
||
150 | * @param string $email |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | 4 | protected function getMXHosts($email) |
|
172 | |||
173 | /** |
||
174 | * @param string $hostname |
||
175 | * @param int $port |
||
176 | * @param int $timeout |
||
177 | * @param string $fromEmail |
||
178 | * @param string $toEmail |
||
179 | * @return bool |
||
180 | */ |
||
181 | 2 | protected function checkMailbox($hostname, $port, $timeout, $fromEmail, $toEmail) |
|
216 | |||
217 | /** |
||
218 | * @param resource $socket |
||
219 | * @param int $expectedCode |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | 2 | private function assertResponse($socket, $expectedCode) |
|
240 | } |
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.