Complex classes like Builder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Builder |
||
17 | { |
||
18 | /** |
||
19 | * @var ServiceInterface |
||
20 | */ |
||
21 | private $service; |
||
22 | |||
23 | /** |
||
24 | * @param ServiceInterface $service |
||
25 | */ |
||
26 | public function __construct(ServiceInterface $service) |
||
30 | |||
31 | /** |
||
32 | * Transform command to request. |
||
33 | * |
||
34 | * @return \Closure |
||
35 | */ |
||
36 | public function commandToRequestTransformer() |
||
37 | { |
||
38 | return function (CommandInterface $command) { |
||
39 | if (!$this->service->hasOperation($command->getName())) { |
||
40 | throw new CommandException( |
||
41 | sprintf( |
||
42 | 'Command "%s" not found', |
||
43 | $command->getName() |
||
44 | ), |
||
45 | $command |
||
46 | ); |
||
47 | } |
||
48 | |||
49 | $operation = $this->service->getOperation($command->getName()); |
||
50 | |||
51 | $result = $this->transformData($command, $command->toArray(), $operation ?: [], 'request'); |
||
52 | |||
53 | $uri = ltrim($operation['requestUri'], '/'); |
||
54 | if ($result['uri']) { |
||
55 | // replace uri |
||
56 | $patterns = []; |
||
57 | $replacements = []; |
||
58 | foreach ($result['uri'] as $key => $value) { |
||
59 | $patterns[] = '/{'.$key.'}/'; |
||
60 | $replacements[] = $value; |
||
61 | } |
||
62 | |||
63 | $uri = preg_replace($patterns, $replacements, $uri); |
||
64 | } |
||
65 | |||
66 | $body = null; |
||
67 | if ('rest_json' === $operation['requestProtocol']) { |
||
68 | $body = GuzzleHttp\json_encode($result['body']); |
||
69 | $result['header']['Content-Type'] = 'application/json'; |
||
70 | } elseif ('form_params' === $operation['requestProtocol']) { |
||
71 | $body = http_build_query($result['body'], '', '&'); |
||
72 | $result['header']['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
73 | } |
||
74 | |||
75 | if ($result['query']) { |
||
76 | $uri .= sprintf('?%s', http_build_query($result['query'], null, '&', PHP_QUERY_RFC3986)); |
||
77 | } |
||
78 | |||
79 | return new Request( |
||
80 | $operation['httpMethod'], |
||
81 | $this->service->getEndpoint().$uri, |
||
82 | $result['header'], |
||
83 | $body |
||
84 | ); |
||
85 | }; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Transform response to result. |
||
90 | * |
||
91 | * @return \Closure |
||
92 | */ |
||
93 | public function responseToResultTransformer() |
||
94 | { |
||
95 | return function ( |
||
96 | ResponseInterface $response, |
||
97 | RequestInterface $request, |
||
98 | CommandInterface $command |
||
99 | ) { |
||
100 | $operation = $this->service->getOperation($command->getName()); |
||
101 | $this->processResponseError($operation ?: [], $request, $response); |
||
102 | |||
103 | $body = []; |
||
104 | if ('rest_json' === $operation['responseProtocol']) { |
||
105 | $body = GuzzleHttp\json_decode($response->getBody(), true); |
||
106 | } |
||
107 | |||
108 | $result = $this->transformData($command, $body, $operation, 'response'); |
||
109 | |||
110 | foreach ($response->getHeaders() as $name => $header) { |
||
111 | $result['header'][$name] = is_array($header) ? array_pop($header) : null; |
||
112 | } |
||
113 | |||
114 | return new Result($result['body'], $result['header']); |
||
115 | }; |
||
116 | } |
||
117 | |||
118 | public function badResponseExceptionParser() |
||
119 | { |
||
120 | return function (CommandInterface $command, GuzzleBadResponseException $e) { |
||
121 | $operation = $this->service->getOperation($command->getName()); |
||
122 | |||
123 | return $this->processResponseError( |
||
124 | $operation ?: [], |
||
125 | $e->getRequest(), |
||
126 | $e->getResponse(), |
||
127 | $e |
||
128 | ); |
||
129 | }; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Process response to check is error or not. |
||
134 | * |
||
135 | * @param array $operation |
||
136 | * @param RequestInterface $request |
||
137 | * @param ResponseInterface $response |
||
138 | * @param GuzzleBadResponseException|null $e |
||
139 | * |
||
140 | * @return BadResponseException|null |
||
141 | */ |
||
142 | private function processResponseError( |
||
143 | array $operation, |
||
144 | RequestInterface $request, |
||
145 | ResponseInterface $response = null, |
||
146 | GuzzleBadResponseException $e = null |
||
147 | ) { |
||
148 | $body = null; |
||
149 | if ('rest_json' === $operation['responseProtocol']) { |
||
150 | try { |
||
151 | $body = GuzzleHttp\json_decode((!is_null($response)) ? $response->getBody() : '', true); |
||
|
|||
152 | } catch (\InvalidArgumentException $ex) { |
||
153 | return new BadResponseException( |
||
154 | '', |
||
155 | $ex->getMessage(), |
||
156 | ($e ? $e->getMessage() : ''), |
||
157 | $request, |
||
158 | $response, |
||
159 | $e ? $e->getPrevious() : null |
||
160 | ); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if ($body) { |
||
165 | foreach ($operation['errors'] as $name => $error) { |
||
166 | if ('field' === $error['type']) { |
||
167 | $responseCode = $this->parseError($body, $error['codeField']); |
||
168 | |||
169 | if (!$responseCode) { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | // if no ifCode property then return exception |
||
174 | if (!$error['ifCode']) { |
||
175 | $responseMessage = $this->getErrorMessage($body, $error, $response); |
||
176 | |||
177 | return new BadResponseException( |
||
178 | $responseCode, |
||
179 | $responseMessage, |
||
180 | ($e ? $e->getMessage() : '').' code: '.$responseCode.', message: '.$responseMessage, |
||
181 | $request, |
||
182 | $response, |
||
183 | $e ? $e->getPrevious() : null |
||
184 | ); |
||
185 | } |
||
186 | } else { |
||
187 | $responseCode = $response->getStatusCode(); |
||
188 | } |
||
189 | |||
190 | if ($error['ifCode'] == $responseCode) { |
||
191 | $responseMessage = $this->getErrorMessage($body, $error, $response); |
||
192 | |||
193 | return new BadResponseException( |
||
194 | $responseCode, |
||
195 | $responseMessage, |
||
196 | ($e ? $e->getMessage() : '').' code: '.$responseCode.', message: '.$responseMessage, |
||
197 | $request, |
||
198 | $response, |
||
199 | $e ? $e->getPrevious() : null |
||
200 | ); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Parse error codeField from body. |
||
210 | * |
||
211 | * @param array $body |
||
212 | * @param string $path |
||
213 | * |
||
214 | * @return string|null |
||
215 | */ |
||
216 | private function parseError(array $body, $path) |
||
229 | |||
230 | /** |
||
231 | * Get error message from posible field. |
||
232 | * |
||
233 | * @param array $body |
||
234 | * @param array $error |
||
235 | * @param ResponseInterface|null $response |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | private function getErrorMessage(array $body, array $error, ResponseInterface $response = null) |
||
251 | |||
252 | /** |
||
253 | * Transform and match data with shape. |
||
254 | * |
||
255 | * @param CommandInterface $command |
||
256 | * @param array $datas |
||
257 | * @param array $operation |
||
258 | * @param string $action |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | private function transformData(CommandInterface $command, array $datas, array $operation, $action) |
||
298 | |||
299 | /** |
||
300 | * Create request/response data and add validation rule. |
||
301 | * |
||
302 | * @param Validator $validator |
||
303 | * @param array $datas |
||
304 | * @param array $shape |
||
305 | * @param string $path |
||
306 | * @param string $action |
||
307 | * @param array &$result |
||
308 | * |
||
309 | * @return array |
||
310 | */ |
||
311 | private function createData( |
||
401 | |||
402 | /** |
||
403 | * Get formatted value. |
||
404 | * |
||
405 | * @param mixed $value [description] |
||
406 | * @param array $parameter [description] |
||
407 | * @param string $action request/response |
||
408 | * |
||
409 | * @return mixed |
||
410 | */ |
||
411 | private function getFormatedValue($value, array $parameter, $action) |
||
466 | } |
||
467 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.