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 |
||
| 17 | class OtoConfirmationController extends Controller { |
||
| 18 | |||
| 19 | private $mapper; |
||
| 20 | private $foo; |
||
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * @NoAdminRequired |
||
| 25 | * @param string $appName |
||
| 26 | * @param IRequest $request an instance of the request |
||
| 27 | * @param OtoConfirmationMapper $mapper |
||
| 28 | * @param OtoConfirmationService $otoConfirmationService |
||
| 29 | * @param OtoLayerService $otoLayerService |
||
| 30 | * @param IUserSession $userSession |
||
| 31 | */ |
||
| 32 | public function __construct($appName, IRequest $request, OtoConfirmationMapper $mapper,OtoConfirmationService $otoConfirmationService, OtoLayerService $otoLayerService, IUserSession $userSession){ |
||
| 40 | /** |
||
| 41 | * @PublicPage |
||
| 42 | * @NoCSRFRequired |
||
| 43 | * @param string $otoLayerId |
||
| 44 | * @param string $password |
||
| 45 | * @param string $eventId |
||
| 46 | * @param string $name |
||
| 47 | * @param string $password |
||
| 48 | */ |
||
| 49 | public function create($otoLayerId, $password, $eventId, $name){ |
||
| 74 | //returns a data response that contains all of the confirmations for a user |
||
| 75 | //along with their otoLayer's sourceId and destinationId |
||
| 76 | /** |
||
| 77 | * @NoAdminRequired |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function getConfirmationsByUser(){ |
|
| 88 | //deletes a layer and all of it's confirmations |
||
| 89 | /** |
||
| 90 | * @NoAdminRequired |
||
| 91 | */ |
||
| 92 | public function deleteConfirmationsLayers($otoLayerId){ |
||
| 102 | //deletes a layer and all of it's confirmations |
||
| 103 | /** |
||
| 104 | * @NoAdminRequired |
||
| 105 | */ |
||
| 106 | public function deleteBySourceId($sourceId){ |
||
| 117 | } |
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.