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 | function init_data_and_files ($data = [], $files = [], $data_stream = null, $copy_stream = true) { |
||
79 | /** |
||
80 | * Get data item by name |
||
81 | * |
||
82 | * @param string[]|string[][] $name |
||
83 | * |
||
84 | * @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 |
||
85 | * missing key will cause the whole thing to fail) |
||
86 | */ |
||
87 | function data (...$name) { |
||
88 | if (count($name) === 1) { |
||
89 | $name = $name[0]; |
||
90 | } |
||
91 | /** |
||
92 | * @var string|string[] $name |
||
93 | */ |
||
94 | if (is_array($name)) { |
||
95 | $result = []; |
||
96 | foreach ($name as &$n) { |
||
97 | if (!array_key_exists($n, $this->data)) { |
||
98 | return null; |
||
99 | } |
||
100 | $result[$n] = $this->data[$n]; |
||
101 | } |
||
102 | return $result; |
||
103 | } |
||
104 | /** @noinspection OffsetOperationsInspection */ |
||
105 | return @$this->data[$name]; |
||
106 | } |
||
107 | /** |
||
108 | * Get file item by name |
||
109 | * |
||
110 | * @param string $name |
||
111 | * |
||
112 | * @return array|null File item if exists or `null` otherwise |
||
113 | */ |
||
114 | function files ($name) { |
||
115 | return @$this->files[$name]; |
||
116 | } |
||
117 | /** |
||
118 | * @param array[] $files |
||
119 | * @param string $file_path |
||
120 | * |
||
121 | * @return array[] |
||
122 | */ |
||
123 | protected function normalize_files ($files, $file_path = '') { |
||
124 | if (!isset($files['name'])) { |
||
125 | foreach ($files as $field => &$file) { |
||
126 | $file = $this->normalize_files($file, "$file_path/$field"); |
||
127 | } |
||
128 | return $files; |
||
129 | } |
||
130 | if (is_array($files['name'])) { |
||
131 | $result = []; |
||
132 | foreach (array_keys($files['name']) as $index) { |
||
133 | $result[] = $this->normalize_file( |
||
134 | [ |
||
135 | 'name' => $files['name'][$index], |
||
136 | 'type' => $files['type'][$index], |
||
137 | 'size' => $files['size'][$index], |
||
138 | 'tmp_name' => @$files['tmp_name'][$index], |
||
139 | 'stream' => @$files['stream'][$index], |
||
140 | 'error' => $files['error'][$index] |
||
141 | ], |
||
142 | $file_path |
||
143 | ); |
||
144 | } |
||
145 | return $result; |
||
146 | } else { |
||
147 | return $this->normalize_file($files, $file_path); |
||
148 | } |
||
149 | } |
||
150 | /** |
||
151 | * @param array $file |
||
152 | * @param string $file_path |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function normalize_file ($file, $file_path) { |
||
172 | /** |
||
173 | * Parsing request body for following Content-Type: `application/json`, `application/x-www-form-urlencoded` and `multipart/form-data` |
||
174 | * |
||
175 | * @throws ExitException |
||
176 | */ |
||
177 | protected function parse_data_stream () { |
||
207 | /** |
||
208 | * Parse content stream |
||
209 | * |
||
210 | * @param resource $stream |
||
211 | * @param string $boundary |
||
212 | * |
||
213 | * @return array[]|false |
||
214 | * |
||
215 | * @throws UnexpectedValueException |
||
216 | * @throws ExitException |
||
217 | */ |
||
218 | protected function parse_multipart_into_parts ($stream, $boundary) { |
||
287 | /** |
||
288 | * @param resource $stream |
||
289 | * @param array[] $parts |
||
290 | * |
||
291 | * @return array[] |
||
292 | */ |
||
293 | protected function parse_multipart_analyze_parts ($stream, $parts) { |
||
338 | /** |
||
339 | * @return int |
||
340 | */ |
||
341 | protected function post_max_size () { |
||
345 | /** |
||
346 | * @return int |
||
347 | */ |
||
348 | protected function upload_max_file_size () { |
||
352 | /** |
||
353 | * @param int|string $size |
||
354 | * |
||
355 | * @return int |
||
356 | */ |
||
357 | protected function convert_size_to_bytes ($size) { |
||
368 | /** |
||
369 | * @param resource $stream |
||
370 | * @param string $next_data |
||
371 | * @param string $target |
||
372 | * |
||
373 | * @return array |
||
374 | * |
||
375 | * @throws UnexpectedValueException |
||
376 | */ |
||
377 | protected function parse_multipart_find ($stream, $next_data, $target) { |
||
397 | /** |
||
398 | * @param string $content |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | protected function parse_multipart_headers ($content) { |
||
424 | /** |
||
425 | * @param array $source |
||
426 | * @param string $name |
||
427 | * @param array|string $value |
||
428 | */ |
||
429 | protected function parse_multipart_set_target (&$source, $name, $value) { |
||
446 | } |
||
447 |