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 |
||
| 21 | class FileController extends RestController |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var FileManager |
||
| 25 | */ |
||
| 26 | private $fileManager; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var RequestManager |
||
| 30 | */ |
||
| 31 | private $requestManager; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * On build time we inject the Manager |
||
| 35 | * |
||
| 36 | * @param FileManager $fileManager Service Manager |
||
| 37 | * |
||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | public function setFileManager(FileManager $fileManager) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * set RequestManager |
||
| 47 | * |
||
| 48 | * @param RequestManager $requestManager requestManager |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public function setRequestManager($requestManager) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Writes a new Entry to the database |
||
| 59 | * Can accept either direct Post data or Form upload |
||
| 60 | * |
||
| 61 | * @param Request $request Current http request |
||
| 62 | * |
||
| 63 | * @return Response $response Result of action with data (if successful) |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function postAction(Request $request) |
|
| 66 | { |
||
| 67 | $file = new File(); |
||
| 68 | if ($formData = $request->get('metadata')) { |
||
| 69 | $file = $this->validateRequest($formData, $this->getModel()); |
||
| 70 | } |
||
| 71 | |||
| 72 | $request = $this->requestManager->updateFileRequest($request); |
||
| 73 | |||
| 74 | $file = $this->fileManager->handleSaveRequest($file, $request, $this->getModel()); |
||
|
|
|||
| 75 | |||
| 76 | // Set status code and content |
||
| 77 | $response = $this->getResponse(); |
||
| 78 | $response->setStatusCode(Response::HTTP_CREATED); |
||
| 79 | $response->headers->set( |
||
| 80 | 'Location', |
||
| 81 | $this->getRouter()->generate('gravitondyn.file.rest.file.get', array('id' => $file->getId())) |
||
| 82 | ); |
||
| 83 | return $response; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * respond with document if non json mime-type is requested |
||
| 88 | * |
||
| 89 | * @param Request $request Current http request |
||
| 90 | * @param string $id id of file |
||
| 91 | * |
||
| 92 | * @return Response |
||
| 93 | */ |
||
| 94 | public function getAction(Request $request, $id) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Update a record |
||
| 113 | * |
||
| 114 | * @param Number $id ID of record |
||
| 115 | * @param Request $request Current http request |
||
| 116 | * |
||
| 117 | * @return Response $response Result of action with data (if successful) |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function putAction($id, Request $request) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Deletes a record |
||
| 143 | * |
||
| 144 | * @param Number $id ID of record |
||
| 145 | * |
||
| 146 | * @return Response $response Result of the action |
||
| 147 | */ |
||
| 148 | public function deleteAction($id) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Patch a record, we add here a patch on Modification Data. |
||
| 157 | * |
||
| 158 | * @param Number $id ID of record |
||
| 159 | * @param Request $request Current http request |
||
| 160 | * |
||
| 161 | * @throws MalformedInputException |
||
| 162 | * |
||
| 163 | * @return Response $response Result of action with data (if successful) |
||
| 164 | */ |
||
| 165 | public function patchAction($id, Request $request) |
||
| 196 | } |
||
| 197 |
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: