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 Calendar implements CalendarInterface, JsonLdSerializableInterface, SerializableInterface |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var CalendarType |
||
19 | */ |
||
20 | protected $type = null; |
||
21 | |||
22 | /** |
||
23 | * @var DateTimeInterface |
||
24 | */ |
||
25 | protected $startDate = null; |
||
26 | |||
27 | /** |
||
28 | * @var DateTimeInterface |
||
29 | */ |
||
30 | protected $endDate = null; |
||
31 | |||
32 | /** |
||
33 | * @var Timestamp[] |
||
34 | */ |
||
35 | protected $timestamps = array(); |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $openingHours = array(); |
||
41 | |||
42 | /** |
||
43 | * @param CalendarType $type |
||
44 | * @param DateTimeInterface|null $startDate |
||
45 | * @param DateTimeInterface|null $endDate |
||
46 | * @param Timestamp[] $timestamps |
||
47 | * @param array $openingHours |
||
48 | */ |
||
49 | public function __construct( |
||
50 | CalendarType $type, |
||
51 | DateTimeInterface $startDate = null, |
||
52 | DateTimeInterface $endDate = null, |
||
53 | array $timestamps = array(), |
||
54 | array $openingHours = array() |
||
55 | ) { |
||
56 | View Code Duplication | if (($type->is(CalendarType::MULTIPLE()) || $type->is(CalendarType::SINGLE())) && empty($startDate)) { |
|
|
|||
57 | throw new \UnexpectedValueException('Start date can not be empty for calendar type: ' . $type . '.'); |
||
58 | } |
||
59 | |||
60 | View Code Duplication | if ($type->is(CalendarType::SINGLE()) && empty($endDate)) { |
|
61 | throw new \UnexpectedValueException('End date can not be empty for calendar type: ' . $type . '.'); |
||
62 | } |
||
63 | |||
64 | if ($type->is(CalendarType::PERIODIC()) && (empty($startDate) || empty($endDate))) { |
||
65 | throw new \UnexpectedValueException('A period should have a start- and end-date.'); |
||
66 | } |
||
67 | |||
68 | $this->type = $type->toNative(); |
||
69 | $this->startDate = $startDate; |
||
70 | $this->endDate = $endDate; |
||
71 | $this->timestamps = $timestamps; |
||
72 | $this->openingHours = $openingHours; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function getType() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function serialize() |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public static function deserialize(array $data) |
||
125 | |||
126 | /** |
||
127 | * This deserialization function takes into account old data that might be missing a timezone. |
||
128 | * It will fall back to creating a DateTime object and assume Brussels. |
||
129 | * If this still fails an error will be thrown. |
||
130 | * |
||
131 | * @param $dateTimeData |
||
132 | * @return DateTime |
||
133 | * |
||
134 | * @throws InvalidArgumentException |
||
135 | */ |
||
136 | private static function deserializeDateTime($dateTimeData) |
||
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | public function getStartDate() |
||
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | public function getEndDate() |
||
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | public function getOpeningHours() |
||
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | */ |
||
178 | public function getTimestamps() |
||
182 | |||
183 | /** |
||
184 | * Return the jsonLD version of a calendar. |
||
185 | */ |
||
186 | public function toJsonLd() |
||
222 | } |
||
223 |
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.