Complex classes like Data_and_files 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 Data_and_files, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait Data_and_files { |
||
15 | /** |
||
16 | * Data array, similar to `$_POST` |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | public $data; |
||
21 | /** |
||
22 | * Normalized files array |
||
23 | * |
||
24 | * Each file item can be either single file or array of files (in contrast with native PHP arrays where each field like `name` become an array) with keys |
||
25 | * `name`, `type`, `size`, `tmp_name`, `stream` and `error` |
||
26 | * |
||
27 | * `name`, `type`, `size` and `error` keys are similar to native PHP fields in `$_FILES`; `tmp_name` might not be temporary file, but file descriptor |
||
28 | * wrapper like `request-file:///file` instead and `stream` is resource like obtained with `fopen('/tmp/xyz', 'rb')` |
||
29 | * |
||
30 | * @var array[] |
||
31 | */ |
||
32 | public $files; |
||
33 | /** |
||
34 | * Data stream resource, similar to `fopen('php://input', 'rb')` |
||
35 | * |
||
36 | * Make sure you're controlling position in stream where you read something, if code in some other place might seek on this stream |
||
37 | * |
||
38 | * Stream is read-only |
||
39 | * |
||
40 | * @var null|resource |
||
41 | */ |
||
42 | public $data_stream; |
||
43 | /** |
||
44 | * `$this->init_server()` assumed to be called already |
||
45 | * |
||
46 | * @param array $data Typically `$_POST` |
||
47 | * @param array[] $files Typically `$_FILES`; might be like native PHP array `$_FILES` or normalized; each file item MUST contain keys |
||
48 | * `name`, `type`, `size`, `error` and at least one of `tmp_name` or `stream` |
||
49 | * @param null|resource|string $data_stream String, like `php://input` or resource, like `fopen('php://input', 'rb')` with request body, will be parsed for |
||
50 | * data and files if necessary |
||
51 | * @param bool $copy_stream Sometimes data stream can only being read once (like most of times with `php://input`), so it is necessary to |
||
52 | * copy it and store its contents for longer period of time |
||
53 | * |
||
54 | * @throws ExitException |
||
55 | */ |
||
56 | 34 | function init_data_and_files ($data = [], $files = [], $data_stream = null, $copy_stream = true) { |
|
86 | /** |
||
87 | * Get data item by name |
||
88 | * |
||
89 | * @param string[]|string[][] $name |
||
90 | * |
||
91 | * @return mixed|mixed[]|null Data items (or associative array of data items) if exists or `null` otherwise (in case if `$name` is an array even one |
||
92 | * missing key will cause the whole thing to fail) |
||
93 | */ |
||
94 | 32 | function data (...$name) { |
|
114 | /** |
||
115 | * Get file item by name |
||
116 | * |
||
117 | * @param string $name |
||
118 | * |
||
119 | * @return array|null File item if exists or `null` otherwise |
||
120 | */ |
||
121 | 2 | function files ($name) { |
|
124 | /** |
||
125 | * @param array[] $files |
||
126 | * @param string $file_path |
||
127 | * |
||
128 | * @return array[] |
||
129 | */ |
||
130 | 34 | protected function normalize_files ($files, $file_path = '') { |
|
157 | /** |
||
158 | * @param array $file |
||
159 | * @param string $file_path |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | 6 | protected function normalize_file ($file, $file_path) { |
|
179 | /** |
||
180 | * Parsing request body for following Content-Type: `application/json`, `application/x-www-form-urlencoded` and `multipart/form-data` |
||
181 | * |
||
182 | * @throws ExitException |
||
183 | */ |
||
184 | 8 | protected function parse_data_stream () { |
|
185 | 8 | $content_type = $this->header('content-type'); |
|
186 | 8 | rewind($this->data_stream); |
|
187 | /** |
||
188 | * application/json |
||
189 | */ |
||
190 | 8 | if (preg_match('#^application/([^+\s]+\+)?json#', $content_type)) { |
|
191 | 2 | $this->data = _json_decode(stream_get_contents($this->data_stream)) ?: []; |
|
192 | 2 | return; |
|
193 | } |
||
194 | /** |
||
195 | * application/x-www-form-urlencoded |
||
196 | */ |
||
197 | 6 | if (strpos($content_type, 'application/x-www-form-urlencoded') === 0) { |
|
198 | 2 | @parse_str(stream_get_contents($this->data_stream), $this->data); |
|
199 | 2 | return; |
|
200 | } |
||
201 | /** |
||
202 | * multipart/form-data |
||
203 | */ |
||
204 | 4 | if (preg_match('#multipart/form-data;.*boundary="?([^;"]{1,70})(?:"|;|$)#Ui', $content_type, $matches)) { |
|
205 | try { |
||
206 | 4 | $parts = $this->parse_multipart_into_parts($this->data_stream, trim($matches[1])) ?: []; |
|
207 | 4 | list($this->data, $files) = $this->parse_multipart_analyze_parts($this->data_stream, $parts); |
|
208 | 4 | $this->files = $this->normalize_files($files); |
|
209 | 2 | } catch (UnexpectedValueException $e) { |
|
210 | // Do nothing, if parsing failed then we'll just leave `::$data` and `::$files` empty |
||
211 | } |
||
212 | } |
||
213 | 4 | } |
|
214 | /** |
||
215 | * Parse content stream |
||
216 | * |
||
217 | * @param resource $stream |
||
218 | * @param string $boundary |
||
219 | * |
||
220 | * @return array[]|false |
||
221 | * |
||
222 | * @throws UnexpectedValueException |
||
223 | * @throws ExitException |
||
224 | */ |
||
225 | 4 | protected function parse_multipart_into_parts ($stream, $boundary) { |
|
226 | 4 | $parts = []; |
|
227 | 4 | $crlf = "\r\n"; |
|
228 | 4 | $position = 0; |
|
229 | 4 | $body = ''; |
|
230 | 4 | list($offset, $body) = $this->parse_multipart_find($stream, $body, "--$boundary$crlf"); |
|
231 | /** |
||
232 | * strlen doesn't take into account trailing CRLF since we'll need it in loop below |
||
233 | */ |
||
234 | 4 | $position += $offset + strlen("--$boundary"); |
|
235 | 4 | $body = substr($body, strlen("--$boundary")); |
|
236 | /** |
||
237 | * Each part always starts with CRLF |
||
238 | */ |
||
239 | 4 | while (strpos($body, $crlf) === 0) { |
|
240 | 4 | $position += 2; |
|
241 | 4 | $body = substr($body, 2); |
|
242 | $part = [ |
||
243 | 'headers' => [ |
||
244 | 4 | 'offset' => $position, |
|
245 | 4 | 'size' => 0 |
|
246 | 4 | ], |
|
247 | 'body' => [ |
||
248 | 'offset' => 0, |
||
249 | 'size' => 0 |
||
250 | ] |
||
251 | ]; |
||
252 | 4 | if (strpos($body, $crlf) === 0) { |
|
253 | /** |
||
254 | * No headers |
||
255 | */ |
||
256 | 2 | $position += 2; |
|
257 | 2 | $body = substr($body, 2); |
|
258 | } else { |
||
259 | /** |
||
260 | * Find headers end in order to determine size |
||
261 | */ |
||
262 | 4 | list($offset, $body) = $this->parse_multipart_find($stream, $body, $crlf.$crlf); |
|
263 | 4 | $part['headers']['size'] = $offset; |
|
264 | 4 | $position += $offset + 4; |
|
265 | 4 | $body = substr($body, 4); |
|
266 | } |
||
267 | 4 | $part['body']['offset'] = $position; |
|
268 | /** |
||
269 | * Find body end in order to determine its size |
||
270 | */ |
||
271 | 4 | list($offset, $body) = $this->parse_multipart_find($stream, $body, "$crlf--$boundary"); |
|
272 | 4 | $part['body']['size'] = $offset; |
|
273 | 4 | $position += $offset + strlen("$crlf--$boundary"); |
|
274 | 4 | $body = substr($body, strlen("$crlf--$boundary")); |
|
275 | 4 | if ($part['headers']['size']) { |
|
276 | 4 | $parts[] = $part; |
|
277 | } |
||
278 | } |
||
279 | /** |
||
280 | * Last boundary after all parts ends with '--' and we don't care what rubbish happens after it |
||
281 | */ |
||
282 | 4 | $post_max_size = $this->post_max_size(); |
|
283 | 4 | if (strpos($body, '--') !== 0) { |
|
284 | 2 | return false; |
|
285 | } |
||
286 | /** |
||
287 | * Check whether body size is bigger than allowed limit |
||
288 | */ |
||
289 | 4 | if ($position + strlen($body) > $post_max_size) { |
|
290 | 2 | throw new ExitException(413); |
|
291 | } |
||
292 | 4 | return $parts; |
|
293 | } |
||
294 | /** |
||
295 | * @param resource $stream |
||
296 | * @param array[] $parts |
||
297 | * |
||
298 | * @return array[] |
||
299 | */ |
||
300 | 4 | protected function parse_multipart_analyze_parts ($stream, $parts) { |
|
301 | 4 | $data = []; |
|
302 | 4 | $files = []; |
|
303 | 4 | foreach ($parts as $part) { |
|
304 | 4 | $headers = $this->parse_multipart_headers( |
|
305 | 4 | stream_get_contents($stream, $part['headers']['size'], $part['headers']['offset']) |
|
306 | ); |
||
307 | if ( |
||
308 | 4 | !isset($headers['content-disposition'][0], $headers['content-disposition']['name']) || |
|
309 | 4 | $headers['content-disposition'][0] != 'form-data' |
|
310 | ) { |
||
311 | 2 | continue; |
|
312 | } |
||
313 | 4 | $name = $headers['content-disposition']['name']; |
|
314 | 4 | if (isset($headers['content-disposition']['filename'])) { |
|
315 | $file = [ |
||
316 | 4 | 'name' => $headers['content-disposition']['filename'], |
|
317 | 4 | 'type' => @$headers['content-type'] ?: 'application/octet-stream', |
|
318 | 4 | 'size' => $part['body']['size'], |
|
319 | 4 | 'stream' => Stream_slicer::slice($stream, $part['body']['offset'], $part['body']['size']), |
|
320 | 4 | 'error' => UPLOAD_ERR_OK |
|
321 | ]; |
||
322 | 4 | if ($file['name'] === '') { |
|
323 | 2 | $file['type'] = ''; |
|
324 | 2 | $file['stream'] = null; |
|
325 | 2 | $file['error'] = UPLOAD_ERR_NO_FILE; |
|
326 | 4 | } elseif ($file['size'] > $this->upload_max_file_size()) { |
|
327 | 2 | $file['stream'] = null; |
|
328 | 2 | $file['error'] = UPLOAD_ERR_INI_SIZE; |
|
329 | } |
||
330 | 4 | $this->parse_multipart_set_target($files, $name, $file); |
|
331 | } else { |
||
332 | 4 | if ($part['body']['size'] == 0) { |
|
333 | 2 | $this->parse_multipart_set_target($data, $name, ''); |
|
334 | } else { |
||
335 | 4 | $this->parse_multipart_set_target( |
|
336 | $data, |
||
337 | $name, |
||
338 | 4 | stream_get_contents($stream, $part['body']['size'], $part['body']['offset']) |
|
339 | ); |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | 4 | return [$data, $files]; |
|
344 | } |
||
345 | /** |
||
346 | * @return int |
||
347 | */ |
||
348 | 4 | protected function post_max_size () { |
|
352 | /** |
||
353 | * @return int |
||
354 | */ |
||
355 | 4 | protected function upload_max_file_size () { |
|
359 | /** |
||
360 | * @param int|string $size |
||
361 | * |
||
362 | * @return int |
||
363 | */ |
||
364 | 4 | protected function convert_size_to_bytes ($size) { |
|
375 | /** |
||
376 | * @param resource $stream |
||
377 | * @param string $next_data |
||
378 | * @param string $target |
||
379 | * |
||
380 | * @return array |
||
381 | * |
||
382 | * @throws UnexpectedValueException |
||
383 | */ |
||
384 | 4 | protected function parse_multipart_find ($stream, $next_data, $target) { |
|
404 | /** |
||
405 | * @param string $content |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | 4 | protected function parse_multipart_headers ($content) { |
|
431 | /** |
||
432 | * @param array $source |
||
433 | * @param string $name |
||
434 | * @param array|string $value |
||
435 | */ |
||
436 | 4 | protected function parse_multipart_set_target (&$source, $name, $value) { |
|
453 | } |
||
454 |