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 |
||
23 | abstract class HttpApi |
||
24 | { |
||
25 | /** |
||
26 | * The HTTP client. |
||
27 | * |
||
28 | * @var HttpClient |
||
29 | */ |
||
30 | private $httpClient; |
||
31 | |||
32 | /** |
||
33 | * @var ResponseDeserializer |
||
34 | */ |
||
35 | protected $serializer; |
||
36 | |||
37 | /** |
||
38 | * @var RequestBuilder |
||
39 | */ |
||
40 | protected $requestBuilder; |
||
41 | |||
42 | /** |
||
43 | * @param HttpClient $httpClient |
||
44 | * @param RequestBuilder $requestBuilder |
||
45 | * @param ResponseDeserializer $deserializer |
||
46 | */ |
||
47 | 4 | public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, ResponseDeserializer $deserializer) |
|
53 | |||
54 | /** |
||
55 | * Attempts to safely deserialize the response into the given class. |
||
56 | * If the HTTP return code != 200, deserializes into SimpleResponse::class |
||
57 | * to contain the error message and any other information provided. |
||
58 | * |
||
59 | * @param ResponseInterface $response |
||
60 | * @param string $className |
||
61 | * |
||
62 | * @return $class|SimpleResponse |
||
63 | */ |
||
64 | 2 | protected function safeDeserialize(ResponseInterface $response, $className) |
|
65 | { |
||
66 | 2 | if ($response->getStatusCode() !== 200) { |
|
67 | return $this->deserializer->deserialize($response, ErrorResponse::class); |
||
68 | } else { |
||
69 | 2 | return $this->deserializer->deserialize($response, $className); |
|
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Send a GET request with query parameters. |
||
75 | * |
||
76 | * @param string $path Request path. |
||
77 | * @param array $parameters GET parameters. |
||
78 | * @param array $requestHeaders Request Headers. |
||
79 | * |
||
80 | * @return ResponseInterface |
||
81 | */ |
||
82 | protected function httpGet($path, array $parameters = [], array $requestHeaders = []) |
||
98 | |||
99 | /** |
||
100 | * Send a POST request with JSON-encoded parameters. |
||
101 | * |
||
102 | * @param string $path Request path. |
||
103 | * @param array $parameters POST parameters to be JSON encoded. |
||
104 | * @param array $requestHeaders Request headers. |
||
105 | * |
||
106 | * @return ResponseInterface |
||
107 | */ |
||
108 | protected function httpPost($path, array $parameters = [], array $requestHeaders = []) |
||
112 | |||
113 | /** |
||
114 | * Send a POST request with raw data. |
||
115 | * |
||
116 | * @param string $path Request path. |
||
117 | * @param array|string $body Request body. |
||
118 | * @param array $requestHeaders Request headers. |
||
119 | * |
||
120 | * @return ResponseInterface |
||
121 | */ |
||
122 | View Code Duplication | protected function httpPostRaw($path, $body, array $requestHeaders = []) |
|
134 | |||
135 | /** |
||
136 | * Send a PUT request with JSON-encoded parameters. |
||
137 | * |
||
138 | * @param string $path Request path. |
||
139 | * @param array $parameters POST parameters to be JSON encoded. |
||
140 | * @param array $requestHeaders Request headers. |
||
141 | * |
||
142 | * @return ResponseInterface |
||
143 | */ |
||
144 | View Code Duplication | protected function httpPut($path, array $parameters = [], array $requestHeaders = []) |
|
156 | |||
157 | /** |
||
158 | * Send a DELETE request with JSON-encoded parameters. |
||
159 | * |
||
160 | * @param string $path Request path. |
||
161 | * @param array $parameters POST parameters to be JSON encoded. |
||
162 | * @param array $requestHeaders Request headers. |
||
163 | * |
||
164 | * @return ResponseInterface |
||
165 | */ |
||
166 | View Code Duplication | protected function httpDelete($path, array $parameters = [], array $requestHeaders = []) |
|
178 | |||
179 | /** |
||
180 | * Create a JSON encoded version of an array of parameters. |
||
181 | * |
||
182 | * @param array $parameters Request parameters |
||
183 | * |
||
184 | * @return null|string |
||
185 | */ |
||
186 | protected function createJsonBody(array $parameters) |
||
190 | } |
||
191 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.