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 |
||
| 42 | class UserController extends AbstractController |
||
| 43 | { |
||
| 44 | protected $settingsManager; |
||
| 45 | |||
| 46 | protected $scopeContext; |
||
| 47 | |||
| 48 | protected $formFactory; |
||
| 49 | |||
| 50 | protected $userRepository; |
||
| 51 | |||
| 52 | public function __construct( |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @Operation( |
||
| 66 | * tags={"user"}, |
||
| 67 | * summary="Change user roles", |
||
| 68 | * @SWG\Parameter( |
||
| 69 | * name="body", |
||
| 70 | * in="body", |
||
| 71 | * @SWG\Schema( |
||
| 72 | * ref=@Model(type=UserRolesType::class) |
||
| 73 | * ) |
||
| 74 | * ), |
||
| 75 | * @SWG\Response( |
||
| 76 | * response="200", |
||
| 77 | * description="Returned on success.", |
||
| 78 | * @Model(type=\SWP\Bundle\CoreBundle\Model\User::class, groups={"api"}) |
||
| 79 | * ), |
||
| 80 | * @SWG\Response( |
||
| 81 | * response="404", |
||
| 82 | * description="Returned on user not found." |
||
| 83 | * ), |
||
| 84 | * @SWG\Response( |
||
| 85 | * response="403", |
||
| 86 | * description="Returned when user don't have permissions to change roles" |
||
| 87 | * ) |
||
| 88 | * ) |
||
| 89 | * |
||
| 90 | * |
||
| 91 | * @Route("/api/{version}/users/{id}/promote", methods={"PATCH"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_promote_user") |
||
| 92 | * @Route("/api/{version}/users/{id}/demote", methods={"PATCH"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_demote_user") |
||
| 93 | */ |
||
| 94 | public function modifyRoles(Request $request, $id, UserManagerInterface $userManager, AuthorizationCheckerInterface $authorizationChecker) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @Operation( |
||
| 127 | * tags={"user"}, |
||
| 128 | * summary="Get user settings", |
||
| 129 | * @SWG\Response( |
||
| 130 | * response="200", |
||
| 131 | * description="Returned on success.", |
||
| 132 | * @Model(type=\SWP\Bundle\CoreBundle\Model\User::class, groups={"api"}) |
||
| 133 | * ), |
||
| 134 | * @SWG\Response( |
||
| 135 | * response="401", |
||
| 136 | * description="Returned on user not found." |
||
| 137 | * ) |
||
| 138 | * ) |
||
| 139 | * |
||
| 140 | * @Route("/api/{version}/users/settings/", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_get_settings") |
||
| 141 | */ |
||
| 142 | public function listSettings(): SingleResourceResponseInterface |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @Operation( |
||
| 159 | * tags={"user"}, |
||
| 160 | * summary="Update user setting", |
||
| 161 | * @SWG\Parameter( |
||
| 162 | * name="body", |
||
| 163 | * in="body", |
||
| 164 | * @SWG\Schema( |
||
| 165 | * ref=@Model(type=SettingType::class) |
||
| 166 | * ) |
||
| 167 | * ), |
||
| 168 | * @SWG\Response( |
||
| 169 | * response="200", |
||
| 170 | * description="Returned on success.", |
||
| 171 | * @Model(type=\SWP\Bundle\CoreBundle\Model\Settings::class, groups={"api"}) |
||
| 172 | * ), |
||
| 173 | * @SWG\Response( |
||
| 174 | * response="404", |
||
| 175 | * description="Setting not found" |
||
| 176 | * ) |
||
| 177 | * ) |
||
| 178 | * |
||
| 179 | * @Route("/api/{version}/users/settings/", methods={"PATCH"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_user_update_settings") |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function updateSettings(Request $request): SingleResourceResponseInterface |
|
| 214 | } |
||
| 215 |
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: