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 |
||
| 32 | abstract class CRUDController extends PollSetterController |
||
| 33 | { |
||
| 34 | // Member variables that must be defined in the custom controller |
||
| 35 | protected $entityName; |
||
| 36 | protected $cancelRoute; |
||
| 37 | protected $successRoute; |
||
| 38 | protected $deleteRoute; |
||
| 39 | protected $deleteSuccessRoute; |
||
| 40 | |||
| 41 | // Methods to be implemented in the custom controller |
||
| 42 | abstract public function newAction(Request $request); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Adds pollUrl into the parameters if not explicitly set |
||
| 48 | * |
||
| 49 | * @param string $route |
||
| 50 | * @param mixed $parameters |
||
| 51 | * @param Boolean $absolute |
||
| 52 | */ |
||
| 53 | public function generateUrlWithPoll($route, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
| 54 | { |
||
| 55 | if (!isset($parameters['pollUrl']) && $this->poll) |
||
| 56 | { |
||
| 57 | $parameters['pollUrl'] = $this->poll->getUrl(); |
||
| 58 | } |
||
| 59 | return parent::generateUrl($route, $parameters, $absolute); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Create a form to create a new entity, and create it when the form is submited |
||
| 64 | */ |
||
| 65 | protected function doNewAction($formType, Entity $entity, Request $request, $successMessage = null, $extra = []) |
||
| 66 | { |
||
| 67 | $form = $this->doCreateCreateForm($formType, $entity, $request->get('_route'), $extra); |
||
| 68 | if ($request->isMethod('POST')) |
||
| 69 | { |
||
| 70 | $form->handleRequest($request); |
||
| 71 | |||
| 72 | if ($entity instanceof Poll) |
||
| 73 | { |
||
| 74 | $this->poll = $entity; |
||
| 75 | } |
||
| 76 | else |
||
| 77 | { |
||
| 78 | $entity->setPoll($this->poll); |
||
| 79 | } |
||
| 80 | |||
| 81 | View Code Duplication | if ($form->get('actions')->has('cancel') && $form->get('actions')->get('cancel')->isClicked()) { |
|
| 82 | return $this->redirect($this->generateUrlWithPoll($this->cancelRoute)); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($form->isValid()) { |
||
| 86 | $em = $this->getDoctrine()->getManager(); |
||
| 87 | $em->persist($entity); |
||
| 88 | $em->flush(); |
||
| 89 | |||
| 90 | if ($successMessage) { |
||
| 91 | $request->getSession()->getFlashBag()->add('success', $successMessage); |
||
| 92 | } |
||
| 93 | return $this->redirect($this->generateUrlWithPoll($this->successRoute)); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return [ |
||
| 98 | 'poll' => $this->poll, |
||
| 99 | 'entity' => $entity, |
||
| 100 | 'form' => $form->createView(), |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Create a form to edit an entity, and update it when the form is submited |
||
| 106 | * |
||
| 107 | * @param string $formType |
||
| 108 | * @param int $id The entity id |
||
| 109 | * @param Request $request |
||
| 110 | */ |
||
| 111 | protected function doEditAction($formType, $id, Request $request, $extra = []) |
||
| 112 | { |
||
| 113 | $em = $this->getDoctrine()->getManager(); |
||
| 114 | |||
| 115 | /** @var Entity $entity */ |
||
| 116 | $entity = $em->getRepository($this->entityName)->find($id); |
||
| 117 | |||
| 118 | if (!$entity) { |
||
| 119 | throw $this->createNotFoundException("Unable to find entity."); |
||
| 120 | } |
||
| 121 | |||
| 122 | $deleteForm = $this->createDeleteForm($id); |
||
| 123 | $editForm = $this->doCreateEditForm($formType, $entity, $request->get('_route'), $extra); |
||
| 124 | if ($request->isMethod('PUT')) |
||
| 125 | { |
||
| 126 | $editForm->handleRequest($request); |
||
| 127 | |||
| 128 | View Code Duplication | if ($editForm->get('actions')->get('cancel')->isClicked()) { |
|
| 129 | $em->refresh($entity); |
||
| 130 | return $this->redirect($this->generateUrlWithPoll($this->cancelRoute)); |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($editForm->isValid()) { |
||
| 134 | $em->flush(); |
||
| 135 | return $this->redirect($this->generateUrlWithPoll($this->successRoute)); |
||
| 136 | } |
||
| 137 | else { |
||
| 138 | $em->refresh($entity); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return [ |
||
| 143 | 'poll' => $this->poll, |
||
| 144 | 'entity' => $entity, |
||
| 145 | 'edit_form' => $editForm->createView(), |
||
| 146 | 'delete_form' => $deleteForm->createView(), |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Deletes an entity |
||
| 152 | * |
||
| 153 | * @param Request $request |
||
| 154 | * @param mixed $id The entity id |
||
| 155 | * |
||
| 156 | */ |
||
| 157 | public function doDeleteAction(Request $request, $id) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Creates a form to create an entity |
||
| 183 | * |
||
| 184 | * @param FormTypeInterface $formType The form builder |
||
| 185 | * @param Entity $entity The new entity |
||
| 186 | * @param string $action The name of the route to the action |
||
| 187 | * |
||
| 188 | * @return \Symfony\Component\Form\Form The form |
||
| 189 | */ |
||
| 190 | protected function doCreateCreateForm($formType, Entity $entity, $action, $extra) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Creates a form to edit an entity |
||
| 213 | * |
||
| 214 | * @param string $formType The form builder |
||
| 215 | * @param Entity $entity The entity to edit |
||
| 216 | * @param string $action The name of the route to the action |
||
| 217 | * |
||
| 218 | * @return \Symfony\Component\Form\Form The form |
||
| 219 | */ |
||
| 220 | protected function doCreateEditForm($formType, Entity $entity, $action, $extra = []) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Creates a form to delete an entity by id. |
||
| 239 | * |
||
| 240 | * @param int $id The entity id |
||
| 241 | * |
||
| 242 | * @return \Symfony\Component\Form\Form The form |
||
| 243 | */ |
||
| 244 | protected function createDeleteForm($id) |
||
| 253 | } |
||
| 254 |
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: