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 |
||
| 17 | class UserController extends Controller |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @Route("/avatar", name="api_avatar") |
||
| 21 | * @Method({"PUT"}) |
||
| 22 | */ |
||
| 23 | public function avatarAction(Request $request) |
||
| 24 | { |
||
| 25 | $headers = $request->headers; |
||
| 26 | /** @var User $user */ |
||
| 27 | $user = $this->getUser(); |
||
| 28 | |||
| 29 | $image = new Image(sprintf('user/%d/avatar', $user->getId())); |
||
| 30 | $image |
||
| 31 | ->setContentType($headers->get('Content-Type')) |
||
| 32 | ->setContent($request->getContent()); |
||
| 33 | /** @var ConstraintViolationList $errors */ |
||
| 34 | $errors = $this->get('validator')->validate($image, null, ['Api']); |
||
| 35 | |||
| 36 | if ($errors->count()) { |
||
| 37 | $outErrors = []; |
||
| 38 | |||
| 39 | /** @var ConstraintViolation $error */ |
||
| 40 | foreach ($errors as $error) { |
||
| 41 | $outErrors['headers'][$error->getPropertyPath()] = $error->getMessage(); |
||
| 42 | } |
||
| 43 | |||
| 44 | throw new JsonHttpException(400, 'Bad Request', $outErrors); |
||
| 45 | } |
||
| 46 | $user->setImage($image); |
||
|
|
|||
| 47 | $this->getDoctrine()->getManager()->flush(); |
||
| 48 | |||
| 49 | return $this->json(['user' => $user], 201, [], [AbstractNormalizer::GROUPS => ['Short']]); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @Route("/user") |
||
| 54 | * @Method({"GET"}) |
||
| 55 | */ |
||
| 56 | 1 | public function userAction() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @Route("/password_reset", name="password_reset") |
||
| 63 | * @Method({"POST"}) |
||
| 64 | * |
||
| 65 | * @return JsonResponse |
||
| 66 | */ |
||
| 67 | 1 | public function resetPasswordAction(Request $request) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @Route("/email") |
||
| 98 | * @Method({"PUT"}) |
||
| 99 | */ |
||
| 100 | public function emailAction(Request $request) |
||
| 124 | } |
||
| 125 |
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: