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() |
||
| 60 | { |
||
| 61 | return [ |
||
| 62 | 'uuid' => new FieldDescriptor('uuid', 'public.id', true), |
||
| 63 | 'typeTranslation' => new FieldDescriptor( |
||
| 64 | 'typeTranslation', |
||
| 65 | 'sulu_article.list.type', |
||
| 66 | !$this->getParameter('sulu_article.display_tab_all'), |
||
| 67 | false |
||
| 68 | ), |
||
| 69 | 'title' => new FieldDescriptor('title', 'public.title', false, true), |
||
| 70 | 'creatorFullName' => new FieldDescriptor('creatorFullName', 'sulu_article.list.creator', true, false), |
||
| 71 | 'changerFullName' => new FieldDescriptor('changerFullName', 'sulu_article.list.changer', false, false), |
||
| 72 | 'authorFullName' => new FieldDescriptor('authorFullName', 'sulu_article.author', false, false), |
||
| 73 | 'created' => new FieldDescriptor('created', 'public.created', true, false, 'datetime'), |
||
| 74 | 'changed' => new FieldDescriptor('changed', 'public.changed', false, false, 'datetime'), |
||
| 75 | 'authored' => new FieldDescriptor('authored', 'sulu_article.authored', false, false, 'date'), |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Returns fields. |
||
| 81 | * |
||
| 82 | * @return Response |
||
| 83 | */ |
||
| 84 | public function cgetFieldsAction() |
||
| 85 | { |
||
| 86 | $fieldDescriptors = $this->getFieldDescriptors(); |
||
| 87 | |||
| 88 | return $this->handleView($this->view(array_values($fieldDescriptors))); |
||
|
|
|||
| 89 | } |
||
| 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 | $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 | 43 | View Code Duplication | public function postAction(Request $request) |
| 224 | { |
||
| 225 | 43 | $action = $request->get('action'); |
|
| 226 | 43 | $document = $this->getDocumentManager()->create(self::DOCUMENT_TYPE); |
|
| 227 | 43 | $locale = $this->getRequestParameter($request, 'locale', true); |
|
| 228 | 43 | $data = $request->request->all(); |
|
| 229 | |||
| 230 | 43 | $this->persistDocument($data, $document, $locale); |
|
| 231 | 43 | $this->handleActionParameter($action, $document, $locale); |
|
| 232 | 43 | $this->getDocumentManager()->flush(); |
|
| 233 | |||
| 234 | 43 | return $this->handleView( |
|
| 235 | 43 | $this->view($document)->setSerializationContext( |
|
| 236 | 43 | SerializationContext::create() |
|
| 237 | 43 | ->setSerializeNull(true) |
|
| 238 | 43 | ->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage']) |
|
| 239 | ) |
||
| 240 | ); |
||
| 241 | } |
||
| 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) |
|
| 289 | { |
||
| 290 | 1 | $ids = array_filter(explode(',', $request->get('ids', ''))); |
|
| 291 | |||
| 292 | 1 | $documentManager = $this->getDocumentManager(); |
|
| 293 | 1 | foreach ($ids as $id) { |
|
| 294 | 1 | $document = $documentManager->find($id); |
|
| 295 | 1 | $documentManager->remove($document); |
|
| 296 | 1 | $documentManager->flush(); |
|
| 297 | } |
||
| 298 | |||
| 299 | 1 | return $this->handleView($this->view(null)); |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Deletes multiple documents. |
||
| 304 | * |
||
| 305 | * @param string $id |
||
| 306 | * |
||
| 307 | * @return Response |
||
| 308 | */ |
||
| 309 | 1 | public function deleteAction($id) |
|
| 310 | { |
||
| 311 | 1 | $documentManager = $this->getDocumentManager(); |
|
| 312 | 1 | $document = $documentManager->find($id); |
|
| 313 | 1 | $documentManager->remove($document); |
|
| 314 | 1 | $documentManager->flush(); |
|
| 315 | |||
| 316 | 1 | return $this->handleView($this->view(null)); |
|
| 317 | } |
||
| 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) |
|
| 330 | { |
||
| 331 | // extract parameter |
||
| 332 | 1 | $action = $this->getRequestParameter($request, 'action', true); |
|
| 333 | 1 | $locale = $this->getRequestParameter($request, 'locale', true); |
|
| 334 | |||
| 335 | // prepare vars |
||
| 336 | 1 | $view = null; |
|
| 337 | 1 | $data = null; |
|
| 338 | 1 | $userId = $this->getUser()->getId(); |
|
| 339 | |||
| 340 | try { |
||
| 341 | switch ($action) { |
||
| 342 | 1 | case 'unpublish': |
|
| 343 | $document = $this->getDocumentManager()->find($uuid, $locale); |
||
| 344 | $this->getDocumentManager()->unpublish($document, $locale); |
||
| 345 | $this->getDocumentManager()->flush(); |
||
| 346 | |||
| 347 | $data = $this->getDocumentManager()->find($uuid, $locale); |
||
| 348 | break; |
||
| 349 | 1 | case 'remove-draft': |
|
| 350 | $data = $this->getDocumentManager()->find($uuid, $locale); |
||
| 351 | $this->getDocumentManager()->removeDraft($data, $locale); |
||
| 352 | $this->getDocumentManager()->flush(); |
||
| 353 | break; |
||
| 354 | 1 | case 'copy-locale': |
|
| 355 | 1 | $destLocales = $this->getRequestParameter($request, 'dest', true); |
|
| 356 | 1 | $destLocales = explode(',', $destLocales); |
|
| 357 | |||
| 358 | 1 | $securityChecker = $this->get('sulu_security.security_checker'); |
|
| 359 | 1 | foreach ($destLocales as $destLocale) { |
|
| 360 | 1 | $securityChecker->checkPermission( |
|
| 361 | 1 | new SecurityCondition($this->getSecurityContext(), $destLocale), |
|
| 362 | 1 | PermissionTypes::EDIT |
|
| 363 | ); |
||
| 364 | } |
||
| 365 | |||
| 366 | 1 | $this->getMapper()->copyLanguage($uuid, $userId, null, $locale, $destLocales); |
|
| 367 | |||
| 368 | 1 | $data = $this->getDocumentManager()->find($uuid, $locale); |
|
| 369 | 1 | break; |
|
| 370 | case 'copy': |
||
| 371 | /** @var ArticleDocument $document */ |
||
| 372 | $document = $this->getDocumentManager()->find($uuid, $locale); |
||
| 373 | $copiedPath = $this->getDocumentManager()->copy($document, dirname($document->getPath())); |
||
| 374 | $this->getDocumentManager()->flush(); |
||
| 375 | |||
| 376 | $data = $this->getDocumentManager()->find($copiedPath, $locale); |
||
| 377 | break; |
||
| 378 | default: |
||
| 379 | throw new RestException('Unrecognized action: ' . $action); |
||
| 380 | } |
||
| 381 | |||
| 382 | // prepare view |
||
| 383 | 1 | $view = $this->view($data); |
|
| 384 | 1 | $view->setSerializationContext( |
|
| 385 | 1 | SerializationContext::create() |
|
| 386 | 1 | ->setSerializeNull(true) |
|
| 387 | 1 | ->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage']) |
|
| 388 | ); |
||
| 389 | } catch (RestException $exc) { |
||
| 390 | $view = $this->view($exc->toArray(), 400); |
||
| 391 | } |
||
| 392 | |||
| 393 | 1 | return $this->handleView($view); |
|
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * {@inheritdoc} |
||
| 398 | */ |
||
| 399 | 44 | public function getSecurityContext() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Persists the document using the given information. |
||
| 406 | * |
||
| 407 | * @param array $data |
||
| 408 | * @param object $document |
||
| 409 | * @param string $locale |
||
| 410 | * |
||
| 411 | * @throws InvalidFormException |
||
| 412 | * @throws MissingParameterException |
||
| 413 | */ |
||
| 414 | 43 | private function persistDocument($data, $document, $locale) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Returns document-manager. |
||
| 442 | * |
||
| 443 | * @return DocumentManagerInterface |
||
| 444 | */ |
||
| 445 | 43 | protected function getDocumentManager() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @return ContentMapperInterface |
||
| 452 | */ |
||
| 453 | 1 | protected function getMapper() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Delegates actions by given actionParameter, which can be retrieved from the request. |
||
| 460 | * |
||
| 461 | * @param string $actionParameter |
||
| 462 | * @param object $document |
||
| 463 | * @param string $locale |
||
| 464 | */ |
||
| 465 | 43 | private function handleActionParameter($actionParameter, $document, $locale) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Converts camel case string into normalized string with underscore. |
||
| 476 | * |
||
| 477 | * @param string $camel |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | 1 | private function uncamelize($camel) |
|
| 491 | } |
||
| 492 |
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: