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 |
||
| 35 | class InventoryController extends AbstractInventoryController |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Lists all Inventory entities. |
||
| 39 | * |
||
| 40 | * @Route("/", name="inventory") |
||
| 41 | * @Method("GET") |
||
| 42 | * @Template() |
||
| 43 | * |
||
| 44 | * @param \Symfony\Component\HttpFoundation\Request $request Paginate request |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function indexAction(Request $request) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Finds and displays a Inventory entity. |
||
| 65 | * |
||
| 66 | * @Route("/{id}/show", name="inventory_show", requirements={"id"="\d+"}) |
||
| 67 | * @Method("GET") |
||
| 68 | * @Template() |
||
| 69 | * |
||
| 70 | * @param \AppBundle\Entity\Inventory $inventory Inventory item to display |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function showAction(Inventory $inventory) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Creates a new Inventory entity. |
||
| 97 | * |
||
| 98 | * @Route("/admin/create", name="inventory_create") |
||
| 99 | * @Method("PUT") |
||
| 100 | * |
||
| 101 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 102 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 103 | */ |
||
| 104 | public function createAction(Request $request) |
||
| 105 | { |
||
| 106 | $etm = $this->getDoctrine()->getManager(); |
||
| 107 | $articles = $etm->getRepository('AppBundle:Article')->getResultArticles(); |
||
| 108 | $settings = $etm->getRepository('AppBundle:Settings')->find(1); |
||
| 109 | |||
| 110 | $inventory = new Inventory(); |
||
| 111 | $form = $this->createCreateForm('inventory_create'); |
||
| 112 | if ($form->handleRequest($request)->isValid()) { |
||
| 113 | $etm->persist($inventory); |
||
| 114 | |||
| 115 | // Saving the first inventory |
||
| 116 | if (empty($settings->getFirstInventory)) { |
||
| 117 | $settings->setFirstInventory($inventory->getDate()); |
||
| 118 | $etm->persist($settings); |
||
| 119 | } |
||
| 120 | // Saving of articles in the inventory |
||
| 121 | View Code Duplication | foreach ($articles as $article) { |
|
| 122 | foreach ($article->getZoneStorages()->getSnapshot() as $zoneStorage) { |
||
| 123 | $inventoryArticles = new InventoryArticles(); |
||
| 124 | $inventoryArticles->setArticle($article); |
||
| 125 | $inventoryArticles->setInventory($inventory); |
||
| 126 | $inventoryArticles->setQuantity($article->getQuantity()); |
||
| 127 | $inventoryArticles->setRealstock(0); |
||
| 128 | $inventoryArticles->setUnitStorage($article->getUnitStorage()); |
||
| 129 | $inventoryArticles->setPrice($article->getPrice()); |
||
| 130 | $inventoryArticles->setZoneStorage($zoneStorage->getName()); |
||
| 131 | $etm->persist($inventoryArticles); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | $etm->flush(); |
||
| 135 | |||
| 136 | return $this->redirectToRoute( |
||
| 137 | 'inventory_print_prepare', |
||
| 138 | array('id' => $inventory->getId(), 'inventoryStyle' => $settings->getInventoryStyle()) |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Displays a form to edit an existing Inventory entity. |
||
| 145 | * |
||
| 146 | * @Route("/admin/{id}/edit", name="inventory_edit", requirements={"id"="\d+"}) |
||
| 147 | * @Method("GET") |
||
| 148 | * @Template() |
||
| 149 | * |
||
| 150 | * @param \AppBundle\Entity\Inventory $inventory Inventory item to edit |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function editAction(Inventory $inventory) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Edits an existing Inventory entity. |
||
| 162 | * |
||
| 163 | * @Route("/admin/{id}/update", name="inventory_update", requirements={"id"="\d+"}) |
||
| 164 | * @Method("PUT") |
||
| 165 | * @Template("AppBundle:Inventory:edit.html.twig") |
||
| 166 | * |
||
| 167 | * @param \AppBundle\Entity\Inventory $inventory Inventory item to update |
||
| 168 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | public function updateAction(Inventory $inventory, Request $request) |
||
| 172 | { |
||
| 173 | $return = $this->getInvetoryEditType($inventory); |
||
| 174 | |||
| 175 | if ($return['editForm']->handleRequest($request)->isValid()) { |
||
| 176 | $inventory->setStatus('2'); |
||
| 177 | $this->getDoctrine()->getManager()->flush(); |
||
| 178 | |||
| 179 | $return = $this->redirectToRoute( |
||
| 180 | 'inventory_edit', |
||
| 181 | array('id' => $inventory->getId(), 'zoneStorages' => $return['zoneStorages'],) |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | return $return; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Displays a form to valid an existing Inventory entity. |
||
| 189 | * |
||
| 190 | * @Route("/admin/{id}/valid", name="inventory_valid", requirements={"id"="\d+"}) |
||
| 191 | * @Method("GET") |
||
| 192 | * @Template() |
||
| 193 | * |
||
| 194 | * @param \AppBundle\Entity\Inventory $inventory Inventory item to validate |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function validAction(Inventory $inventory) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Close an existing Inventory entity. |
||
| 214 | * |
||
| 215 | * @Route("/admin/{id}/close", name="inventory_close", requirements={"id"="\d+"}) |
||
| 216 | * @Method("PUT") |
||
| 217 | * @Template("AppBundle:Inventory:valid.html.twig") |
||
| 218 | * |
||
| 219 | * @param \AppBundle\Entity\Inventory $inventory Inventory item to close |
||
| 220 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
| 221 | * @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 222 | */ |
||
| 223 | public function closeAction(Inventory $inventory, Request $request) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Deletes a Inventory entity. |
||
| 263 | * |
||
| 264 | * @Route("/admin/{id}/delete", name="inventory_delete", requirements={"id"="\d+"}) |
||
| 265 | * @Method("DELETE") |
||
| 266 | * |
||
| 267 | * @param \AppBundle\Entity\Inventory $inventory Inventory item to delete |
||
| 268 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
| 269 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 270 | */ |
||
| 271 | public function deleteAction(Inventory $inventory, Request $request) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Print the current inventory.<br />Creating a `PDF` file for viewing on paper |
||
| 280 | * |
||
| 281 | * @Route("/{id}/print/", name="inventory_print", requirements={"id"="\d+"}) |
||
| 282 | * @Method("GET") |
||
| 283 | * @Template() |
||
| 284 | * |
||
| 285 | * @param \AppBundle\Entity\Inventory $inventory Inventory item to print |
||
| 286 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 287 | */ |
||
| 288 | public function printAction(Inventory $inventory) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Print the preparation of inventory.<br />Creating a `PDF` file for viewing on paper |
||
| 309 | * |
||
| 310 | * @Route("/{id}/print/{inventoryStyle}/prepare", name="inventory_print_prepare", requirements={"id"="\d+"}) |
||
| 311 | * @Method("GET") |
||
| 312 | * @Template() |
||
| 313 | * |
||
| 314 | * @param \AppBundle\Entity\Inventory $inventory Inventory to print |
||
| 315 | * @param string $inventoryStyle Style of inventory |
||
| 316 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 317 | */ |
||
| 318 | public function prepareDataAction(Inventory $inventory, $inventoryStyle) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * get Html for PDF file. |
||
| 335 | * |
||
| 336 | * @param \AppBundle\Entity\Inventory $inventory Inventory entity |
||
| 337 | * @param string $inventoryStyle Style of inventory |
||
| 338 | * @return string The rendered view |
||
| 339 | */ |
||
| 340 | private function getHtml(Inventory $inventory, $inventoryStyle) |
||
| 360 | } |
||
| 361 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.