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 | 9 | public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger) |
|
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * Locates identity based on ID |
||
| 35 | * |
||
| 36 | * @param int $identityId |
||
| 37 | * |
||
| 38 | * @return Palladium\Entity\Identity |
||
| 39 | */ |
||
| 40 | 2 | View Code Duplication | public function findIdentityById($identityId) |
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Locates identity based on email address |
||
| 64 | * |
||
| 65 | * @param string $emailAddress |
||
| 66 | * |
||
| 67 | * @return Palladium\Entity\EmailIdentity |
||
| 68 | */ |
||
| 69 | 2 | View Code Duplication | public function findEmailIdentityByEmailAddress(string $emailAddress) |
| 70 | { |
||
| 71 | 2 | $identity = new Entity\EmailIdentity; |
|
| 72 | 2 | $identity->setIdentifier($emailAddress); |
|
| 73 | |||
| 74 | 2 | $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class); |
|
| 75 | 2 | $mapper->fetch($identity); |
|
| 76 | |||
| 77 | 2 | if ($identity->getId() === null) { |
|
| 78 | 1 | $this->logger->warning('identity not found', [ |
|
| 79 | 'input' => [ |
||
| 80 | 1 | 'identifier' => $emailAddress, |
|
| 81 | ], |
||
| 82 | ]); |
||
| 83 | |||
| 84 | 1 | throw new IdentityNotFound; |
|
| 85 | } |
||
| 86 | |||
| 87 | 1 | return $identity; |
|
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $token |
||
| 93 | * @param int $action |
||
| 94 | * |
||
| 95 | * @return Palladium\Entity\EmailIdentity |
||
| 96 | */ |
||
| 97 | 2 | public function findEmailIdentityByToken(string $token, $action = Entity\Identity::ACTION_ANY) |
|
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @param int $accountId |
||
| 124 | * @param string $series |
||
| 125 | * |
||
| 126 | * @return Palladium\Entity\CookieIdentity |
||
| 127 | */ |
||
| 128 | 1 | public function findCookieIdentity($accountId, $series) |
|
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * @return Palladium\Entity\IdentityCollection |
||
| 144 | */ |
||
| 145 | 1 | public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE) |
|
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * @return Palladium\Entity\IdentityCollection |
||
| 157 | */ |
||
| 158 | 1 | public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE) |
|
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * @return Palladium\Entity\IdentityCollection |
||
| 169 | */ |
||
| 170 | 2 | private function fetchIdentitiesByStatus(Entity\IdentityCollection $collection, $status) |
|
| 179 | } |
||
| 180 |
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.