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 |
||
18 | class Search |
||
19 | { |
||
20 | |||
21 | private $repository; |
||
22 | private $logger; |
||
23 | |||
24 | /** |
||
25 | * @param Palladium\Repository\Identity $repository Repository for abstracting persistence layer structures |
||
26 | * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger |
||
27 | */ |
||
28 | 12 | public function __construct(Repository $repository, LoggerInterface $logger) |
|
33 | |||
34 | |||
35 | /** |
||
36 | * Locates identity based on ID |
||
37 | * |
||
38 | * @param int $identityId |
||
39 | * |
||
40 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
41 | * |
||
42 | * @return Palladium\Entity\Identity |
||
43 | */ |
||
44 | 2 | View Code Duplication | public function findIdentityById(int $identityId) |
63 | |||
64 | |||
65 | /** |
||
66 | * Locates identity based on email address |
||
67 | * |
||
68 | * @param string $identifier |
||
69 | * |
||
70 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
71 | * |
||
72 | * @return Palladium\Entity\StandardIdentity |
||
73 | */ |
||
74 | 2 | View Code Duplication | public function findStandardIdentityByIdentifier(string $identifier) |
75 | { |
||
76 | 2 | $identity = new Entity\StandardIdentity; |
|
77 | 2 | $identity->setIdentifier($identifier); |
|
78 | |||
79 | 2 | $this->repository->load($identity); |
|
80 | |||
81 | 2 | if ($identity->getId() === null) { |
|
82 | 1 | $this->logger->notice('identity not found', [ |
|
83 | 'input' => [ |
||
84 | 1 | 'identifier' => $identifier, |
|
85 | ], |
||
86 | ]); |
||
87 | |||
88 | 1 | throw new IdentityNotFound; |
|
89 | } |
||
90 | |||
91 | 1 | return $identity; |
|
92 | } |
||
93 | |||
94 | |||
95 | 2 | View Code Duplication | public function findNonceIdentityByIdentifier(string $identifier) |
114 | |||
115 | |||
116 | /** |
||
117 | * @param string $token |
||
118 | * @param int $action |
||
119 | * |
||
120 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
121 | * |
||
122 | * @return Palladium\Entity\StandardIdentity |
||
123 | */ |
||
124 | 2 | public function findStandardIdentityByToken(string $token, $action = Entity\Identity::ACTION_NONE): Entity\StandardIdentity |
|
146 | |||
147 | |||
148 | /** |
||
149 | * @param string $token |
||
150 | * @param int $action |
||
151 | * |
||
152 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
153 | * |
||
154 | * @return Palladium\Entity\Identity |
||
155 | */ |
||
156 | 2 | public function findIdentityByToken(string $token, $action = Entity\Identity::ACTION_NONE): Entity\Identity |
|
168 | |||
169 | /** |
||
170 | * @param int $identityId |
||
171 | * |
||
172 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
173 | * |
||
174 | * @return Palladium\Entity\StandardIdentity |
||
175 | */ |
||
176 | View Code Duplication | public function findStandardIdentityById(int $identityId) |
|
195 | |||
196 | |||
197 | /** |
||
198 | * @param int $accountId |
||
199 | * @param string $series |
||
200 | * |
||
201 | * @throws Palladium\Exception\IdentityNotFound if identity was not found |
||
202 | * |
||
203 | * @return Palladium\Entity\CookieIdentity |
||
204 | */ |
||
205 | 2 | public function findCookieIdentity($accountId, $series) |
|
227 | |||
228 | |||
229 | /** |
||
230 | * @return Palladium\Entity\IdentityCollection |
||
231 | */ |
||
232 | 1 | public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE) |
|
240 | |||
241 | |||
242 | /** |
||
243 | * @return Palladium\Entity\IdentityCollection |
||
244 | */ |
||
245 | 1 | public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE) |
|
252 | |||
253 | |||
254 | /** |
||
255 | * @return Palladium\Entity\IdentityCollection |
||
256 | */ |
||
257 | 2 | private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status) |
|
264 | } |
||
265 |
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.