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 |
||
14 | class Address implements SerializableInterface, JsonLdSerializableInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $countryCode; |
||
20 | |||
21 | /** |
||
22 | * @var Locality |
||
23 | */ |
||
24 | protected $locality; |
||
25 | |||
26 | /** |
||
27 | * @var PostalCode |
||
28 | */ |
||
29 | protected $postalCode; |
||
30 | |||
31 | /** |
||
32 | * @var Street |
||
33 | */ |
||
34 | protected $streetAddress; |
||
35 | |||
36 | public function __construct( |
||
47 | |||
48 | /** |
||
49 | * @return Country |
||
50 | */ |
||
51 | public function getCountry() |
||
55 | |||
56 | /** |
||
57 | * @return Locality |
||
58 | */ |
||
59 | public function getLocality() |
||
63 | |||
64 | /** |
||
65 | * @return PostalCode |
||
66 | */ |
||
67 | public function getPostalCode() |
||
71 | |||
72 | /** |
||
73 | * @return Street |
||
74 | */ |
||
75 | public function getStreetAddress() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | View Code Duplication | public function serialize() |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public static function deserialize(array $data) |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | View Code Duplication | public function toJsonLd() |
|
118 | |||
119 | /** |
||
120 | * @param Address $otherAddress |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function sameAs(Address $otherAddress) |
||
127 | |||
128 | /** |
||
129 | * @param Udb3ModelAddress $address |
||
130 | * @return self |
||
131 | */ |
||
132 | public static function fromUdb3Model(Udb3ModelAddress $address) |
||
141 | } |
||
142 |
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.