| Conditions | 8 |
| Paths | 6 |
| Total Lines | 61 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 41 | public function addAction(Request $request) |
||
| 42 | { |
||
| 43 | $cart = $this->getCurrentCart(); |
||
| 44 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
| 45 | |||
| 46 | $this->isGrantedOr403($configuration, CartActions::ADD); |
||
| 47 | /** @var OrderItemInterface $orderItem */ |
||
| 48 | $orderItem = $this->newResourceFactory->create($configuration, $this->factory); |
||
| 49 | |||
| 50 | $form = $this->getFormFactory()->create( |
||
| 51 | $configuration->getFormType(), |
||
| 52 | $this->createAddToCartCommand($cart, $orderItem), |
||
| 53 | $configuration->getFormOptions() |
||
| 54 | ); |
||
| 55 | |||
| 56 | if ($request->isMethod('POST') && $form->submit($request)->isValid()) { |
||
| 57 | /** @var AddToCartCommandInterface $addCartItemCommand */ |
||
| 58 | $addToCartCommand = $form->getData(); |
||
| 59 | |||
| 60 | $event = $this->eventDispatcher->dispatchPreEvent(CartActions::ADD, $configuration, $orderItem); |
||
| 61 | |||
| 62 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
| 63 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
| 64 | } |
||
| 65 | if ($event->isStopped()) { |
||
| 66 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
| 67 | |||
| 68 | return $this->redirectHandler->redirectToIndex($configuration, $orderItem); |
||
| 69 | } |
||
| 70 | |||
| 71 | $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem()); |
||
| 72 | |||
| 73 | $cartManager = $this->getCartManager(); |
||
| 74 | $cartManager->persist($cart); |
||
| 75 | $cartManager->flush(); |
||
| 76 | |||
| 77 | $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem); |
||
| 78 | |||
| 79 | if (!$configuration->isHtmlRequest()) { |
||
| 80 | return $this->viewHandler->handle($configuration, View::create($orderItem, Response::HTTP_CREATED)); |
||
| 81 | } |
||
| 82 | $this->flashHelper->addSuccessFlash($configuration, CartActions::ADD, $orderItem); |
||
| 83 | |||
| 84 | return $this->redirectHandler->redirectToResource($configuration, $orderItem); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!$configuration->isHtmlRequest()) { |
||
| 88 | return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
||
| 89 | } |
||
| 90 | |||
| 91 | $view = View::create() |
||
| 92 | ->setData([ |
||
| 93 | 'configuration' => $configuration, |
||
| 94 | $this->metadata->getName() => $orderItem, |
||
| 95 | 'form' => $form->createView(), |
||
| 96 | ]) |
||
| 97 | ->setTemplate($configuration->getTemplate(CartActions::ADD . '.html')) |
||
| 98 | ; |
||
| 99 | |||
| 100 | return $this->viewHandler->handle($configuration, $view); |
||
| 101 | } |
||
| 102 | |||
| 235 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.