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 |
||
11 | View Code Duplication | class RefreshToken |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * @var integer |
||
15 | * @Id |
||
16 | * @Column(type="integer", length=11) |
||
17 | * @GeneratedValue |
||
18 | */ |
||
19 | private $id; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | * @Column(type="string",length=40) |
||
24 | */ |
||
25 | private $refreshToken; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | * @Column(type="integer",length=11) |
||
30 | */ |
||
31 | private $clientId; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | * @Column(type="integer",length=11, nullable=true) |
||
36 | */ |
||
37 | private $userId; |
||
38 | |||
39 | /** |
||
40 | * @var DateTime |
||
41 | * @Column(type="datetime") |
||
42 | */ |
||
43 | private $expires; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | * @Column(type="string",length=50) |
||
48 | */ |
||
49 | private $scope; |
||
50 | |||
51 | /** |
||
52 | * @var Client |
||
53 | * @ManyToOne(targetEntity="OAuth\Client") |
||
54 | * @JoinColumn(name="client", referencedColumnName="id") |
||
55 | */ |
||
56 | private $client; |
||
57 | |||
58 | /** |
||
59 | * @var User |
||
60 | * @ManyToOne(targetEntity="OAuth\User") |
||
61 | * @JoinColumn(name="user", referencedColumnName="id") |
||
62 | */ |
||
63 | private $user; |
||
64 | |||
65 | /** |
||
66 | * Get id |
||
67 | * |
||
68 | * @return integer |
||
69 | */ |
||
70 | public function getId() |
||
74 | |||
75 | /** |
||
76 | * Set refresh_token |
||
77 | * |
||
78 | * @param string $refresh_token |
||
79 | * @return RefreshToken |
||
80 | */ |
||
81 | public function setRefreshToken($refresh_token) |
||
86 | |||
87 | /** |
||
88 | * Get refresh_token |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getRefreshToken() |
||
96 | |||
97 | /** |
||
98 | * Set client_id |
||
99 | * |
||
100 | * @param int $clientId |
||
101 | * @return RefreshToken |
||
102 | */ |
||
103 | public function setClientId($clientId) |
||
108 | |||
109 | /** |
||
110 | * Get client_id |
||
111 | * |
||
112 | * @return int |
||
113 | */ |
||
114 | public function getClientId() |
||
118 | |||
119 | /** |
||
120 | * Set user_id |
||
121 | * |
||
122 | * @param int $userId |
||
123 | * @return RefreshToken |
||
124 | */ |
||
125 | public function setUserId($userId) |
||
130 | |||
131 | /** |
||
132 | * Get user_identifier |
||
133 | * |
||
134 | * @return int |
||
135 | */ |
||
136 | public function getUserId() |
||
140 | |||
141 | /** |
||
142 | * Set expires |
||
143 | * |
||
144 | * @param DateTime $expires |
||
145 | * @return RefreshToken |
||
146 | */ |
||
147 | public function setExpires(DateTime $expires) |
||
152 | |||
153 | /** |
||
154 | * Get expires |
||
155 | * |
||
156 | * @return DateTime |
||
157 | */ |
||
158 | public function getExpires() |
||
162 | |||
163 | /** |
||
164 | * Set scope |
||
165 | * |
||
166 | * @param string $scope |
||
167 | * @return RefreshToken |
||
168 | */ |
||
169 | public function setScope($scope) |
||
174 | |||
175 | /** |
||
176 | * Get scope |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getScope() |
||
184 | |||
185 | /** |
||
186 | * Set client |
||
187 | * |
||
188 | * @param Client $client |
||
189 | * @return RefreshToken |
||
190 | */ |
||
191 | public function setClient(Client $client = null) |
||
196 | |||
197 | /** |
||
198 | * Get client |
||
199 | * |
||
200 | * @return Client |
||
201 | */ |
||
202 | public function getClient() |
||
206 | |||
207 | /** |
||
208 | * Set user |
||
209 | * |
||
210 | * @param User $user |
||
211 | * @return RefreshToken |
||
212 | */ |
||
213 | public function setUser(User $user = null) |
||
218 | |||
219 | /** |
||
220 | * Get user |
||
221 | * |
||
222 | * @return User |
||
223 | */ |
||
224 | public function getUser() |
||
228 | |||
229 | /** |
||
230 | * @return array |
||
231 | */ |
||
232 | public function toArray() |
||
242 | |||
243 | /** |
||
244 | * @param $params |
||
245 | * @return RefreshToken |
||
246 | */ |
||
247 | public static function fromArray($params) |
||
255 | } |
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.