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 Atog\Api; |
||
10 | class Model implements JsonSerializable |
||
11 | { |
||
12 | use Jsonable; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $attributes = []; |
||
18 | |||
19 | /** |
||
20 | * Model constructor. |
||
21 | * @param null $attributes |
||
22 | */ |
||
23 | public function __construct($attributes = null) |
||
29 | |||
30 | /** |
||
31 | * Fill the attributes |
||
32 | * @param string|array $attributes |
||
33 | * @return void |
||
34 | */ |
||
35 | private function fill($attributes) |
||
51 | |||
52 | /** |
||
53 | * Set a given attribute on the model. |
||
54 | * @param string $key |
||
55 | * @param mixed $value |
||
56 | * @return $this |
||
57 | */ |
||
58 | View Code Duplication | public function setAttribute($key, $value) |
|
73 | |||
74 | /** |
||
75 | * Determine if a set mutator exists for an attribute. |
||
76 | * @param string $key |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function hasSetMutator($key) |
||
83 | |||
84 | /** |
||
85 | * @param string $name |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function __get($name) |
||
92 | |||
93 | /** |
||
94 | * @param string $name |
||
95 | * @param mixed $value |
||
96 | */ |
||
97 | public function __set($name, $value) |
||
101 | |||
102 | /** |
||
103 | * Get an attribute from the $attributes array. |
||
104 | * @param string $key |
||
105 | * @return mixed |
||
106 | */ |
||
107 | View Code Duplication | public function getAttribute($key) |
|
121 | |||
122 | /** |
||
123 | * Get an attribute from the $attributes array. |
||
124 | * @param string $key |
||
125 | * @return mixed |
||
126 | */ |
||
127 | protected function getAttributeValue($key) |
||
135 | |||
136 | /** |
||
137 | * Determine if a get mutator exists for an attribute. |
||
138 | * @param string $key |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function hasGetMutator($key) |
||
145 | |||
146 | /** |
||
147 | * @param $key |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function __isset($key) |
||
154 | |||
155 | /** |
||
156 | * Transform an array of values into a collection of models |
||
157 | * @param array $values |
||
158 | * @param null $class |
||
159 | * @return \Illuminate\Support\Collection |
||
160 | */ |
||
161 | protected function makeCollection(array $values, $class = null) |
||
177 | |||
178 | /** |
||
179 | * Create a new model instance |
||
180 | * @param array $attributes |
||
181 | * @return \Atog\Api\Model |
||
182 | * @throws \InvalidArgumentException if attributes is not an array or a json object |
||
183 | */ |
||
184 | public function newInstance($attributes = []) |
||
188 | } |
||
189 |
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.