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) { |
||
56 | if (is_resource($this->data_stream)) { |
||
57 | fclose($this->data_stream); |
||
58 | } |
||
59 | $this->data = $data; |
||
60 | $this->files = $this->normalize_files($files); |
||
61 | $data_stream = is_string($data_stream) ? fopen($data_stream, 'rb') : $data_stream; |
||
62 | if ($copy_stream && is_resource($data_stream)) { |
||
63 | $this->data_stream = fopen('php://temp', 'w+b'); |
||
64 | stream_copy_to_stream($data_stream, $this->data_stream); |
||
65 | rewind($this->data_stream); |
||
66 | fclose($data_stream); |
||
67 | } else { |
||
68 | $this->data_stream = $data_stream; |
||
69 | } |
||
70 | /** |
||
71 | * If we don't appear to have any data or files detected - probably, we need to parse request ourselves |
||
72 | */ |
||
73 | if (!$this->data && !$this->files && is_resource($this->data_stream)) { |
||
74 | $this->parse_data_stream(); |
||
75 | } |
||
76 | // Hack: for compatibility we'll override $_POST since it might be filled during parsing |
||
77 | $_POST = $this->data; |
||
78 | } |
||
79 | /** |
||
80 | * Get data item by name |
||
81 | * |
||
82 | * @param string|string[] $name |
||
83 | * |
||
84 | * @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) |
||
85 | */ |
||
86 | function data ($name) { |
||
87 | if (is_array($name)) { |
||
88 | foreach ($name as &$n) { |
||
89 | if (!isset($this->data[$n])) { |
||
90 | return false; |
||
91 | } |
||
92 | $n = $this->data[$n]; |
||
93 | } |
||
94 | return $name; |
||
95 | } |
||
96 | /** @noinspection OffsetOperationsInspection */ |
||
97 | return isset($this->data[$name]) ? $this->data[$name] : false; |
||
98 | } |
||
99 | /** |
||
100 | * @param array[] $files |
||
101 | * @param string $file_path |
||
102 | * |
||
103 | * @return array[] |
||
104 | */ |
||
105 | protected function normalize_files ($files, $file_path = '') { |
||
106 | if (!isset($files['name'])) { |
||
107 | foreach ($files as $field => &$file) { |
||
108 | $file = $this->normalize_files($file, "$file_path/$field"); |
||
109 | } |
||
110 | return $files; |
||
111 | } |
||
112 | if (is_array($files['name'])) { |
||
113 | $result = []; |
||
114 | foreach (array_keys($files['name']) as $index) { |
||
115 | $result[] = $this->normalize_file( |
||
116 | [ |
||
117 | 'name' => $files['name'][$index], |
||
118 | 'type' => $files['type'][$index], |
||
119 | 'size' => $files['size'][$index], |
||
120 | 'tmp_name' => @$files['tmp_name'][$index] ?: null, |
||
121 | 'stream' => @$files['stream'][$index] ?: null, |
||
122 | 'error' => $files['error'][$index] |
||
123 | ], |
||
124 | $file_path |
||
125 | ); |
||
126 | } |
||
127 | return $result; |
||
128 | } else { |
||
129 | return $this->normalize_file($files, $file_path); |
||
130 | } |
||
131 | } |
||
132 | /** |
||
133 | * @param array $file |
||
134 | * @param string $file_path |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | protected function normalize_file ($file, $file_path) { |
||
139 | $file += [ |
||
140 | 'tmp_name' => null, |
||
141 | 'stream' => null |
||
142 | ]; |
||
143 | if (isset($file['tmp_name']) && $file['stream'] === null) { |
||
144 | $file['stream'] = fopen($file['tmp_name'], 'rb'); |
||
145 | } |
||
146 | if (isset($file['stream']) && !$file['tmp_name']) { |
||
147 | $file['tmp_name'] = "request-file://".$file_path; |
||
148 | } |
||
149 | if ($file['tmp_name'] === null && $file['stream'] === null) { |
||
150 | $file['error'] = UPLOAD_ERR_NO_FILE; |
||
151 | } |
||
152 | return $file; |
||
153 | } |
||
154 | /** |
||
155 | * Parsing request body for following Content-Type: `application/json`, `application/x-www-form-urlencoded` and `multipart/form-data` |
||
156 | * |
||
157 | * @throws ExitException |
||
158 | */ |
||
159 | protected function parse_data_stream () { |
||
160 | $content_type = $this->header('content-type'); |
||
161 | /** |
||
162 | * application/json |
||
163 | */ |
||
164 | if (preg_match('#^application/([^+\s]+\+)?json#', $content_type)) { |
||
165 | $this->data = _json_decode(stream_get_contents($this->data_stream)) ?: []; |
||
166 | return; |
||
167 | } |
||
168 | /** |
||
169 | * application/x-www-form-urlencoded |
||
170 | */ |
||
171 | if (strpos($content_type, 'application/x-www-form-urlencoded') === 0) { |
||
172 | @parse_str(stream_get_contents($this->data_stream), $this->data); |
||
173 | return; |
||
174 | } |
||
175 | /** |
||
176 | * multipart/form-data |
||
177 | */ |
||
178 | if (preg_match('#multipart/form-data;.*boundary="?([^;"]{1,70})(?:"|;|$)#Ui', $content_type, $matches)) { |
||
179 | try { |
||
180 | $parts = $this->parse_multipart_into_parts($this->data_stream, trim($matches[1])) ?: []; |
||
181 | list($this->data, $files) = $this->parse_multipart_analyze_parts($this->data_stream, $parts); |
||
182 | $this->files = $this->normalize_files($files); |
||
183 | } catch (UnexpectedValueException $e) { |
||
184 | // Do nothing, if parsing failed then we'll just leave `::$data` and `::$files` empty |
||
185 | } |
||
186 | } |
||
187 | rewind($this->data_stream); |
||
188 | } |
||
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 |
||
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 |