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 Request |
||
10 | { |
||
11 | const BASE_URL = 'https://api.hh.ru/'; |
||
12 | |||
13 | /** |
||
14 | * @var \GuzzleHttp\Client |
||
15 | */ |
||
16 | protected $client; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $headers = []; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $locale = 'RU'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $host = 'hh.ru'; |
||
32 | |||
33 | /** |
||
34 | * @param string $token |
||
35 | */ |
||
36 | public function __construct($token = null) |
||
45 | |||
46 | /** |
||
47 | * @param string $uri |
||
48 | * @param array $params |
||
49 | * @return array|null |
||
50 | */ |
||
51 | View Code Duplication | public function get($uri, $params = []) |
|
52 | { |
||
53 | if (!empty($params)) { |
||
54 | $uri .= '?' . $this->makeQueryString($params); |
||
55 | } |
||
56 | |||
57 | return $this->executeRequest('GET', $uri); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $uri |
||
62 | * @param array $params |
||
63 | * @return array|null |
||
64 | */ |
||
65 | public function post($uri, $params = []) |
||
71 | |||
72 | /** |
||
73 | * @param string $uri |
||
74 | * @param array $params |
||
75 | * @return array|null |
||
76 | */ |
||
77 | public function postJson($uri, $params = []) |
||
83 | |||
84 | /** |
||
85 | * @param string $uri |
||
86 | * @param array $params |
||
87 | * @return array|null |
||
88 | */ |
||
89 | public function postFile($uri, $params = []) |
||
95 | |||
96 | /** |
||
97 | * @param string $uri |
||
98 | * @param array $params |
||
99 | * @return array|null |
||
100 | */ |
||
101 | public function put($uri, $params = []) |
||
107 | |||
108 | /** |
||
109 | * @param string $uri |
||
110 | * @param array $params |
||
111 | * @return array|null |
||
112 | */ |
||
113 | public function putJson($uri, $params = []) |
||
119 | |||
120 | /** |
||
121 | * @param string $uri |
||
122 | * @param array $params |
||
123 | * @return array|null |
||
124 | */ |
||
125 | View Code Duplication | public function delete($uri, $params = []) |
|
133 | |||
134 | /** |
||
135 | * @param ResponseInterface $response |
||
136 | * @return array|null |
||
137 | */ |
||
138 | protected function parseResponse(ResponseInterface $response) |
||
142 | |||
143 | /** |
||
144 | * @param string $method |
||
145 | * @param string $uri |
||
146 | * @param array $options |
||
147 | * @return array|null |
||
148 | */ |
||
149 | protected function executeRequest($method, $uri, array $options = []) |
||
157 | |||
158 | /** |
||
159 | * @param string $locale |
||
160 | * @return Request |
||
161 | */ |
||
162 | public function setLocale($locale) |
||
168 | |||
169 | /** |
||
170 | * @param $params |
||
171 | * @return string |
||
172 | */ |
||
173 | protected function makeQueryString($params = []) |
||
186 | |||
187 | /** |
||
188 | * @param string $host |
||
189 | * @return Request |
||
190 | */ |
||
191 | public function setHost($host) |
||
197 | |||
198 | /** |
||
199 | * @param string $token |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setToken($token) |
||
208 | |||
209 | /** |
||
210 | * @param string $token |
||
211 | */ |
||
212 | protected function addAuthHeader($token) |
||
216 | } |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: