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 |
||
14 | class ManagersController extends FOSRestController |
||
15 | { |
||
16 | use Traits\TokenFromHeaderTrait; |
||
17 | use Traits\HandleUserTrait; |
||
18 | |||
19 | /** |
||
20 | * NOTE: use annotation for routing here even |
||
21 | * if the FOSRestBundle is automatically able |
||
22 | * to handle them. In fact, ParamConverter is |
||
23 | * not supported by FOSRestController. |
||
24 | * |
||
25 | * @Annotations\Post("/users") |
||
26 | * @ParamConverter("user", converter="fos_rest.request_body") |
||
27 | */ |
||
28 | public function postUserAction(User $user) |
||
29 | { |
||
30 | $this->throwIfClientNot('backend'); |
||
31 | |||
32 | // TODO: 13 - refactor common parameters |
||
33 | // settings in the HandleUserTrait. |
||
34 | $manager = $this->get('fos_user.user_manager'); |
||
35 | $manager->deleteIfNonEnabledExists($user); |
||
36 | |||
37 | //TODO: 16 - use the mobile_app_registration |
||
38 | //group for now, but should be renamed |
||
39 | //for the backend |
||
40 | $errors = $this->validates( |
||
41 | $user, |
||
|
|||
42 | 'mobile_app_registration' |
||
43 | ); |
||
44 | if (count($errors) > 0) { |
||
45 | return $this->handleView( |
||
46 | new View($errors, Response::HTTP_BAD_REQUEST) |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | $newUser = $manager->createUser(); |
||
51 | |||
52 | $phoneNumber = $user->getPhoneNumber(); |
||
53 | if (!is_null($phoneNumber)) { |
||
54 | $phoneNumber = str_replace('+', '00', $phoneNumber); |
||
55 | } |
||
56 | |||
57 | $newUser->setPhoneNumber($phoneNumber); |
||
58 | $newUser->setEmail($user->getEmail()); |
||
59 | $newUser->setUsername($user->getUsername()); |
||
60 | $newUser->setPlainPassword($user->getPlainPassword()); |
||
61 | $newUser->setRoles($user->getRoles()); |
||
62 | $newUser->setEnabled(true); |
||
63 | $newUser->setLocked(false); |
||
64 | $manager->updateUser($newUser); |
||
65 | |||
66 | return $this->handleView( |
||
67 | new View( |
||
68 | array( |
||
69 | 'id' => $newUser->getId(), |
||
70 | ), |
||
71 | Response::HTTP_CREATED |
||
72 | ) |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @Annotations\Put("/users/{id}") |
||
78 | * |
||
79 | * @ParamConverter("updatedUser", converter="fos_rest.request_body") |
||
80 | * |
||
81 | * @param User $user |
||
82 | * @param User $updatedUser |
||
83 | */ |
||
84 | public function putUserAction( |
||
85 | User $user, |
||
86 | User $updatedUser |
||
87 | ) { |
||
88 | $this->throwIfClientNot('backend'); |
||
89 | |||
90 | $errors = $this->validates( |
||
91 | $updatedUser, |
||
92 | 'backend_user_edit' |
||
93 | ); |
||
94 | if (count($errors) > 0) { |
||
95 | return $this->handleView( |
||
96 | new View($errors, Response::HTTP_BAD_REQUEST) |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | $user->setUsername($updatedUser->getUsername()); |
||
101 | $user->setEmail($updatedUser->getEmail()); |
||
102 | $user->setPhoneNumber($updatedUser->getPhoneNumber()); |
||
103 | $user->setRoles($updatedUser->getRoles()); |
||
104 | |||
105 | $this->get('fos_user.user_manager')->updateUser($user); |
||
106 | |||
107 | return $this->handleView( |
||
108 | new View( |
||
109 | array( |
||
110 | 'id' => $user->getId(), |
||
111 | ), |
||
112 | Response::HTTP_OK |
||
113 | ) |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param User $user |
||
119 | * @param Request $request |
||
120 | */ |
||
121 | public function putUserRolesAction( |
||
133 | |||
134 | /** |
||
135 | * @param User $user |
||
136 | */ |
||
137 | public function getUserAction(User $user) |
||
143 | |||
144 | /** |
||
145 | * Disable one given user. |
||
146 | * |
||
147 | * @param User $user |
||
148 | */ |
||
149 | public function patchUserDisableAction(User $user) |
||
157 | |||
158 | /** |
||
159 | * @param User $user |
||
160 | */ |
||
161 | public function patchUserEnableAction(User $user) |
||
169 | |||
170 | /** |
||
171 | * @param User $user |
||
172 | * @param Request $request |
||
173 | * |
||
174 | * @Annotations\put("/users/{id}/password") |
||
175 | * |
||
176 | * @return Response |
||
177 | */ |
||
178 | 2 | public function putUsersPasswordAction(User $user, Request $request) |
|
207 | |||
208 | /** |
||
209 | * Check if the JSON sent data is correct |
||
210 | * for the current called action |
||
211 | * and throws a bad request exception if the input is wrong. |
||
212 | * |
||
213 | * @param Request $request |
||
214 | * @param array $keys |
||
215 | * @param string $message |
||
216 | * |
||
217 | * @return array |
||
218 | * |
||
219 | * @throws BadRequestHttpException |
||
220 | */ |
||
221 | 1 | View Code Duplication | private function requestIsJsonWithKeysOrThrow( |
236 | |||
237 | 2 | private function isCurrentUserAdmin() |
|
244 | } |
||
245 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: