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 |
||
13 | trait Mutable |
||
14 | { |
||
15 | /** |
||
16 | * Attribute mutator instance. |
||
17 | * |
||
18 | * @var \Sofa\Eloquence\Contracts\Mutator |
||
19 | */ |
||
20 | protected static $attributeMutator; |
||
21 | |||
22 | /** |
||
23 | * Register hooks for the trait. |
||
24 | * |
||
25 | * @codeCoverageIgnore |
||
26 | * |
||
27 | * @return void |
||
28 | */ |
||
29 | public static function bootMutable() |
||
45 | |||
46 | /** |
||
47 | * Mutate mutable attributes for array conversion. |
||
48 | * |
||
49 | * @param array $attributes |
||
50 | * @return array |
||
51 | */ |
||
52 | View Code Duplication | protected function mutableAttributesToArray(array $attributes) |
|
62 | |||
63 | /** |
||
64 | * Determine whether an attribute has getter mutators defined. |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @return boolean |
||
68 | */ |
||
69 | public function hasGetterMutator($key) |
||
73 | |||
74 | /** |
||
75 | * Determine whether an attribute has setter mutators defined. |
||
76 | * |
||
77 | * @param string $key |
||
78 | * @return boolean |
||
79 | */ |
||
80 | public function hasSetterMutator($key) |
||
84 | |||
85 | /** |
||
86 | * Mutate the attribute. |
||
87 | * |
||
88 | * @param string $key |
||
89 | * @param string $value |
||
90 | * @param string $dir |
||
91 | * @return mixed |
||
92 | */ |
||
93 | protected function mutableMutate($key, $value, $dir) |
||
99 | |||
100 | /** |
||
101 | * Get the mutators for an attribute. |
||
102 | * |
||
103 | * @param string $key |
||
104 | * @return string |
||
105 | */ |
||
106 | protected function getMutatorsForAttribute($key, $dir) |
||
110 | |||
111 | /** |
||
112 | * Get the array of attribute mutators. |
||
113 | * |
||
114 | * @param string $dir |
||
115 | * @return array |
||
116 | */ |
||
117 | public function getMutators($dir) |
||
123 | |||
124 | /** |
||
125 | * Set attribute mutator instance. |
||
126 | * |
||
127 | * @codeCoverageIgnore |
||
128 | * |
||
129 | * @param \Sofa\Eloquence\Contracts\Mutator $mutator |
||
130 | * @return void |
||
131 | */ |
||
132 | public static function setAttributeMutator(MutatorContract $mutator) |
||
136 | |||
137 | /** |
||
138 | * Get attribute mutator instance. |
||
139 | * |
||
140 | * @codeCoverageIgnore |
||
141 | * |
||
142 | * @return \Sofa\Eloquence\Contracts\Mutator |
||
143 | */ |
||
144 | public static function getAttributeMutator() |
||
148 | } |
||
149 |
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.