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 RevisionableTrait 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 RevisionableTrait, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Sofa\Revisionable\Laravel; |
||
24 | trait RevisionableTrait |
||
25 | { |
||
26 | /** |
||
27 | * Revisionable Logger instance. |
||
28 | * |
||
29 | * @var \Sofa\Revisionable\Logger |
||
30 | */ |
||
31 | protected static $revisionableLogger; |
||
32 | |||
33 | /** |
||
34 | * Revisioning switch. |
||
35 | * |
||
36 | * @var boolean |
||
37 | */ |
||
38 | protected $revisioned = true; |
||
39 | |||
40 | /** |
||
41 | * Boot revisionable trait for the model. |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | public static function bootRevisionableTrait() |
||
46 | { |
||
47 | static::bootLogger(); |
||
48 | |||
49 | static::registerListeners(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Register event listeners. |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | public static function registerListeners() |
||
58 | { |
||
59 | foreach (static::getRevisionableEvents() as $event) { |
||
60 | static::{"register{$event}Listener"}(); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Register listener for created event. |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | protected static function registerCreatedListener() |
||
73 | |||
74 | /** |
||
75 | * Register listener for updated event. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | protected static function registerUpdatedListener() |
||
83 | |||
84 | /** |
||
85 | * Register listener for deleted event. |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | protected static function registerDeletedListener() |
||
93 | |||
94 | /** |
||
95 | * Register listener for restored event. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | protected static function registerRestoredListener() |
||
100 | { |
||
101 | if (method_exists(get_called_class(), 'restored')) { |
||
102 | static::restored('Sofa\Revisionable\Listener@onRestored'); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Boot Revisionable Logger. |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | public static function bootLogger() |
||
112 | { |
||
113 | if (!static::$revisionableLogger) { |
||
114 | static::setRevisionableLogger(App::make('revisionable.logger')); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Set logger instance. |
||
120 | * |
||
121 | * @param mixed $logger |
||
122 | */ |
||
123 | public static function setRevisionableLogger($logger) |
||
127 | |||
128 | /** |
||
129 | * Get logger instance. |
||
130 | * |
||
131 | * @return \Sofa\Revisionable\Logger |
||
132 | */ |
||
133 | public static function getRevisionableLogger() |
||
137 | |||
138 | /** |
||
139 | * Get the connection for revision logs. |
||
140 | * |
||
141 | * @return \Illuminate\Database\ConnectionInterface |
||
142 | */ |
||
143 | public function getRevisionableConnection() |
||
144 | { |
||
145 | $connection = (isset($this->revisionableConnection)) ? $this->revisionableConnection : null; |
||
146 | |||
147 | return static::resolveConnection($connection); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Get an array of updated revisionable attributes. |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function getDiff() |
||
156 | { |
||
157 | $old = $this->getOldAttributes(); |
||
158 | |||
159 | $new = $this->getNewAttributes(); |
||
160 | |||
161 | return array_diff_assoc($new, $old); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Get an array of original revisionable attributes. |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | public function getOldAttributes() |
||
170 | { |
||
171 | $attributes = $this->getRevisionableItems($this->original); |
||
172 | |||
173 | return $this->prepareAttributes($attributes); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get an array of current revisionable attributes. |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public function getNewAttributes() |
||
182 | { |
||
183 | $attributes = $this->getRevisionableItems($this->attributes); |
||
184 | |||
185 | return $this->prepareAttributes($attributes); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Stringify revisionable attributes. |
||
190 | * |
||
191 | * @param array $attributes |
||
192 | * @return array |
||
193 | */ |
||
194 | protected function prepareAttributes(array $attributes) |
||
195 | { |
||
196 | return array_map(function ($attribute) { |
||
197 | return ($attribute instanceof DateTime) |
||
198 | ? $this->fromDateTime($attribute) |
||
|
|||
199 | : (string) $attribute; |
||
200 | }, $attributes); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Get an array of revisionable attributes. |
||
205 | * |
||
206 | * @param array $values |
||
207 | * @return array |
||
208 | */ |
||
209 | public function getRevisionableItems(array $values) |
||
210 | { |
||
211 | if (count($this->getRevisionable()) > 0) { |
||
212 | return array_intersect_key($values, array_flip($this->getRevisionable())); |
||
213 | } |
||
214 | |||
215 | return array_diff_key($values, array_flip($this->getNonRevisionable())); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Events being tracked. |
||
220 | * |
||
221 | * @var array |
||
222 | */ |
||
223 | protected static function getRevisionableEvents() |
||
224 | { |
||
225 | return (isset(static::$revisionableEvents)) |
||
226 | ? (array) static::$revisionableEvents |
||
227 | : ['Created', 'Updated', 'Deleted', 'Restored']; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Attributes being revisioned. |
||
232 | * |
||
233 | * @var array |
||
234 | */ |
||
235 | public function getRevisionable() |
||
236 | { |
||
237 | return (isset($this->revisionable)) |
||
238 | ? (array) $this->revisionable |
||
239 | : []; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Attributes hidden from revisioning if revisionable are not provided. |
||
244 | * |
||
245 | * @var array |
||
246 | */ |
||
247 | public function getNonRevisionable() |
||
248 | { |
||
249 | return (isset($this->nonRevisionable)) |
||
250 | ? (array) $this->nonRevisionable |
||
251 | : ['created_at', 'updated_at', 'deleted_at']; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Determine if model should be revisioned. |
||
256 | * |
||
257 | * @return boolean |
||
258 | */ |
||
259 | public function isRevisioned() |
||
263 | |||
264 | /** |
||
265 | * Disable revisioning for current instance. |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function disableRevisioning() |
||
273 | |||
274 | /** |
||
275 | * Enable revisioning for current instance. |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | public function enableRevisioning() |
||
283 | |||
284 | /** |
||
285 | * Model has many Revision |
||
286 | * |
||
287 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
288 | */ |
||
289 | public function revisions() |
||
290 | { |
||
291 | return $this->hasMany('Sofa\Revisionable\Laravel\Revision', 'row_id') |
||
292 | ->latest() |
||
293 | ->where('table_name', $this->getTable()); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Accessor for revisions property |
||
298 | * |
||
299 | * @return \Illuminate\Database\Eloquent\Collection |
||
300 | */ |
||
301 | public function getRevisionsAttribute() |
||
302 | { |
||
303 | $this->loadRelationIfNecessary('revisions'); |
||
304 | |||
305 | $collection = $this->getRelation('revisions'); |
||
306 | |||
307 | $presenter = $this->getRevisionPresenter(); |
||
308 | |||
309 | $default = $this->getDefaultRevisionPresenter(); |
||
310 | |||
311 | if (is_subclass_of($presenter, $default) || $presenter == $default) { |
||
312 | return $presenter::make($collection, $this); |
||
313 | } |
||
314 | |||
315 | return $collection; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Get record version at given timestamp. |
||
320 | * |
||
321 | * @param \Carbon\Carbon|string $timestamp |
||
322 | * |
||
323 | * @return \Sofa\Revisionable\Laravel\Revision|\Sofa\Revisionable\laravel\Presenter|null |
||
324 | */ |
||
325 | public function revisionSnapshot($timestamp) |
||
326 | { |
||
327 | $revision = $this->revisions()->where('created_at', '<=', $timestamp)->first(); |
||
328 | |||
329 | return ($revision) ? $this->wrapRevision($revision) : null; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Get record version at given step in history. |
||
334 | * |
||
335 | * @param integer $step |
||
336 | * |
||
337 | * @return \Sofa\Revisionable\Laravel\Revision|\Sofa\Revisionable\laravel\Presenter|null |
||
338 | */ |
||
339 | public function revisionStep($step) |
||
340 | { |
||
341 | $revision = $this->revisions()->skip($step)->first(); |
||
342 | |||
343 | return ($revision) ? $this->wrapRevision($revision) : null; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Wrap revision model with the presenter if provided. |
||
348 | * |
||
349 | * @param \Sofa\Revisionable\Laravel\Revision $revision |
||
350 | * @return \Sofa\Revisionable\Laravel\Presenter|\Sofa\Revisionable\Laravel\Revision |
||
351 | */ |
||
352 | public function wrapRevision(Revision $revision) |
||
353 | { |
||
354 | $presenter = $this->getRevisionPresenter(); |
||
355 | |||
356 | return (is_subclass_of($presenter, $this->getDefaultRevisionPresenter()) |
||
357 | || $presenter == $this->getDefaultRevisionPresenter()) |
||
358 | ? $presenter::make($revision, $this) |
||
359 | : $revision; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Load revisions relation if not loaded. |
||
364 | * |
||
365 | * @param string $relation |
||
366 | * |
||
367 | * @return void |
||
368 | */ |
||
369 | protected function loadRelationIfNecessary($relation) |
||
370 | { |
||
371 | if (!array_key_exists($relation, $this->relations)) { |
||
372 | $this->load($relation); |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Revisionable has one Revision count. |
||
378 | * |
||
379 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
380 | */ |
||
381 | public function revisionsCount() |
||
382 | { |
||
383 | return $this->hasOne('Sofa\Revisionable\Laravel\Revision', 'row_id') |
||
384 | ->where('table_name', $this->getTable()) |
||
385 | ->selectRaw('count(*) as aggregate, row_id') |
||
386 | ->groupBy('row_id'); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Convenient accessor for revisionsCount relation. |
||
391 | * |
||
392 | * @return integer |
||
393 | */ |
||
394 | public function getRevisionsCountAttribute() |
||
395 | { |
||
396 | if (!array_key_exists('revisionsCount', $this->relations)) { |
||
397 | $this->load('revisionsCount'); |
||
398 | } |
||
399 | |||
400 | $relation = $this->getRelation('revisionsCount'); |
||
401 | |||
402 | return ($relation) ? (int) $relation->aggregate : 0; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Determine if model has any revisions history. |
||
407 | * |
||
408 | * @return boolean |
||
409 | */ |
||
410 | public function hasRevisions() |
||
414 | |||
415 | /** |
||
416 | * Determine if model has any revisions history. |
||
417 | * |
||
418 | * @return boolean |
||
419 | */ |
||
420 | public function hasHistory() |
||
424 | |||
425 | /** |
||
426 | * User has one oldestRevision |
||
427 | * |
||
428 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
429 | */ |
||
430 | public function oldestRevision() |
||
431 | { |
||
432 | return $this->hasOne('Sofa\Revisionable\Laravel\Revision', 'row_id') |
||
433 | ->where('table_name', $this->getTable()) |
||
434 | ->oldest(); |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * Accessor for oldestRevision property |
||
439 | * |
||
440 | * @return \Sofa\Revisionable\Laravel\Revision|\Sofa\Revisionable\Laravel\Presenter|null |
||
441 | */ |
||
442 | View Code Duplication | public function getOldestRevisionAttribute() |
|
443 | { |
||
444 | $this->loadRelationIfNecessary('oldestRevision'); |
||
445 | |||
446 | $revision = $this->getRelation('oldestRevision'); |
||
447 | |||
448 | return ($revision) ? $this->wrapRevision($revision) : null; |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * User has one latestRevision |
||
453 | * |
||
454 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
455 | */ |
||
456 | public function latestRevision() |
||
457 | { |
||
458 | return $this->hasOne('Sofa\Revisionable\Laravel\Revision', 'row_id') |
||
459 | ->where('table_name', $this->getTable()) |
||
460 | ->latest(); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Accessor for latestRevision property |
||
465 | * |
||
466 | * @return \Sofa\Revisionable\Laravel\Revision|\Sofa\Revisionable\Laravel\Presenter|null |
||
467 | */ |
||
468 | View Code Duplication | public function getLatestRevisionAttribute() |
|
469 | { |
||
470 | $this->loadRelationIfNecessary('latestRevision'); |
||
471 | |||
472 | $revision = $this->getRelation('latestRevision'); |
||
473 | |||
474 | return ($revision) ? $this->wrapRevision($revision) : null; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Set revision presenter class or true for default. |
||
479 | * |
||
480 | * @param string|true $class |
||
481 | */ |
||
482 | public function setRevisionPresenter($class) |
||
486 | |||
487 | /** |
||
488 | * Get revision presenter class for the model. |
||
489 | * |
||
490 | * @return string|null |
||
491 | */ |
||
492 | public function getRevisionPresenter() |
||
493 | { |
||
494 | if (!isset($this->revisionPresenter)) { |
||
495 | return null; |
||
496 | } |
||
497 | |||
498 | // Use default or custom presenter class if provided. |
||
499 | return ($this->revisionPresenter === true) |
||
500 | ? $this->getDefaultRevisionPresenter() |
||
501 | : $this->revisionPresenter; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Get default revision presenter from the package. |
||
506 | * |
||
507 | * @return string |
||
508 | */ |
||
509 | public function getDefaultRevisionPresenter() |
||
513 | } |
||
514 |
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.