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 |
||
30 | class AutoConfig { |
||
31 | |||
32 | /** @var Logger */ |
||
33 | private $logger; |
||
34 | |||
35 | /** @var string */ |
||
36 | private $userId; |
||
37 | |||
38 | /** @var ICrypto */ |
||
39 | private $crypto; |
||
40 | |||
41 | /** @var IspDb */ |
||
42 | private $ispDb; |
||
43 | |||
44 | /** @var MxRecord */ |
||
45 | private $mxRecord; |
||
46 | |||
47 | /** @var ImapConnectivityTester */ |
||
48 | private $imapConnectivityTester; |
||
49 | |||
50 | /** @var ImapServerDetector */ |
||
51 | private $imapServerDetector; |
||
52 | |||
53 | /** @var ImapConnector */ |
||
54 | private $imapConnector; |
||
55 | |||
56 | /** @var SmtpConnectivityTester */ |
||
57 | private $smtpConnectivityTester; |
||
58 | |||
59 | /** @var SmtpServerDetector */ |
||
60 | private $smtpServerDetector; |
||
61 | |||
62 | /** |
||
63 | * |
||
64 | * @param Logger $logger |
||
65 | * @param string $UserId |
||
66 | * @param IspDb $ispDb |
||
67 | * @param MxRecord $mxRecord |
||
68 | * @param ImapConnectivityTester $imapTester |
||
69 | * @param ImapServerDetector $imapDetector |
||
70 | * @param SmtpConnectivityTester $smtpTester |
||
71 | * @param SmtpServerDetector $smtpDetector |
||
72 | * @param ImapConnector $imapConnector |
||
73 | * @param ICrypto $crypto |
||
74 | */ |
||
75 | public function __construct(Logger $logger, $UserId, |
||
76 | IspDb $ispDb, MxRecord $mxRecord, |
||
77 | ImapConnectivityTester $imapTester, ImapServerDetector $imapDetector, |
||
78 | SmtpConnectivityTester $smtpTester, SmtpServerDetector $smtpDetector, |
||
79 | ImapConnector $imapConnector, ICrypto $crypto) { |
||
80 | $this->logger = $logger; |
||
81 | $this->userId = $UserId; |
||
82 | $this->crypto = $crypto; |
||
83 | $this->ispDb = $ispDb; |
||
84 | $this->mxRecord = $mxRecord; |
||
85 | $this->imapConnectivityTester = $imapTester; |
||
86 | $this->imapServerDetector = $imapDetector; |
||
87 | $this->imapConnector = $imapConnector; |
||
88 | $this->smtpConnectivityTester = $smtpTester; |
||
89 | $this->smtpServerDetector = $smtpDetector; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $email |
||
94 | * @param string $password |
||
95 | * @param string $name |
||
96 | * @return null|MailAccount |
||
97 | */ |
||
98 | public function createAutoDetected($email, $password, $name) { |
||
178 | |||
179 | /** |
||
180 | * @param $email |
||
181 | * @param $password |
||
182 | * @param $name |
||
183 | * @return null|MailAccount |
||
184 | */ |
||
185 | private function detectImapAndSmtp($email, $password, $name) { |
||
195 | |||
196 | } |
||
197 |
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.