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 |
||
| 26 | abstract class AbstractController extends Controller |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Lists all items entity. |
||
| 30 | * |
||
| 31 | * @param string $entityName Name of Entity |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | public function abstractIndexAction($entityName) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Finds and displays an item entity. |
||
| 47 | * |
||
| 48 | * @param Object $entity Entity |
||
| 49 | * @param string $entityName Name of Entity |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | public function abstractShowAction($entity, $entityName) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Displays a form to create a new item entity. |
||
| 64 | * |
||
| 65 | * @param string $entity Entity |
||
| 66 | * @param string $entityPath Path of Entity |
||
| 67 | * @param string $typePath Path of FormType |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function abstractNewAction($entity, $entityPath, $typePath) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Creates a new item entity. |
||
| 92 | * |
||
| 93 | * @param Request $request Request in progress |
||
| 94 | * @param string $entity Entity |
||
| 95 | * @param string $entityPath Path of Entity |
||
| 96 | * @param string $typePath Path of FormType |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Displays a form to edit an existing item entity. |
||
| 131 | * |
||
| 132 | * @param Object $entity Entity |
||
| 133 | * @param string $entityName Name of Entity |
||
| 134 | * @param string $typePath Path of FormType |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function abstractEditAction($entity, $entityName, $typePath) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Edits an existing item entity. |
||
| 159 | * |
||
| 160 | * @param Object $entity Entity |
||
| 161 | * @param Request $request Request in progress |
||
| 162 | * @param string $entityName Name of Entity |
||
| 163 | * @param string $typePath Path of FormType |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public function abstractUpdateAction($entity, Request $request, $entityName, $typePath) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Deletes an item entity. |
||
| 194 | * |
||
| 195 | * @param Object $entity Entity |
||
| 196 | * @param Request $request Request in progress |
||
| 197 | * @param string $entityName Name of Entity |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function abstractDeleteAction($entity, Request $request, $entityName) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * SetOrder for the SortAction in views. |
||
| 212 | * |
||
| 213 | * @param string $name session name |
||
| 214 | * @param string $field field name |
||
| 215 | * @param string $type sort type ("ASC"/"DESC") |
||
| 216 | */ |
||
| 217 | protected function setOrder($name, $field, $type = 'ASC') |
||
| 221 | |||
| 222 | /** |
||
| 223 | * GetOrder for the SortAction in views. |
||
| 224 | * |
||
| 225 | * @param string $name |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | protected function getOrder($name) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * AddQueryBuilderSort for the SortAction in views. |
||
| 238 | * |
||
| 239 | * @param QueryBuilder $qb |
||
| 240 | * @param string $name |
||
| 241 | */ |
||
| 242 | protected function addQueryBuilderSort(QueryBuilder $qb, $name) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Create Delete form. |
||
| 252 | * |
||
| 253 | * @param int $id |
||
| 254 | * @param string $route |
||
| 255 | * |
||
| 256 | * @return \Symfony\Component\Form\Form |
||
| 257 | */ |
||
| 258 | protected function createDeleteForm($id, $route) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Test Inventory. |
||
| 269 | * |
||
| 270 | * @return string|null |
||
| 271 | */ |
||
| 272 | protected function testInventory() |
||
| 294 | } |
||
| 295 |
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.