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 |
||
| 19 | class Search |
||
| 20 | { |
||
| 21 | |||
| 22 | private $mapperFactory; |
||
| 23 | private $logger; |
||
| 24 | |||
| 25 | |||
| 26 | 7 | public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger) |
|
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $identifier |
||
| 35 | * |
||
| 36 | * @return Palladium\Entity\EmailIdentity |
||
| 37 | */ |
||
| 38 | 2 | public function findEmailIdenityByIdentifier($identifier) |
|
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $token |
||
| 62 | * @param int $action |
||
| 63 | * |
||
| 64 | * @return Palladium\Entity\EmailIdentity |
||
| 65 | */ |
||
| 66 | 2 | public function findEmailIdenityByToken($token, $action = Entity\Identity::ACTION_ANY) |
|
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @param int $accountId |
||
| 93 | * @param string $series |
||
| 94 | * |
||
| 95 | * @return Palladium\Entity\CookieIdentity |
||
| 96 | */ |
||
| 97 | 1 | public function findCookieIdenity($accountId, $series) |
|
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @return Palladium\Entity\IdentityCollection |
||
| 113 | */ |
||
| 114 | 1 | public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE) |
|
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @return Palladium\Entity\IdentityCollection |
||
| 126 | */ |
||
| 127 | 1 | public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE) |
|
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * @return Palladium\Entity\IdentityCollection |
||
| 138 | */ |
||
| 139 | 2 | private function fetchIdentitiesByStatus(Entity\IdentityCollection $collection, $status) |
|
| 148 | } |
||
| 149 |
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.