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 |
||
10 | class BoardingPass implements \JsonSerializable, TravelClassInterface |
||
11 | { |
||
12 | use ValidatorTrait; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $passengerName; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $pnrNumber; |
||
23 | |||
24 | /** |
||
25 | * @var string|null |
||
26 | */ |
||
27 | protected $travelClass; |
||
28 | |||
29 | /** |
||
30 | * @var string|null |
||
31 | */ |
||
32 | protected $seat; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $auxiliaryFields = []; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $secondaryFields = []; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $logoImageUrl; |
||
48 | |||
49 | /** |
||
50 | * @var string|null |
||
51 | */ |
||
52 | protected $headerImageUrl; |
||
53 | |||
54 | /** |
||
55 | * @var string|null |
||
56 | */ |
||
57 | protected $headerTextField; |
||
58 | |||
59 | /** |
||
60 | * @var string|null |
||
61 | */ |
||
62 | protected $qrCode; |
||
63 | |||
64 | /** |
||
65 | * @var string|null |
||
66 | */ |
||
67 | protected $barcodeImageUrl; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $aboveBarcodeImageUrl; |
||
73 | |||
74 | /** |
||
75 | * @var FlightInfo; |
||
76 | */ |
||
77 | protected $flightInfo; |
||
78 | |||
79 | /** |
||
80 | * BoardingPass constructor. |
||
81 | * |
||
82 | * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo $flightInfo |
||
83 | */ |
||
84 | 5 | public function __construct( |
|
100 | |||
101 | /** |
||
102 | * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo $flightInfo |
||
103 | * |
||
104 | * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass |
||
105 | */ |
||
106 | 5 | public static function create( |
|
116 | |||
117 | /** |
||
118 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
119 | * |
||
120 | * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass |
||
121 | */ |
||
122 | 2 | public function setTravelClass(string $travelClass): self |
|
130 | |||
131 | /** |
||
132 | * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass |
||
133 | */ |
||
134 | 1 | public function setSeat(string $seat): self |
|
140 | |||
141 | /** |
||
142 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
143 | * |
||
144 | * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass |
||
145 | */ |
||
146 | 2 | public function addAuxiliaryFields(string $label, string $value): self |
|
154 | |||
155 | /** |
||
156 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
157 | * |
||
158 | * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass |
||
159 | */ |
||
160 | 2 | public function addSecondaryFields(string $label, string $value): self |
|
168 | |||
169 | /** |
||
170 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
171 | * |
||
172 | * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass |
||
173 | */ |
||
174 | 1 | public function setHeaderImageUrl(string $headerImageUrl): self |
|
182 | |||
183 | /** |
||
184 | * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass |
||
185 | */ |
||
186 | 1 | public function setHeaderTextField(string $headerTextField): self |
|
192 | |||
193 | 3 | private function setLabelValue(string $label, string $value): array |
|
200 | |||
201 | 5 | private function setCode(string $code): void |
|
210 | |||
211 | /** |
||
212 | * @throws \Kerox\Messenger\Exception\MessengerException |
||
213 | */ |
||
214 | 2 | View Code Duplication | public function isValidTravelClass(string $travelClass): void |
221 | |||
222 | 2 | public function getAllowedTravelClass(): array |
|
230 | |||
231 | 2 | public function toArray(): array |
|
251 | |||
252 | 2 | public function jsonSerialize(): array |
|
256 | } |
||
257 |
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.