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 | abstract class HttpDeathByCaptchaAbstract extends HttpHandlerResponseAbstract implements ClientInterface |
||
13 | { |
||
14 | /** |
||
15 | * Headers por padrão para requisição HTTP |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $defaultHeaders = [ |
||
19 | 'Accept' => 'application/json', |
||
20 | 'Expect' => '', |
||
21 | 'User-Agent' => DeathByCaptcha::API_VERSION |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * Create and send an HTTP request. |
||
26 | * |
||
27 | * Use an absolute path to override the base path of the client, or a |
||
28 | * relative path to append to the base path of the client. The URL can |
||
29 | * contain the query string as well. |
||
30 | * |
||
31 | * @param string $method HTTP method. |
||
32 | * @param string|UriInterface $uri URI object or string. |
||
33 | * @param array $options Request options to apply. |
||
34 | * |
||
35 | * @return ResponseInterface |
||
36 | * @throws GuzzleException |
||
37 | */ |
||
38 | 78 | protected function request($method, $uri, array $options = []): ResponseInterface |
|
51 | |||
52 | /** |
||
53 | * Efetua requisição GET no client |
||
54 | * @param string $path |
||
55 | * @param array $query |
||
56 | * @param array $headers |
||
57 | * @return ResponseInterface |
||
58 | */ |
||
59 | 63 | View Code Duplication | protected function get(string $path, array $query = [], array $headers = []): ResponseInterface |
70 | |||
71 | /** |
||
72 | * Efetua requisição POST no client |
||
73 | * @param string $path |
||
74 | * @param array $query |
||
75 | * @param array $headers |
||
76 | * @return ResponseInterface |
||
77 | */ |
||
78 | 15 | View Code Duplication | protected function post(string $path, array $query = [], array $headers = []): ResponseInterface |
88 | |||
89 | /** |
||
90 | * Retorna os headers que serão utilizados |
||
91 | * nas requisições no cliente |
||
92 | * @param array $headers |
||
93 | * @return array $headers |
||
94 | */ |
||
95 | 78 | protected function getHeadersForClientRequest(array $headers = []): array |
|
101 | |||
102 | /** |
||
103 | * Retorna array de dados que serão utilizados |
||
104 | * nas requisições no cliente |
||
105 | * @param array $query |
||
106 | * @return array $query |
||
107 | */ |
||
108 | 78 | protected function getQueryForClientRequest(array $query = []): array |
|
115 | |||
116 | /** |
||
117 | * Retorna dados de autenticação |
||
118 | * para as requisições |
||
119 | * @return array |
||
120 | */ |
||
121 | 78 | protected function getAuthData() |
|
128 | |||
129 | /** |
||
130 | * Retorna as opções da requisição para |
||
131 | * o cliente |
||
132 | * @param array $headers |
||
133 | * @param boolean $referer |
||
134 | * @return array |
||
135 | */ |
||
136 | 78 | protected function getClientOptions(array $headers, $referer = false): array |
|
148 | |||
149 | /** |
||
150 | * Efetua requisição no cliente |
||
151 | * retornando informações da conta no serviço |
||
152 | * @return ResponseInterface |
||
153 | */ |
||
154 | 39 | public function accountRequest(): ResponseInterface |
|
160 | |||
161 | /** |
||
162 | * Efetua requisição no cliente |
||
163 | * retornando informações do status do serviço |
||
164 | * @return ResponseInterface |
||
165 | */ |
||
166 | 18 | public function statusRequest(): ResponseInterface |
|
172 | |||
173 | /** |
||
174 | * Informa pro serviço que o captcha recuperado |
||
175 | * é inválido |
||
176 | * @param int $id |
||
177 | * @return ResponseInterface |
||
178 | */ |
||
179 | 3 | public function captchAsIncorrect(int $id): ResponseInterface |
|
185 | |||
186 | /** |
||
187 | * Recupera o resultado de um captcha através |
||
188 | * do ID recentemente retornado pelo método |
||
189 | * upload. |
||
190 | * @param int $id |
||
191 | * @return ResponseInterface |
||
192 | */ |
||
193 | 6 | public function retrieveCaptcha(int $id): ResponseInterface |
|
199 | |||
200 | /** |
||
201 | * Envia o captcha para o serviço |
||
202 | * @param string $imageBase64 |
||
203 | * @return ResponseInterface |
||
204 | */ |
||
205 | 12 | public function uploadCaptcha($imageBase64): ResponseInterface |
|
213 | |||
214 | /** |
||
215 | * Envia o captcha para o serviço |
||
216 | * @param string $googlekey |
||
217 | * @param $url |
||
218 | * @return ResponseInterface |
||
219 | */ |
||
220 | public function sendReCaptchaV2($googlekey, $url) |
||
233 | } |
||
234 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: