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 |
||
13 | trait Data_and_files { |
||
14 | /** |
||
15 | * Data array, similar to `$_POST` |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | public $data; |
||
20 | /** |
||
21 | * Normalized files array |
||
22 | * |
||
23 | * 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 |
||
24 | * `name`, `type`, `size`, `tmp_name`, `stream` and `error` |
||
25 | * |
||
26 | * `name`, `type`, `size` and `error` keys are similar to native PHP fields in `$_FILES`; `tmp_name` might not be temporary file, but file descriptor |
||
27 | * wrapper like `request-file:///file` instead and `stream` is resource like obtained with `fopen('/tmp/xyz', 'rb')` |
||
28 | * |
||
29 | * @var array[] |
||
30 | */ |
||
31 | public $files; |
||
32 | /** |
||
33 | * Data stream resource, similar to `fopen('php://input', 'rb')` |
||
34 | * |
||
35 | * Make sure you're controlling position in stream where you read something, if code in some other place might seek on this stream |
||
36 | * |
||
37 | * Stream is read-only |
||
38 | * |
||
39 | * @var null|resource |
||
40 | */ |
||
41 | public $data_stream; |
||
42 | /** |
||
43 | * `$this->init_server()` assumed to be called already |
||
44 | * |
||
45 | * @param array $data Typically `$_POST` |
||
46 | * @param array[] $files Typically `$_FILES`; might be like native PHP array `$_FILES` or normalized; each file item MUST contain keys |
||
47 | * `name`, `type`, `size`, `error` and at least one of `tmp_name` or `stream` |
||
48 | * @param null|resource|string $data_stream String, like `php://input` or resource, like `fopen('php://input', 'rb')` with request body, will be parsed for |
||
49 | * data and files if necessary |
||
50 | * @param bool $copy_stream Sometimes data stream can only being read once (like most of times with `php://input`), so it is necessary to |
||
51 | * copy it and store its contents for longer period of time |
||
52 | * |
||
53 | * @throws ExitException |
||
54 | */ |
||
55 | function init_data_and_files ($data = [], $files = [], $data_stream = null, $copy_stream = true) { |
||
74 | /** |
||
75 | * Get data item by name |
||
76 | * |
||
77 | * @param string|string[] $name |
||
78 | * |
||
79 | * @return false|mixed|mixed[] Data if exists or `false` otherwise (in case if `$name` is an array even one missing key will cause the whole thing to fail) |
||
80 | */ |
||
81 | function data ($name) { |
||
94 | /** |
||
95 | * @param array[] $files |
||
96 | * @param string $file_path |
||
97 | * |
||
98 | * @return array[] |
||
99 | */ |
||
100 | protected function normalize_files ($files, $file_path = '') { |
||
127 | /** |
||
128 | * @param array $file |
||
129 | * @param string $file_path |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | protected function normalize_file ($file, $file_path) { |
||
149 | /** |
||
150 | * Parsing request body for following Content-Type: `application/json`, `application/x-www-form-urlencoded` and `multipart/form-data` |
||
151 | * |
||
152 | * @throws ExitException |
||
153 | */ |
||
154 | protected function parse_data_stream () { |
||
189 | /** |
||
190 | * Parse content stream |
||
191 | * |
||
192 | * @param resource $stream |
||
193 | * @param string $boundary |
||
194 | * |
||
195 | * @return array[]|false |
||
196 | * |
||
197 | * @throws UnexpectedValueException |
||
198 | * @throws ExitException |
||
199 | */ |
||
200 | protected function parse_multipart_into_parts ($stream, $boundary) { |
||
269 | /** |
||
270 | * @param resource $stream |
||
271 | * @param array[] $parts |
||
272 | * |
||
273 | * @return array[] |
||
274 | */ |
||
275 | protected function parse_multipart_analyze_parts ($stream, $parts) { |
||
322 | /** |
||
323 | * @return int |
||
324 | */ |
||
325 | protected function post_max_size () { |
||
337 | /** |
||
338 | * @return int |
||
339 | */ |
||
340 | protected function upload_max_file_size () { |
||
352 | /** |
||
353 | * @param resource $stream |
||
354 | * @param string $next_data |
||
355 | * @param string $target |
||
356 | * |
||
357 | * @return array |
||
1 ignored issue
–
show
|
|||
358 | * |
||
359 | * @throws UnexpectedValueException |
||
360 | */ |
||
361 | protected function parse_multipart_find ($stream, $next_data, $target) { |
||
381 | /** |
||
382 | * @param string $content |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | protected function parse_multipart_headers ($content) { |
||
408 | /** |
||
409 | * @param array $source |
||
410 | * @param string $name |
||
411 | * @param array|string $value |
||
412 | */ |
||
413 | protected function parse_multipart_set_target (&$source, $name, $value) { |
||
430 | } |
||
431 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.