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 | trait QueueItemTrait |
||
15 | { |
||
16 | /** |
||
17 | * The queue ID. |
||
18 | * |
||
19 | * @var mixed $queueId |
||
20 | */ |
||
21 | private $queueId; |
||
22 | |||
23 | /** |
||
24 | * Whether the item has been processed. |
||
25 | * |
||
26 | * @var boolean $processed |
||
27 | */ |
||
28 | private $processed = false; |
||
29 | |||
30 | /** |
||
31 | * When the item was queued. |
||
32 | * |
||
33 | * @var DateTimeInterface|null $queuedDate |
||
34 | */ |
||
35 | private $queuedDate; |
||
36 | |||
37 | /** |
||
38 | * When the item should be processed. |
||
39 | * |
||
40 | * The date/time at which this queue item job should be ran. |
||
41 | * If NULL, 0, or a past date/time, then it should be performed immediately. |
||
42 | * |
||
43 | * @var DateTimeInterface|null $processingDate |
||
44 | */ |
||
45 | private $processingDate; |
||
46 | |||
47 | /** |
||
48 | * When the item was processed. |
||
49 | * |
||
50 | * @var DateTimeInterface|null $processedDate |
||
51 | */ |
||
52 | private $processedDate; |
||
53 | |||
54 | /** |
||
55 | * When the item should be considered expired. |
||
56 | * |
||
57 | * The date/time at which this queue item job should expire and be prevented to fire. |
||
58 | * If NULL, 0, or a future date/time, then it should be allowed to be performed. |
||
59 | * |
||
60 | * @var DateTimeInterface|null $lexpiryDate |
||
61 | */ |
||
62 | private $expiryDate; |
||
63 | |||
64 | /** |
||
65 | * Default amount of seconds before expiry after processing date. |
||
66 | * |
||
67 | * @var integer $defaultExpiryInSeconde |
||
68 | */ |
||
69 | private $defaultExpiryInSeconds = 86400; |
||
70 | |||
71 | /** |
||
72 | * The status of the queue item. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | private $status; |
||
77 | |||
78 | /** |
||
79 | * Process the item. |
||
80 | * |
||
81 | * @param callable $alwaysCallback An optional callback routine executed after the item is processed. |
||
82 | * @param callable $successCallback An optional callback routine executed when the item is resolved. |
||
83 | * @param callable $failureCallback An optional callback routine executed when the item is rejected. |
||
84 | * @return boolean|null Returns TRUE i this item was successfully processed, |
||
85 | * FALSE on failure or if an error occurs, NULL if this item is already processed. |
||
86 | */ |
||
87 | abstract public function process( |
||
92 | |||
93 | /** |
||
94 | * @param Exception $e Exception to log. |
||
95 | * @return void |
||
96 | */ |
||
97 | protected function logProcessingException(Exception $e) |
||
108 | |||
109 | /** |
||
110 | * Set the queue item's data. |
||
111 | * |
||
112 | * @param array $data The queue item data to set. |
||
113 | * @return self |
||
114 | */ |
||
115 | public function setQueueItemData(array $data) |
||
135 | |||
136 | /** |
||
137 | * Set the queue's ID. |
||
138 | * |
||
139 | * @param mixed $id The unique queue identifier. |
||
140 | * @return self |
||
141 | */ |
||
142 | public function setQueueId($id) |
||
147 | |||
148 | /** |
||
149 | * Get the queue's ID. |
||
150 | * |
||
151 | * @return mixed |
||
152 | */ |
||
153 | public function queueId() |
||
157 | |||
158 | /** |
||
159 | * Set the item's processed status. |
||
160 | * |
||
161 | * @param boolean $processed Whether the item has been processed. |
||
162 | * @return self |
||
163 | */ |
||
164 | public function setProcessed($processed) |
||
169 | |||
170 | /** |
||
171 | * Determine if the item has been processed. |
||
172 | * |
||
173 | * @return boolean |
||
174 | */ |
||
175 | public function processed() |
||
179 | |||
180 | /** |
||
181 | * Set the date/time the item was queued at. |
||
182 | * |
||
183 | * @param null|string|DateTimeInterface $ts A date/time string or object. |
||
184 | * @throws InvalidArgumentException If the date/time is invalid. |
||
185 | * @return self |
||
186 | */ |
||
187 | View Code Duplication | public function setQueuedDate($ts) |
|
214 | |||
215 | /** |
||
216 | * Retrieve the date/time the item was queued at. |
||
217 | * |
||
218 | * @return null|\DateTimeInterface |
||
219 | */ |
||
220 | public function queuedDate() |
||
224 | |||
225 | /** |
||
226 | * Set the date/time the item should be processed at. |
||
227 | * |
||
228 | * @param null|string|\DateTimeInterface $ts A date/time string or object. |
||
229 | * @throws InvalidArgumentException If the date/time is invalid. |
||
230 | * @return self |
||
231 | */ |
||
232 | View Code Duplication | public function setProcessingDate($ts) |
|
259 | |||
260 | /** |
||
261 | * Retrieve the date/time the item should be processed at. |
||
262 | * |
||
263 | * @return null|\DateTimeInterface |
||
264 | */ |
||
265 | public function processingDate() |
||
269 | |||
270 | /** |
||
271 | * Set the date/time the item was processed at. |
||
272 | * |
||
273 | * @param null|string|\DateTimeInterface $ts A date/time string or object. |
||
274 | * @throws InvalidArgumentException If the date/time is invalid. |
||
275 | * @return self |
||
276 | */ |
||
277 | View Code Duplication | public function setProcessedDate($ts) |
|
304 | |||
305 | /** |
||
306 | * Retrieve the date/time the item was processed at. |
||
307 | * |
||
308 | * @return null|\DateTimeInterface |
||
309 | */ |
||
310 | public function processedDate() |
||
314 | |||
315 | /** |
||
316 | * Retrieve the date/time the item should be expired at. |
||
317 | * |
||
318 | * @return null|\DateTimeInterface |
||
319 | */ |
||
320 | public function expiryDate() |
||
324 | |||
325 | /** |
||
326 | * Set the date/time the item will expire at. |
||
327 | * |
||
328 | * @param null|string|\DateTimeInterface $ts A date/time string or object. |
||
329 | * @throws InvalidArgumentException If the date/time is invalid. |
||
330 | * @return self |
||
331 | */ |
||
332 | View Code Duplication | public function setExpiryDate($ts) |
|
359 | |||
360 | /** |
||
361 | * @return string |
||
362 | */ |
||
363 | public function status() |
||
367 | |||
368 | /** |
||
369 | * @param string $status Status for QueueItemTrait. |
||
370 | * @return self |
||
371 | */ |
||
372 | public function setStatus($status) |
||
378 | |||
379 | /** |
||
380 | * Hook called before saving the item. |
||
381 | * |
||
382 | * Presets the item as _to-be_ processed and queued now. |
||
383 | * |
||
384 | * @return self |
||
385 | */ |
||
386 | protected function preSaveQueueItem() |
||
397 | |||
398 | /** |
||
399 | * Generate an expiry date based on the default interval and the scheduled processing date. |
||
400 | * |
||
401 | * @return self |
||
402 | */ |
||
403 | protected function generateExpiry() |
||
410 | } |
||
411 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: