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 |
||
| 29 | class FileController extends RestController |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var FileManager |
||
| 33 | */ |
||
| 34 | private $fileManager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Response $response Response |
||
| 38 | * @param RestUtilsInterface $restUtils Rest utils |
||
| 39 | * @param Router $router Router |
||
| 40 | * @param ValidatorInterface $validator Validator |
||
| 41 | * @param EngineInterface $templating Templating |
||
| 42 | * @param FormFactory $formFactory form factory |
||
| 43 | * @param DocumentType $formType generic form |
||
| 44 | * @param ContainerInterface $container Container |
||
| 45 | * @param SchemaUtils $schemaUtils schema utils |
||
| 46 | * @param FileManager $fileManager Handles file specific tasks |
||
| 47 | */ |
||
| 48 | 1 | View Code Duplication | public function __construct( |
| 73 | |||
| 74 | /** |
||
| 75 | * Writes a new Entry to the database |
||
| 76 | * |
||
| 77 | * @param Request $request Current http request |
||
| 78 | * |
||
| 79 | * @return Response $response Result of action with data (if successful) |
||
| 80 | */ |
||
| 81 | 1 | public function postAction(Request $request) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * respond with document if non json mime-type is requested |
||
| 106 | * |
||
| 107 | * @param Request $request Current http request |
||
| 108 | * @param string $id id of file |
||
| 109 | * |
||
| 110 | * @return Response |
||
| 111 | */ |
||
| 112 | 1 | public function getAction(Request $request, $id) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Update a record |
||
| 141 | * |
||
| 142 | * @param Number $id ID of record |
||
| 143 | * @param Request $request Current http request |
||
| 144 | * |
||
| 145 | * @return Response $response Result of action with data (if successful) |
||
| 146 | */ |
||
| 147 | public function putAction($id, Request $request) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Deletes a record |
||
| 173 | * |
||
| 174 | * @param Number $id ID of record |
||
| 175 | * |
||
| 176 | * @return Response $response Result of the action |
||
| 177 | */ |
||
| 178 | 1 | public function deleteAction($id) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Determines the routes and replaces the http method |
||
| 189 | * |
||
| 190 | * @param string $routeName Name of the route to be generated |
||
| 191 | * @param array $files Set of uploaded files |
||
| 192 | * @param array $routeTypes Set of route types to be recognized |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 1 | private function determineRoutes($routeName, array $files, array $routeTypes) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Validates the provided request |
||
| 221 | * |
||
| 222 | * @param Request $request Http request to be validated |
||
| 223 | * @param Response $response Http response to be returned in case of an error |
||
| 224 | * @param string $fileData Alternative content to be validated |
||
| 225 | * |
||
| 226 | * @throws \Exception |
||
| 227 | * @return File|null |
||
| 228 | */ |
||
| 229 | 1 | private function validateRequest(Request $request, Response $response, $fileData = '') |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Gathers information into a request |
||
| 245 | * |
||
| 246 | * @param Request $request master request sent by client. |
||
| 247 | * |
||
| 248 | * @return Request |
||
| 249 | */ |
||
| 250 | private function normalizeRequest(Request $request) |
||
| 263 | } |
||
| 264 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.