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 ShowResponse implements ApiResponse |
||
18 | { |
||
19 | /** |
||
20 | * @var Domain |
||
21 | */ |
||
22 | private $domain; |
||
23 | |||
24 | /** |
||
25 | * @var DnsRecord[] |
||
26 | */ |
||
27 | private $inboundDnsRecords; |
||
28 | |||
29 | /** |
||
30 | * @var DnsRecord[] |
||
31 | */ |
||
32 | private $outboundDnsRecords; |
||
33 | |||
34 | /** |
||
35 | * @param array $data |
||
36 | * |
||
37 | * @return self |
||
38 | */ |
||
39 | public static function create(array $data) |
||
63 | |||
64 | /** |
||
65 | * @param Domain $domainInfo |
||
66 | * @param DnsRecord[] $rxRecords |
||
67 | * @param DnsRecord[] $txRecords |
||
68 | */ |
||
69 | private function __construct(Domain $domainInfo, array $rxRecords, array $txRecords) |
||
75 | |||
76 | /** |
||
77 | * @return Domain |
||
78 | */ |
||
79 | public function getDomain() |
||
83 | |||
84 | /** |
||
85 | * @return DnsRecord[] |
||
86 | */ |
||
87 | public function getInboundDNSRecords() |
||
91 | |||
92 | /** |
||
93 | * @return DnsRecord[] |
||
94 | */ |
||
95 | public function getOutboundDNSRecords() |
||
99 | } |
||
100 |
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.