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 declare(strict_types=1); |
||
| 12 | class DragonApiController |
||
| 13 | { |
||
| 14 | /** @var DragonService $service */ |
||
| 15 | private $service; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * DragonController constructor. |
||
| 19 | * @param DragonService $service |
||
| 20 | */ |
||
| 21 | public function __construct(DragonService $service) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Controller. |
||
| 28 | * |
||
| 29 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 30 | * |
||
| 31 | * @return \Psr\Http\Message\ResponseInterface |
||
| 32 | */ |
||
| 33 | public function indexAction(ServerRequestInterface $request, array $args) : ResponseInterface |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param ServerRequestInterface $request |
||
| 43 | * @param array $args |
||
| 44 | * @return ResponseInterface |
||
| 45 | * @throws \Doctrine\ORM\ORMException |
||
| 46 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 47 | */ |
||
| 48 | public function createAction(ServerRequestInterface $request, array $args) : ResponseInterface |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param ServerRequestInterface $request |
||
| 71 | * @param array $args |
||
| 72 | * @return ResponseInterface |
||
| 73 | * @throws \Doctrine\ORM\EntityNotFoundException |
||
| 74 | */ |
||
| 75 | public function viewAction(ServerRequestInterface $request, array $args) : ResponseInterface |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param ServerRequestInterface $request |
||
| 84 | * @param array $args |
||
| 85 | * @return ResponseInterface |
||
| 86 | * @throws \Doctrine\ORM\EntityNotFoundException |
||
| 87 | * @throws \Doctrine\ORM\ORMException |
||
| 88 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 89 | */ |
||
| 90 | public function updateAction(ServerRequestInterface $request, array $args) : ResponseInterface |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param ServerRequestInterface $request |
||
| 116 | * @param array $args |
||
| 117 | * @return ResponseInterface |
||
| 118 | * @throws \Doctrine\ORM\EntityNotFoundException |
||
| 119 | * @throws \Doctrine\ORM\ORMException |
||
| 120 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 121 | */ |
||
| 122 | public function deleteAction(ServerRequestInterface $request, array $args) : ResponseInterface |
||
| 129 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.