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:
Complex classes like Auditable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Auditable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | trait Auditable |
||
28 | { |
||
29 | /** |
||
30 | * Auditable attributes excluded from the Audit. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $excludedAttributes = []; |
||
35 | |||
36 | /** |
||
37 | * Audit event name. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $auditEvent; |
||
42 | |||
43 | /** |
||
44 | * Is auditing disabled? |
||
45 | * |
||
46 | * @var bool |
||
47 | */ |
||
48 | public static $auditingDisabled = false; |
||
49 | |||
50 | /** |
||
51 | * Auditable boot logic. |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | 79 | public static function bootAuditable() |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 12 | public function audits(): MorphMany |
|
72 | |||
73 | /** |
||
74 | * Resolve the Auditable attributes to exclude from the Audit. |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 43 | protected function resolveAuditExclusions() |
|
114 | |||
115 | /** |
||
116 | * Get the old/new attributes of a retrieved event. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | 2 | protected function getRetrievedEventAttributes(): array |
|
130 | |||
131 | /** |
||
132 | * Get the old/new attributes of a created event. |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 37 | View Code Duplication | protected function getCreatedEventAttributes(): array |
151 | |||
152 | /** |
||
153 | * Get the old/new attributes of an updated event. |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 4 | protected function getUpdatedEventAttributes(): array |
|
174 | |||
175 | /** |
||
176 | * Get the old/new attributes of a deleted event. |
||
177 | * |
||
178 | * @return array |
||
179 | */ |
||
180 | 4 | View Code Duplication | protected function getDeletedEventAttributes(): array |
195 | |||
196 | /** |
||
197 | * Get the old/new attributes of a restored event. |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 2 | protected function getRestoredEventAttributes(): array |
|
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | 54 | public function readyForAuditing(): bool |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 48 | public function toAudit(): array |
|
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | 38 | public function transformAudit(array $data): array |
|
267 | |||
268 | /** |
||
269 | * Resolve the User. |
||
270 | * |
||
271 | * @throws AuditingException |
||
272 | * |
||
273 | * @return mixed|null |
||
274 | */ |
||
275 | 43 | View Code Duplication | protected function resolveUser() |
285 | |||
286 | /** |
||
287 | * Resolve the URL. |
||
288 | * |
||
289 | * @throws AuditingException |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | 42 | View Code Duplication | protected function resolveUrl(): string |
303 | |||
304 | /** |
||
305 | * Resolve the IP Address. |
||
306 | * |
||
307 | * @throws AuditingException |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | 41 | View Code Duplication | protected function resolveIpAddress(): string |
321 | |||
322 | /** |
||
323 | * Resolve the User Agent. |
||
324 | * |
||
325 | * @throws AuditingException |
||
326 | * |
||
327 | * @return string|null |
||
328 | */ |
||
329 | 40 | View Code Duplication | protected function resolveUserAgent() |
339 | |||
340 | /** |
||
341 | * Determine if an attribute is eligible for auditing. |
||
342 | * |
||
343 | * @param string $attribute |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | 38 | protected function isAttributeAuditable(string $attribute): bool |
|
360 | |||
361 | /** |
||
362 | * Determine whether an event is auditable. |
||
363 | * |
||
364 | * @param string $event |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | 55 | protected function isEventAuditable($event): bool |
|
372 | |||
373 | /** |
||
374 | * Attribute getter method resolver. |
||
375 | * |
||
376 | * @param string $event |
||
377 | * |
||
378 | * @return string|null |
||
379 | */ |
||
380 | 55 | protected function resolveAttributeGetter($event) |
|
392 | |||
393 | /** |
||
394 | * {@inheritdoc} |
||
395 | */ |
||
396 | 55 | public function setAuditEvent(string $event): Contracts\Auditable |
|
402 | |||
403 | /** |
||
404 | * {@inheritdoc} |
||
405 | */ |
||
406 | 2 | public function getAuditEvent() |
|
410 | |||
411 | /** |
||
412 | * {@inheritdoc} |
||
413 | */ |
||
414 | 58 | public function getAuditEvents(): array |
|
423 | |||
424 | /** |
||
425 | * Disable Auditing. |
||
426 | * |
||
427 | * @return void |
||
428 | */ |
||
429 | 1 | public static function disableAuditing() |
|
433 | |||
434 | /** |
||
435 | * Enable Auditing. |
||
436 | * |
||
437 | * @return void |
||
438 | */ |
||
439 | 1 | public static function enableAuditing() |
|
443 | |||
444 | /** |
||
445 | * Determine whether auditing is enabled. |
||
446 | * |
||
447 | * @return bool |
||
448 | */ |
||
449 | 82 | public static function isAuditingEnabled(): bool |
|
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | */ |
||
461 | 40 | public function getAuditInclude(): array |
|
465 | |||
466 | /** |
||
467 | * {@inheritdoc} |
||
468 | */ |
||
469 | 45 | public function getAuditExclude(): array |
|
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | 46 | public function getAuditStrict(): bool |
|
481 | |||
482 | /** |
||
483 | * {@inheritdoc} |
||
484 | */ |
||
485 | 46 | public function getAuditTimestamps(): bool |
|
489 | |||
490 | /** |
||
491 | * {@inheritdoc} |
||
492 | */ |
||
493 | 43 | public function getAuditDriver() |
|
497 | |||
498 | /** |
||
499 | * {@inheritdoc} |
||
500 | */ |
||
501 | 40 | public function getAuditThreshold(): int |
|
505 | |||
506 | /** |
||
507 | * {@inheritdoc} |
||
508 | */ |
||
509 | 44 | public function generateTags(): array |
|
513 | |||
514 | /** |
||
515 | * {@inheritdoc} |
||
516 | */ |
||
517 | 8 | public function transitionTo(Contracts\Audit $audit, bool $old = false): Contracts\Auditable |
|
560 | } |
||
561 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.