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 |
||
15 | final class UserInfo implements ApiResponse, \ArrayAccess |
||
16 | { |
||
17 | /** |
||
18 | * @var array the raw data from the API |
||
19 | */ |
||
20 | private $data; |
||
21 | |||
22 | /** |
||
23 | * @param array $data |
||
24 | */ |
||
25 | private function __construct(array $data) |
||
29 | |||
30 | /** |
||
31 | * @param array $data |
||
32 | * |
||
33 | * @return self |
||
34 | */ |
||
35 | public static function create($data) |
||
39 | |||
40 | /** |
||
41 | * @return string |
||
42 | */ |
||
43 | public function __toString() |
||
59 | |||
60 | public function offsetExists($offset) |
||
64 | |||
65 | public function offsetGet($offset) |
||
69 | |||
70 | public function offsetSet($offset, $value) |
||
74 | |||
75 | public function offsetUnset($offset) |
||
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function isEmailVerified() |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getEmail() |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getUsername() |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getClientId() |
||
115 | |||
116 | /** |
||
117 | * @return \DateTimeInterface |
||
118 | */ |
||
119 | public function getUpdatedAt() |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getName() |
||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getPicture() |
||
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getNickname() |
||
147 | |||
148 | /** |
||
149 | * @return array |
||
150 | */ |
||
151 | public function getIdentities() |
||
155 | |||
156 | /** |
||
157 | * @return \DateTimeInterface |
||
158 | */ |
||
159 | public function getCreatedAt() |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getUserId() |
||
168 | { |
||
169 | return $this->data['sub'] ?? $this->data['user_id']; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getRoles() |
||
183 | |||
184 | /** |
||
185 | * @param string $name |
||
186 | * @param mixed $default |
||
187 | * |
||
188 | * @return mixed |
||
189 | */ |
||
190 | View Code Duplication | public function getAppMetadata($name, $default = null) |
|
202 | |||
203 | /** |
||
204 | * @param string $name |
||
205 | * @param mixed $default |
||
206 | * |
||
207 | * @return mixed |
||
208 | */ |
||
209 | View Code Duplication | public function getUserMetadata($name, $default = null) |
|
221 | } |
||
222 |
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.