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 declare(strict_types=1); |
||
10 | class Parameters |
||
11 | { |
||
12 | private $consumer; |
||
13 | |||
14 | private $accessToken; |
||
15 | |||
16 | private $nonce; |
||
17 | |||
18 | private $timestamp; |
||
19 | |||
20 | private $signatureMethod; |
||
21 | |||
22 | private $version; |
||
23 | |||
24 | private $parameters; |
||
25 | |||
26 | private $url; |
||
27 | |||
28 | 16 | public function __construct(Application $consumer, AccessToken $accessToken, Url $url, Parameter ...$parameters) |
|
29 | { |
||
30 | 16 | $this->consumer = $consumer->getKey(); |
|
31 | 16 | $this->accessToken = $accessToken->getToken(); |
|
32 | 16 | $this->nonce = bin2hex(random_bytes(16)); |
|
33 | 16 | $this->timestamp = (new \DateTimeImmutable())->format('U'); |
|
34 | 16 | $this->signatureMethod = 'HMAC-SHA1'; |
|
35 | 16 | $this->version = '1.0'; |
|
36 | 16 | $this->parameters = $parameters; |
|
37 | 16 | $this->url = $url; |
|
38 | } |
||
39 | |||
40 | 6 | public function getConsumerKey(): string |
|
44 | |||
45 | 8 | public function getNonce(): string |
|
49 | |||
50 | 6 | public function getSignatureMethod(): string |
|
54 | |||
55 | 6 | public function getTimestamp(): string |
|
59 | |||
60 | 6 | public function getToken(): string |
|
64 | |||
65 | 6 | public function getVersion(): string |
|
69 | |||
70 | 9 | public function getAll(): array |
|
74 | |||
75 | 9 | private function getBaseParameters(): array |
|
86 | |||
87 | 9 | View Code Duplication | private function getParameters(): array |
97 | } |
||
98 |
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.