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 namespace Distilleries\Expendable\Scopes; |
||
5 | trait Translatable { |
||
6 | |||
7 | /** |
||
8 | * Boot the soft deleting trait for a model. |
||
9 | * |
||
10 | * @return void |
||
11 | */ |
||
12 | public static function bootTranslatable() |
||
16 | |||
17 | |||
18 | /** |
||
19 | * Get a new query builder that only includes soft deletes. |
||
20 | * |
||
21 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
22 | */ |
||
23 | public static function withoutTranslation() |
||
30 | |||
31 | /** |
||
32 | * Get the fully qualified "iso" column. |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | public function getQualifiedIsoColumn() |
||
42 | |||
43 | /** |
||
44 | * Get the fully qualified "id_element" column. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getQualifiedIdElementColumn() |
||
54 | |||
55 | /** |
||
56 | * Get the fully qualified "id_element" column. |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getQualifiedModelColumn() |
||
66 | |||
67 | /** |
||
68 | * Get the fully qualified "id_source" column. |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getQualifiedIdSourceColumn() |
||
78 | |||
79 | /** |
||
80 | * Get the fully qualified "table" column. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getQualifiedTable() |
||
90 | |||
91 | public function setTranslation($id_element, $model, $id_source, $iso) |
||
111 | |||
112 | View Code Duplication | public function getIso($model, $id_element) |
|
119 | |||
120 | View Code Duplication | public function hasBeenTranslated($model, $id_source, $iso) |
|
129 | |||
130 | View Code Duplication | public function hasTranslation($model, $id_element) |
|
137 | } |
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.