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 |
||
12 | class AccessTokenFileStorage implements AccessTokenStorageInterface |
||
13 | { |
||
14 | /** @var string $file */ |
||
15 | protected $file; |
||
16 | |||
17 | /** @var AccessToken|null $token */ |
||
18 | protected $token; |
||
19 | |||
20 | /** |
||
21 | * @param string $file |
||
22 | */ |
||
23 | public function __construct(string $file) |
||
27 | |||
28 | /** |
||
29 | * Has storage token? |
||
30 | * |
||
31 | * @return bool |
||
32 | */ |
||
33 | public function hasToken(): bool |
||
66 | |||
67 | /** |
||
68 | * Get AccessToken from Storage or throw TokenNotFoundException if not found. |
||
69 | * |
||
70 | * @throws TokenNotFoundException |
||
71 | * |
||
72 | * @return AccessToken |
||
73 | */ |
||
74 | public function getToken(): AccessToken |
||
82 | |||
83 | /** |
||
84 | * Set AccessToken to the Storage object. |
||
85 | * |
||
86 | * @param AccessToken $token |
||
87 | * |
||
88 | * @throws \Symfony\Component\Filesystem\Exception\IOException |
||
89 | */ |
||
90 | public function setToken(AccessToken $token) |
||
96 | |||
97 | /** |
||
98 | * Abandon AccessToken. |
||
99 | * |
||
100 | * @throws \Symfony\Component\Filesystem\Exception\IOException |
||
101 | */ |
||
102 | public function abandonToken() |
||
108 | |||
109 | /** |
||
110 | * Is AccessToken expired? |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | View Code Duplication | public function isTokenExpired(): bool |
|
128 | } |
||
129 |
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.