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 |
||
7 | class Entity extends ValueObject |
||
8 | { |
||
9 | /** |
||
10 | * Entities Hidden Attributes, that will be discarded when converting |
||
11 | * the entity to Array/Json |
||
12 | * (can include any embedded object's attribute) |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $hidden = []; |
||
17 | |||
18 | /** |
||
19 | * Return the entity's attribute |
||
20 | * @param string $key |
||
21 | * @return mixed |
||
22 | */ |
||
23 | public function __get($key) |
||
24 | { |
||
25 | if ($this->hasGetMutator($key)) { |
||
26 | $method = 'get' . $this->getMutatorMethod($key); |
||
27 | |||
28 | $attribute = null; |
||
29 | |||
30 | if (isset($this->attributes[$key])) { |
||
31 | $attribute = $this->attributes[$key]; |
||
32 | } |
||
33 | |||
34 | return $this->$method($attribute); |
||
35 | } |
||
36 | if (!array_key_exists($key, $this->attributes)) { |
||
37 | return null; |
||
38 | } |
||
39 | if ($this->attributes[$key] instanceof EntityProxy) { |
||
40 | $this->attributes[$key] = $this->attributes[$key]->load(); |
||
41 | } |
||
42 | return $this->attributes[$key]; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Dynamically set attributes on the entity. |
||
47 | * |
||
48 | * @param string $key |
||
49 | * @param mixed $value |
||
50 | * @return void |
||
51 | */ |
||
52 | public function __set($key, $value) |
||
53 | { |
||
54 | if ($this->hasSetMutator($key)) { |
||
55 | $method = 'set' . $this->getMutatorMethod($key); |
||
56 | |||
57 | $this->$method($value); |
||
58 | } else { |
||
59 | $this->attributes[$key] = $value; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Is a getter method defined ? |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @return boolean |
||
68 | */ |
||
69 | protected function hasGetMutator($key) |
||
73 | |||
74 | /** |
||
75 | * Is a setter method defined ? |
||
76 | * |
||
77 | * @param string $key |
||
78 | * @return boolean |
||
79 | */ |
||
80 | protected function hasSetMutator($key) |
||
84 | |||
85 | /** |
||
86 | * @param $key |
||
87 | * @return string |
||
88 | */ |
||
89 | protected function getMutatorMethod($key) |
||
94 | |||
95 | /** |
||
96 | * Convert every attributes to value / arrays |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function toArray() |
||
118 | |||
119 | /** |
||
120 | * Fill an entity with key-value pairs |
||
121 | * |
||
122 | * @param array $attributes |
||
123 | * @return void |
||
124 | */ |
||
125 | public function fill(array $attributes) |
||
138 | } |
||
139 |
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.