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 |
||
20 | class Mapper |
||
21 | { |
||
22 | /** |
||
23 | * Map user data into user object. |
||
24 | * |
||
25 | * @param array $data |
||
26 | * |
||
27 | * @return \eZ\Publish\SPI\Persistence\User |
||
28 | */ |
||
29 | public function mapUser(array $data) |
||
30 | { |
||
31 | $user = new User(); |
||
32 | $user->id = $data['contentobject_id']; |
||
33 | $user->login = $data['login']; |
||
34 | $user->email = $data['email']; |
||
35 | $user->passwordHash = $data['password_hash']; |
||
36 | $user->hashAlgorithm = (int)$data['password_hash_type']; |
||
37 | $user->passwordUpdatedAt = $data['password_updated_at'] !== null ? (int)$data['password_updated_at'] : null; |
||
38 | $user->isEnabled = (bool)$data['is_enabled']; |
||
39 | $user->maxLogin = $data['max_login']; |
||
40 | |||
41 | return $user; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Map data for a set of user data. |
||
46 | * |
||
47 | * @param array $data |
||
48 | * |
||
49 | * @return \eZ\Publish\SPI\Persistence\User[] |
||
50 | */ |
||
51 | public function mapUsers(array $data) |
||
60 | |||
61 | /** |
||
62 | * Map policy data to an array of policies. |
||
63 | * |
||
64 | * @param array $data |
||
65 | * |
||
66 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
67 | */ |
||
68 | public function mapPolicies(array $data) |
||
109 | |||
110 | /** |
||
111 | * Map role data to a role. |
||
112 | * |
||
113 | * @param array $data |
||
114 | * |
||
115 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
116 | */ |
||
117 | public function mapRole(array $data) |
||
135 | |||
136 | /** |
||
137 | * Map data for a set of roles. |
||
138 | * |
||
139 | * @param array $data |
||
140 | * |
||
141 | * @return \eZ\Publish\SPI\Persistence\User\Role[] |
||
142 | */ |
||
143 | public function mapRoles(array $data) |
||
157 | |||
158 | /** |
||
159 | * Map data for a set of role assignments. |
||
160 | * |
||
161 | * @param array $data |
||
162 | * |
||
163 | * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[] |
||
164 | */ |
||
165 | public function mapRoleAssignments(array $data) |
||
210 | |||
211 | /** |
||
212 | * Creates a create struct from an existing $role. |
||
213 | * |
||
214 | * @param \eZ\Publish\SPI\Persistence\User\Role $role |
||
215 | * |
||
216 | * @return \eZ\Publish\SPI\Persistence\User\RoleCreateStruct |
||
217 | */ |
||
218 | View Code Duplication | public function createCreateStructFromRole(Role $role) |
|
227 | |||
228 | /** |
||
229 | * Maps properties from $struct to $role. |
||
230 | * |
||
231 | * @param \eZ\Publish\SPI\Persistence\User\RoleCreateStruct $createStruct |
||
232 | * |
||
233 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
234 | */ |
||
235 | View Code Duplication | public function createRoleFromCreateStruct(RoleCreateStruct $createStruct) |
|
245 | |||
246 | /** |
||
247 | * Maps properties from $struct to $role. |
||
248 | * |
||
249 | * @param \eZ\Publish\SPI\Persistence\User\RoleCopyStruct $copyStruct |
||
250 | * |
||
251 | * @return \eZ\Publish\SPI\Persistence\User\Role |
||
252 | */ |
||
253 | View Code Duplication | public function createRoleFromCopyStruct(User\RoleCopyStruct $copyStruct) |
|
263 | } |
||
264 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..