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 | * @param Palladium\Contract\CanCreateMapper $mapperFactory Factory for creating persistence layer structures |
||
| 27 | * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger |
||
| 28 | */ |
||
| 29 | 12 | public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger) |
|
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Locates identity based on ID |
||
| 38 | * |
||
| 39 | * @param int $identityId |
||
| 40 | * |
||
| 41 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
| 42 | * |
||
| 43 | * @return Palladium\Entity\Identity |
||
| 44 | */ |
||
| 45 | 2 | View Code Duplication | public function findIdentityById(int $identityId) |
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Locates identity based on email address |
||
| 69 | * |
||
| 70 | * @param string $emailAddress |
||
| 71 | * |
||
| 72 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
| 73 | * |
||
| 74 | * @return Palladium\Entity\EmailIdentity |
||
| 75 | */ |
||
| 76 | 2 | View Code Duplication | public function findEmailIdentityByEmailAddress(string $emailAddress) |
| 96 | |||
| 97 | |||
| 98 | 2 | public function findNonceIdentityByIdentifier(string $identifier) |
|
| 99 | { |
||
| 100 | 2 | $identity = new Entity\NonceIdentity; |
|
| 101 | 2 | $identity->setIdentifier($identifier); |
|
| 102 | |||
| 103 | 2 | $mapper = $this->mapperFactory->create(Mapper\NonceIdentity::class); |
|
| 104 | 2 | $mapper->fetch($identity); |
|
| 105 | |||
| 106 | 2 | if ($identity->getId() === null) { |
|
| 107 | 1 | $this->logger->notice('identity not found', [ |
|
| 108 | 'input' => [ |
||
| 109 | 1 | 'identifier' => $identifier, |
|
| 110 | ], |
||
| 111 | ]); |
||
| 112 | |||
| 113 | 1 | throw new IdentityNotFound; |
|
| 114 | } |
||
| 115 | |||
| 116 | 1 | return $identity; |
|
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $token |
||
| 122 | * @param int $action |
||
| 123 | * |
||
| 124 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
| 125 | * |
||
| 126 | * @return Palladium\Entity\EmailIdentity |
||
| 127 | */ |
||
| 128 | 2 | View Code Duplication | public function findEmailIdentityByToken(string $token, $action = Entity\Identity::ACTION_NONE) |
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * @param int $identityId |
||
| 155 | * |
||
| 156 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
| 157 | * |
||
| 158 | * @return Palladium\Entity\EmailIdentity |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function findEmailIdentityById(int $identityId) |
|
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @param int $accountId |
||
| 184 | * @param string $series |
||
| 185 | * |
||
| 186 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
| 187 | * |
||
| 188 | * @return Palladium\Entity\CookieIdentity |
||
| 189 | */ |
||
| 190 | 2 | View Code Duplication | public function findCookieIdentity($accountId, $series) |
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * @return Palladium\Entity\IdentityCollection |
||
| 217 | */ |
||
| 218 | 1 | public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE) |
|
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * @return Palladium\Entity\IdentityCollection |
||
| 230 | */ |
||
| 231 | 1 | public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE) |
|
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @return Palladium\Entity\IdentityCollection |
||
| 242 | */ |
||
| 243 | 2 | private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status) |
|
| 252 | } |
||
| 253 |
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.