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 |
||
17 | final class CreateResponse implements ApiResponse |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $message; |
||
23 | |||
24 | /** |
||
25 | * @var Domain |
||
26 | */ |
||
27 | private $domain; |
||
28 | |||
29 | /** |
||
30 | * @var DnsRecord[] |
||
31 | */ |
||
32 | private $inboundDnsRecords; |
||
33 | |||
34 | /** |
||
35 | * @var DnsRecord[] |
||
36 | */ |
||
37 | private $outboundDnsRecords; |
||
38 | |||
39 | /** |
||
40 | * @param array $data |
||
41 | * |
||
42 | * @return self |
||
43 | */ |
||
44 | public static function create(array $data) |
||
73 | |||
74 | /** |
||
75 | * @param Domain $domainInfo |
||
76 | * @param DnsRecord[] $rxRecords |
||
77 | * @param DnsRecord[] $txRecords |
||
78 | * @param string $message |
||
79 | */ |
||
80 | private function __construct(Domain $domainInfo, array $rxRecords, array $txRecords, $message) |
||
87 | |||
88 | /** |
||
89 | * @return Domain |
||
90 | */ |
||
91 | public function getDomain() |
||
95 | |||
96 | /** |
||
97 | * @return DnsRecord[] |
||
98 | */ |
||
99 | public function getInboundDNSRecords() |
||
103 | |||
104 | /** |
||
105 | * @return DnsRecord[] |
||
106 | */ |
||
107 | public function getOutboundDNSRecords() |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getMessage() |
||
119 | } |
||
120 |
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.