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 |
||
23 | View Code Duplication | trait BountyTrait |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * The bounty amount. |
||
27 | * |
||
28 | * @var int|null |
||
29 | */ |
||
30 | protected $bodyAmount; |
||
31 | |||
32 | /** |
||
33 | * The bounty closes date. |
||
34 | * |
||
35 | * @var \DateTime|null |
||
36 | */ |
||
37 | protected $bountyClosesDate; |
||
38 | |||
39 | /** |
||
40 | * Bounty user. |
||
41 | * |
||
42 | * @var \BenatEspina\StackExchangeApiClient\Model\Interfaces\ShallowUserInterface|null |
||
43 | */ |
||
44 | protected $bountyUser; |
||
45 | |||
46 | /** |
||
47 | * Sets body amount. |
||
48 | * |
||
49 | * @param int|null $bodyAmount The body amount |
||
50 | * |
||
51 | * @return $this self Object |
||
52 | */ |
||
53 | public function setBodyAmount($bodyAmount) |
||
59 | |||
60 | /** |
||
61 | * Gets body amount. |
||
62 | * |
||
63 | * @return int|null |
||
64 | */ |
||
65 | public function getBodyAmount() |
||
69 | |||
70 | /** |
||
71 | * Sets bounty closes date. |
||
72 | * |
||
73 | * @param \DateTime|null $bountyClosesDate The bounty closes date |
||
74 | * |
||
75 | * @return $this self Object |
||
76 | */ |
||
77 | public function setBountyClosesDate(\DateTime $bountyClosesDate) |
||
83 | |||
84 | /** |
||
85 | * Gets bounty closes date. |
||
86 | * |
||
87 | * @return \DateTime|null |
||
88 | */ |
||
89 | public function getBountyClosesDate() |
||
93 | |||
94 | /** |
||
95 | * Sets bounty user. |
||
96 | * |
||
97 | * @param \BenatEspina\StackExchangeApiClient\Model\Interfaces\ShallowUserInterface|null $bountyUser The bounty user |
||
98 | * |
||
99 | * @return $this self Object |
||
100 | */ |
||
101 | public function setBountyUser(ShallowUserInterface $bountyUser) |
||
107 | |||
108 | /** |
||
109 | * Gets bounty user. |
||
110 | * |
||
111 | * @return \BenatEspina\StackExchangeApiClient\Model\Interfaces\ShallowUserInterface|null |
||
112 | */ |
||
113 | public function getBountyUser() |
||
117 | |||
118 | /** |
||
119 | * Loads the variables if the data exist into resource. It works like a constructor. |
||
120 | * |
||
121 | * @param null|mixed[] $resource The resource |
||
122 | */ |
||
123 | protected function loadBounty($resource) |
||
129 | } |
||
130 |
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.