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 |
||
25 | class AmplifypayAdapter implements AdapterInterface |
||
26 | { |
||
27 | |||
28 | use Pluggable, KeyVerifier; |
||
29 | |||
30 | /** |
||
31 | * @var \GuzzleHttp\Client |
||
32 | */ |
||
33 | protected $httpClient; |
||
34 | |||
35 | /** |
||
36 | * Keys to initiate a successful request |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $apiKeys; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | const BASE_URL = 'https://api.amplifypay.com/merchant'; |
||
45 | |||
46 | /** |
||
47 | * Only pass a client constructor in if it (client object) has been properly bootstrapped |
||
48 | * AmplifypayAdapter constructor. |
||
49 | * @param \GuzzleHttp\Client|null $client |
||
50 | */ |
||
51 | 25 | public function __construct(Client $client = null) |
|
62 | |||
63 | 25 | protected function registerPlugins() |
|
69 | |||
70 | 3 | public function charge(array $data) |
|
81 | |||
82 | 3 | protected function authorizeTransaction(string $relative, array $data = null) |
|
83 | { |
||
84 | /** |
||
85 | * @var ResponseInterface $response |
||
86 | */ |
||
87 | 3 | $response = $this->httpClient->post( |
|
88 | 3 | self::BASE_URL . $relative, [ |
|
89 | 3 | 'body' => json_encode($data) |
|
90 | ] |
||
91 | ); |
||
92 | |||
93 | 3 | if ($response->getStatusCode() === 200) { |
|
94 | 2 | return $response; |
|
95 | } |
||
96 | |||
97 | 1 | throw InvalidHttpResponseException::createFromResponse($response); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return \GuzzleHttp\Client |
||
102 | */ |
||
103 | 15 | public function getHttpClient(): Client |
|
107 | |||
108 | /** |
||
109 | * @codeCoverageIgnore |
||
110 | * @return \GuzzleHttp\Client |
||
111 | */ |
||
112 | View Code Duplication | protected function setHttpClient() : Client |
|
125 | } |
||
126 |
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.