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 |
||
9 | class DocumentProvider |
||
10 | { |
||
11 | use AdjustUrl; |
||
12 | |||
13 | /** @var \GuzzleHttp\Client */ |
||
14 | protected $client; |
||
15 | |||
16 | /** @var Headers */ |
||
17 | protected $headers; |
||
18 | |||
19 | /** @var string The environment this is being run in */ |
||
20 | protected $environment; |
||
21 | |||
22 | /** The URI of the action */ |
||
23 | const URI = 'https://api.signere.no/api/DocumentProvider'; |
||
24 | |||
25 | /** |
||
26 | * Instantiate the class. |
||
27 | * |
||
28 | * @param Client $client |
||
29 | * @param Headers $headers |
||
30 | * @param string $environment |
||
31 | */ |
||
32 | 6 | public function __construct(Client $client, Headers $headers, $environment = null) |
|
38 | |||
39 | /** |
||
40 | * Retrieves a document provider account. |
||
41 | * |
||
42 | * @param string $providerId |
||
43 | * @return object |
||
44 | */ |
||
45 | 1 | public function getProviderAccount(string $providerId) |
|
61 | |||
62 | /** |
||
63 | * Gets the expires date for your BankID certificate. If you don't |
||
64 | * have your own BankID certificate it will return Bad request. |
||
65 | * |
||
66 | * @return object |
||
67 | */ |
||
68 | 1 | public function getCertExpiry() |
|
84 | |||
85 | /** |
||
86 | * Get the usage when using prepaid or demo account. |
||
87 | * |
||
88 | * @param string $providerId |
||
89 | * @param bool|bool $demo |
||
90 | * @return object |
||
91 | */ |
||
92 | 2 | public function getUsage(string $providerId, bool $demo = false) |
|
113 | |||
114 | /** |
||
115 | * Creates a new document provider. |
||
116 | * |
||
117 | * @param array $body |
||
118 | * @return object |
||
119 | */ |
||
120 | 1 | public function create(array $body) |
|
164 | |||
165 | /** |
||
166 | * Updates a new document provider. |
||
167 | * |
||
168 | * @param array $body |
||
169 | * @return object |
||
170 | */ |
||
171 | 1 | public function update(array $body) |
|
203 | } |
||
204 |
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.