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 |
||
| 31 | class CommentController extends RestController implements ClassResourceInterface |
||
| 32 | { |
||
| 33 | use RequestParametersTrait; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Returns list of field-descriptors. |
||
| 37 | * |
||
| 38 | * @Get("/comments/fields") |
||
| 39 | * |
||
| 40 | * @return Response |
||
| 41 | */ |
||
| 42 | public function getFieldsAction() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns list of comments. |
||
| 49 | * |
||
| 50 | * @param Request $request |
||
| 51 | * |
||
| 52 | * @return Response |
||
| 53 | */ |
||
| 54 | 3 | public function cgetAction(Request $request) |
|
| 55 | { |
||
| 56 | 3 | $restHelper = $this->get('sulu_core.doctrine_rest_helper'); |
|
| 57 | 3 | $factory = $this->get('sulu_core.doctrine_list_builder_factory'); |
|
| 58 | 3 | $listBuilder = $factory->create($this->getParameter('sulu.model.comment.class')); |
|
| 59 | |||
| 60 | 3 | $fieldDescriptors = $this->getFieldDescriptors(); |
|
| 61 | 3 | $restHelper->initializeListBuilder($listBuilder, $fieldDescriptors); |
|
| 62 | |||
| 63 | 3 | if ($request->query->get('threadType')) { |
|
| 64 | 1 | $listBuilder->in( |
|
| 65 | 1 | $fieldDescriptors['threadType'], |
|
| 66 | 1 | array_filter(explode(',', $request->query->get('threadType'))) |
|
| 67 | ); |
||
| 68 | |||
| 69 | 1 | $request->query->remove('threadType'); |
|
| 70 | } |
||
| 71 | |||
| 72 | 3 | View Code Duplication | foreach ($request->query->all() as $filterKey => $filterValue) { |
| 73 | 1 | if (isset($fieldDescriptors[$filterKey])) { |
|
| 74 | 1 | $listBuilder->where($fieldDescriptors[$filterKey], $filterValue); |
|
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | 3 | $results = $listBuilder->execute(); |
|
| 79 | 3 | $list = new ListRepresentation( |
|
| 80 | 3 | $results, |
|
| 81 | 3 | 'comments', |
|
| 82 | 3 | 'get_comments', |
|
| 83 | 3 | $request->query->all(), |
|
| 84 | 3 | $listBuilder->getCurrentPage(), |
|
| 85 | 3 | $listBuilder->getLimit(), |
|
| 86 | 3 | $listBuilder->count() |
|
| 87 | ); |
||
| 88 | |||
| 89 | 3 | return $this->handleView($this->view($list, 200)); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns single comment. |
||
| 94 | * |
||
| 95 | * @param int $id |
||
| 96 | * |
||
| 97 | * @return Response |
||
| 98 | * |
||
| 99 | * @throws EntityNotFoundException |
||
| 100 | */ |
||
| 101 | 1 | public function getAction($id) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Update comment. |
||
| 113 | * |
||
| 114 | * @param int $id |
||
| 115 | * @param Request $request |
||
| 116 | * |
||
| 117 | * @return Response |
||
| 118 | * |
||
| 119 | * @throws EntityNotFoundException |
||
| 120 | */ |
||
| 121 | 1 | View Code Duplication | public function putAction($id, Request $request) |
| 136 | |||
| 137 | /** |
||
| 138 | * Delete comment identified by id. |
||
| 139 | * |
||
| 140 | * @param int $id |
||
| 141 | * |
||
| 142 | * @return Response |
||
| 143 | */ |
||
| 144 | 1 | View Code Duplication | public function deleteAction($id) |
| 151 | |||
| 152 | /** |
||
| 153 | * Delete multiple comments identified by ids parameter. |
||
| 154 | * |
||
| 155 | * @param Request $request |
||
| 156 | * |
||
| 157 | * @return Response |
||
| 158 | */ |
||
| 159 | 1 | View Code Duplication | public function cdeleteAction(Request $request) |
| 172 | |||
| 173 | /** |
||
| 174 | * trigger a action for given comment specified over action get-parameter |
||
| 175 | * - publish: Publish a comment |
||
| 176 | * - unpublish: Unpublish a comment. |
||
| 177 | * |
||
| 178 | * @Post("/comments/{id}") |
||
| 179 | * |
||
| 180 | * @param int $id |
||
| 181 | * @param Request $request |
||
| 182 | * |
||
| 183 | * @return Response |
||
| 184 | * |
||
| 185 | * @throws RestException |
||
| 186 | */ |
||
| 187 | 2 | public function postTriggerAction($id, Request $request) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Returns array of field-descriptors. |
||
| 216 | * |
||
| 217 | * @return FieldDescriptorInterface[] |
||
| 218 | */ |
||
| 219 | 3 | private function getFieldDescriptors() |
|
| 224 | } |
||
| 225 |
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: