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 | View Code Duplication | trait ExpireAccountTrait |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * @var int Unix Epoch timestamp when the account expires. |
||
23 | * |
||
24 | * @ORM\Column(name="account_expires_at", type="integer", nullable=true) |
||
25 | */ |
||
26 | protected $accountExpiresAt; |
||
27 | |||
28 | /** |
||
29 | * Makes account to expire at specified moment of time (NULL for no expiration). |
||
30 | * |
||
31 | * @param \DateTime $time |
||
32 | * |
||
33 | * @return self |
||
34 | */ |
||
35 | 1 | public function expireAccountAt(\DateTime $time = null) |
|
43 | |||
44 | /** |
||
45 | * Makes account to expire in specified period of time. |
||
46 | * |
||
47 | * @param \DateInterval $interval |
||
48 | * |
||
49 | * @return self |
||
50 | */ |
||
51 | 1 | public function expireAccountIn(\DateInterval $interval) |
|
59 | |||
60 | /** |
||
61 | * Specifies whether the "account expiration" feature is available for this user. |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | 2 | protected function canAccountBeExpired(): bool |
|
69 | } |
||
70 |
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.