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 |
||
| 47 | class ArticleController extends RestController implements ClassResourceInterface, SecuredControllerInterface |
||
| 48 | { |
||
| 49 | const DOCUMENT_TYPE = 'article'; |
||
| 50 | |||
| 51 | use RequestParametersTrait; |
||
| 52 | use ArticleViewDocumentIdTrait; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Create field-descriptor array. |
||
| 56 | * |
||
| 57 | * @return FieldDescriptor[] |
||
| 58 | */ |
||
| 59 | private function getFieldDescriptors() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Returns fields. |
||
| 81 | * |
||
| 82 | * @return Response |
||
| 83 | */ |
||
| 84 | public function cgetFieldsAction() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns list of articles. |
||
| 93 | * |
||
| 94 | * @param Request $request |
||
| 95 | * |
||
| 96 | * @return Response |
||
| 97 | */ |
||
| 98 | 12 | public function cgetAction(Request $request) |
|
| 99 | { |
||
| 100 | 12 | $locale = $this->getRequestParameter($request, 'locale', true); |
|
| 101 | |||
| 102 | 12 | $restHelper = $this->get('sulu_core.list_rest_helper'); |
|
| 103 | |||
| 104 | /** @var Manager $manager */ |
||
| 105 | 12 | $manager = $this->get('es.manager.default'); |
|
| 106 | 12 | $repository = $manager->getRepository($this->get('sulu_article.view_document.factory')->getClass('article')); |
|
| 107 | 12 | $search = $repository->createSearch(); |
|
| 108 | |||
| 109 | 12 | $limit = (int) $restHelper->getLimit(); |
|
| 110 | 12 | $page = (int) $restHelper->getPage(); |
|
| 111 | |||
| 112 | 12 | if (null !== $locale) { |
|
| 113 | 12 | $search->addQuery(new TermQuery('locale', $locale)); |
|
| 114 | } |
||
| 115 | |||
| 116 | 12 | if (count($ids = array_filter(explode(',', $request->get('ids', ''))))) { |
|
| 117 | 1 | $search->addQuery(new IdsQuery($this->getViewDocumentIds($ids, $locale))); |
|
| 118 | 1 | $limit = count($ids); |
|
| 119 | } |
||
| 120 | |||
| 121 | 12 | if (!empty($searchPattern = $restHelper->getSearchPattern()) |
|
| 122 | 12 | && 0 < count($searchFields = $restHelper->getSearchFields()) |
|
| 123 | ) { |
||
| 124 | 2 | $search->addQuery(new MultiMatchQuery($searchFields, $searchPattern)); |
|
| 125 | } |
||
| 126 | |||
| 127 | 12 | if (null !== ($type = $request->get('type'))) { |
|
| 128 | 10 | $search->addQuery(new TermQuery('type', $type)); |
|
| 129 | } |
||
| 130 | |||
| 131 | 12 | if (null !== ($contactId = $request->get('contactId'))) { |
|
| 132 | 1 | $boolQuery = new BoolQuery(); |
|
| 133 | 1 | $boolQuery->add(new MatchQuery('changer_contact_id', $contactId), BoolQuery::SHOULD); |
|
| 134 | 1 | $boolQuery->add(new MatchQuery('creator_contact_id', $contactId), BoolQuery::SHOULD); |
|
| 135 | 1 | $boolQuery->add(new MatchQuery('author_id', $contactId), BoolQuery::SHOULD); |
|
| 136 | 1 | $search->addQuery($boolQuery); |
|
| 137 | } |
||
| 138 | |||
| 139 | 12 | if (null !== ($categoryId = $request->get('categoryId'))) { |
|
| 140 | 1 | $search->addQuery(new TermQuery('excerpt.categories.id', $categoryId), BoolQuery::MUST); |
|
| 141 | } |
||
| 142 | |||
| 143 | 12 | if (null === $search->getQueries()) { |
|
| 144 | $search->addQuery(new MatchAllQuery()); |
||
| 145 | } |
||
| 146 | |||
| 147 | 12 | $count = $repository->count($search); |
|
| 148 | |||
| 149 | 12 | if (null !== $restHelper->getSortColumn()) { |
|
| 150 | 1 | $search->addSort( |
|
| 151 | 1 | new FieldSort($this->uncamelize($restHelper->getSortColumn()), $restHelper->getSortOrder()) |
|
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | 12 | $search->setSize($limit); |
|
| 156 | 12 | $search->setFrom(($page - 1) * $limit); |
|
| 157 | |||
| 158 | 12 | $result = []; |
|
| 159 | 12 | foreach ($repository->findDocuments($search) as $document) { |
|
| 160 | 10 | if (false !== ($index = array_search($document->getUuid(), $ids))) { |
|
| 161 | 1 | $result[$index] = $document; |
|
| 162 | } else { |
||
| 163 | 12 | $result[] = $document; |
|
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | 12 | if (count($ids)) { |
|
| 168 | 1 | ksort($result); |
|
| 169 | 1 | $result = array_values($result); |
|
| 170 | } |
||
| 171 | |||
| 172 | 12 | return $this->handleView( |
|
| 173 | 12 | $this->view( |
|
| 174 | 12 | new ListRepresentation( |
|
| 175 | $result, |
||
| 176 | 12 | 'articles', |
|
| 177 | 12 | 'get_articles', |
|
| 178 | 12 | $request->query->all(), |
|
| 179 | $page, |
||
| 180 | $limit, |
||
| 181 | 12 | $count |
|
| 182 | ) |
||
| 183 | ) |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns single article. |
||
| 189 | * |
||
| 190 | * @param string $uuid |
||
| 191 | * @param Request $request |
||
| 192 | * |
||
| 193 | * @return Response |
||
| 194 | */ |
||
| 195 | 9 | public function getAction($uuid, Request $request) |
|
| 196 | { |
||
| 197 | 9 | $locale = $this->getRequestParameter($request, 'locale', true); |
|
| 198 | 9 | $document = $this->getDocumentManager()->find( |
|
| 199 | $uuid, |
||
| 200 | $locale, |
||
| 201 | [ |
||
| 202 | 9 | 'load_ghost_content' => true, |
|
| 203 | 'load_shadow_content' => false, |
||
| 204 | ] |
||
| 205 | ); |
||
| 206 | |||
| 207 | 9 | return $this->handleView( |
|
| 208 | 9 | $this->view($document)->setSerializationContext( |
|
| 209 | 9 | SerializationContext::create() |
|
| 210 | 9 | ->setSerializeNull(true) |
|
| 211 | 9 | ->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage']) |
|
| 212 | ) |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Create article. |
||
| 218 | * |
||
| 219 | * @param Request $request |
||
| 220 | * |
||
| 221 | * @return Response |
||
| 222 | */ |
||
| 223 | 48 | View Code Duplication | public function postAction(Request $request) |
| 242 | |||
| 243 | /** |
||
| 244 | * Update articles. |
||
| 245 | * |
||
| 246 | * @param Request $request |
||
| 247 | * @param string $uuid |
||
| 248 | * |
||
| 249 | * @return Response |
||
| 250 | */ |
||
| 251 | 9 | View Code Duplication | public function putAction(Request $request, $uuid) |
| 252 | { |
||
| 253 | 9 | $locale = $this->getRequestParameter($request, 'locale', true); |
|
| 254 | 9 | $action = $request->get('action'); |
|
| 255 | 9 | $data = $request->request->all(); |
|
| 256 | |||
| 257 | 9 | $document = $this->getDocumentManager()->find( |
|
| 258 | $uuid, |
||
| 259 | $locale, |
||
| 260 | [ |
||
| 261 | 9 | 'load_ghost_content' => false, |
|
| 262 | 'load_shadow_content' => false, |
||
| 263 | ] |
||
| 264 | ); |
||
| 265 | |||
| 266 | 9 | $this->get('sulu_hash.request_hash_checker')->checkHash($request, $document, $document->getUuid()); |
|
| 267 | |||
| 268 | 9 | $this->persistDocument($data, $document, $locale); |
|
| 269 | 9 | $this->handleActionParameter($action, $document, $locale); |
|
| 270 | 9 | $this->getDocumentManager()->flush(); |
|
| 271 | |||
| 272 | 9 | return $this->handleView( |
|
| 273 | 9 | $this->view($document)->setSerializationContext( |
|
| 274 | 9 | SerializationContext::create() |
|
| 275 | 9 | ->setSerializeNull(true) |
|
| 276 | 9 | ->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage']) |
|
| 277 | ) |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Deletes multiple documents. |
||
| 283 | * |
||
| 284 | * @param Request $request |
||
| 285 | * |
||
| 286 | * @return Response |
||
| 287 | */ |
||
| 288 | 1 | public function cdeleteAction(Request $request) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Deletes multiple documents. |
||
| 304 | * |
||
| 305 | * @param string $id |
||
| 306 | * |
||
| 307 | * @return Response |
||
| 308 | */ |
||
| 309 | 1 | public function deleteAction($id) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Trigger a action for given article specified over get-action parameter. |
||
| 321 | * |
||
| 322 | * @Post("/articles/{uuid}") |
||
| 323 | * |
||
| 324 | * @param string $uuid |
||
| 325 | * @param Request $request |
||
| 326 | * |
||
| 327 | * @return Response |
||
| 328 | */ |
||
| 329 | 1 | public function postTriggerAction($uuid, Request $request) |
|
| 402 | |||
| 403 | private function orderPages(array $pages, $locale) |
||
| 413 | |||
| 414 | 48 | /** |
|
| 415 | * {@inheritdoc} |
||
| 416 | 48 | */ |
|
| 417 | 48 | public function getSecurityContext() |
|
| 421 | 48 | ||
| 422 | /** |
||
| 423 | * Persists the document using the given information. |
||
| 424 | 48 | * |
|
| 425 | * @param array $data |
||
| 426 | 48 | * @param object $document |
|
| 427 | * @param string $locale |
||
| 428 | * |
||
| 429 | * @throws InvalidFormException |
||
| 430 | 48 | * @throws MissingParameterException |
|
| 431 | */ |
||
| 432 | private function persistDocument($data, $document, $locale) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Returns document-manager. |
||
| 460 | * |
||
| 461 | * @return DocumentManagerInterface |
||
| 462 | */ |
||
| 463 | protected function getDocumentManager() |
||
| 467 | |||
| 468 | 48 | /** |
|
| 469 | 15 | * @return ContentMapperInterface |
|
| 470 | 15 | */ |
|
| 471 | protected function getMapper() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Delegates actions by given actionParameter, which can be retrieved from the request. |
||
| 478 | * |
||
| 479 | * @param string $actionParameter |
||
| 480 | * @param object $document |
||
| 481 | 1 | * @param string $locale |
|
| 482 | */ |
||
| 483 | 1 | private function handleActionParameter($actionParameter, $document, $locale) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Converts camel case string into normalized string with underscore. |
||
| 494 | * |
||
| 495 | * @param string $camel |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | private function uncamelize($camel) |
||
| 509 | } |
||
| 510 |
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: