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:
Complex classes like AccessToken often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AccessToken, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class AccessToken |
||
32 | { |
||
33 | /** @var string */ |
||
34 | private $providerId; |
||
35 | |||
36 | /** @var \DateTime */ |
||
37 | private $issuedAt; |
||
38 | |||
39 | /** @var string */ |
||
40 | private $accessToken; |
||
41 | |||
42 | /** @var string */ |
||
43 | private $tokenType; |
||
44 | |||
45 | /** @var int|null */ |
||
46 | private $expiresIn = null; |
||
47 | |||
48 | /** @var string|null */ |
||
49 | private $refreshToken = null; |
||
50 | |||
51 | /** @var string|null */ |
||
52 | private $scope = null; |
||
53 | |||
54 | /** |
||
55 | * @param array $tokenData |
||
56 | */ |
||
57 | public function __construct(array $tokenData) |
||
83 | |||
84 | /** |
||
85 | * @param Provider $provider |
||
86 | * @param \DateTime $dateTime |
||
87 | * @param array $tokenData |
||
88 | * @param string $scope |
||
89 | */ |
||
90 | public static function fromCodeResponse(Provider $provider, DateTime $dateTime, array $tokenData, $scope) |
||
106 | |||
107 | /** |
||
108 | * @param Provider $provider |
||
109 | * @param \DateTime $dateTime |
||
110 | * @param array $tokenData |
||
111 | * @param AccessToken $accessToken to steal the old scope and refresh_token from! |
||
112 | */ |
||
113 | public static function fromRefreshResponse(Provider $provider, DateTime $dateTime, array $tokenData, AccessToken $accessToken) |
||
134 | |||
135 | public function getProviderId() |
||
139 | |||
140 | /** |
||
141 | * @return \DateTime |
||
142 | */ |
||
143 | public function getIssuedAt() |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | * |
||
151 | * @see https://tools.ietf.org/html/rfc6749#section-5.1 |
||
152 | */ |
||
153 | public function getToken() |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | * |
||
161 | * @see https://tools.ietf.org/html/rfc6749#section-7.1 |
||
162 | */ |
||
163 | public function getTokenType() |
||
167 | |||
168 | /** |
||
169 | * @return int|null |
||
170 | * |
||
171 | * @see https://tools.ietf.org/html/rfc6749#section-5.1 |
||
172 | */ |
||
173 | public function getExpiresIn() |
||
177 | |||
178 | /** |
||
179 | * @return string|null the refresh token |
||
180 | * |
||
181 | * @see https://tools.ietf.org/html/rfc6749#section-1.5 |
||
182 | */ |
||
183 | public function getRefreshToken() |
||
187 | |||
188 | /** |
||
189 | * @return string|null |
||
190 | * |
||
191 | * @see https://tools.ietf.org/html/rfc6749#section-3.3 |
||
192 | */ |
||
193 | public function getScope() |
||
197 | |||
198 | /** |
||
199 | * @param \DateTime $dateTime |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function isExpired(DateTime $dateTime) |
||
216 | |||
217 | /** |
||
218 | * @param string $jsonString |
||
219 | */ |
||
220 | public static function fromJson($jsonString) |
||
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | public function toJson() |
||
248 | |||
249 | /** |
||
250 | * @param string $providerId |
||
251 | */ |
||
252 | private function setProviderId($providerId) |
||
256 | |||
257 | /** |
||
258 | * @param string $issuedAt |
||
259 | */ |
||
260 | private function setIssuedAt($issuedAt) |
||
268 | |||
269 | /** |
||
270 | * @param string $accessToken |
||
271 | */ |
||
272 | private function setAccessToken($accessToken) |
||
282 | |||
283 | /** |
||
284 | * @param string $tokenType |
||
285 | */ |
||
286 | private function setTokenType($tokenType) |
||
294 | |||
295 | /** |
||
296 | * @param int|null $expiresIn |
||
297 | */ |
||
298 | private function setExpiresIn($expiresIn) |
||
308 | |||
309 | /** |
||
310 | * @param string|null $refreshToken |
||
311 | */ |
||
312 | View Code Duplication | private function setRefreshToken($refreshToken) |
|
324 | |||
325 | /** |
||
326 | * @param string|null $scope |
||
327 | */ |
||
328 | View Code Duplication | private function setScope($scope) |
|
343 | |||
344 | /** |
||
345 | * @param string $k |
||
346 | * @param string $v |
||
347 | */ |
||
348 | private static function requireString($k, $v) |
||
354 | |||
355 | /** |
||
356 | * @param string $k |
||
357 | * @param int $v |
||
358 | */ |
||
359 | private static function requireInt($k, $v) |
||
365 | } |
||
366 |
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.