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 | 26 | function init_data_and_files ($data = [], $files = [], $data_stream = null, $copy_stream = true) { |
|
| 57 | 26 | if (is_resource($this->data_stream)) { |
|
| 58 | fclose($this->data_stream); |
||
| 59 | } |
||
| 60 | 26 | $this->data = $data; |
|
| 61 | 26 | $this->files = $this->normalize_files($files); |
|
| 62 | 26 | $data_stream = is_string($data_stream) ? fopen($data_stream, 'rb') : $data_stream; |
|
| 63 | 26 | if ($copy_stream && is_resource($data_stream)) { |
|
| 64 | 24 | $this->data_stream = fopen('php://temp', 'w+b'); |
|
| 65 | 24 | stream_copy_to_stream($data_stream, $this->data_stream); |
|
| 66 | 24 | fclose($data_stream); |
|
| 67 | } else { |
||
| 68 | 2 | $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 | 26 | if (!$this->data && !$this->files && is_resource($this->data_stream)) { |
|
| 74 | 20 | $this->parse_data_stream(); |
|
| 75 | } |
||
| 76 | // Hack: for compatibility we'll override $_POST since it might be filled during parsing |
||
| 77 | 26 | $_POST = $this->data; |
|
| 78 | 26 | } |
|
| 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 | 32 | function data (...$name) { |
|
| 88 | 32 | if (count($name) === 1) { |
|
| 89 | 28 | $name = $name[0]; |
|
| 90 | } |
||
| 91 | /** |
||
| 92 | * @var string|string[] $name |
||
| 93 | */ |
||
| 94 | 32 | if (is_array($name)) { |
|
| 95 | 6 | $result = []; |
|
| 96 | 6 | foreach ($name as &$n) { |
|
| 97 | 6 | if (!array_key_exists($n, $this->data)) { |
|
| 98 | 2 | return null; |
|
| 99 | } |
||
| 100 | 6 | $result[$n] = $this->data[$n]; |
|
| 101 | } |
||
| 102 | 6 | return $result; |
|
| 103 | } |
||
| 104 | /** @noinspection OffsetOperationsInspection */ |
||
| 105 | 28 | 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 | 2 | function files ($name) { |
|
| 115 | 2 | return @$this->files[$name]; |
|
| 116 | } |
||
| 117 | /** |
||
| 118 | * @param array[] $files |
||
| 119 | * @param string $file_path |
||
| 120 | * |
||
| 121 | * @return array[] |
||
| 122 | */ |
||
| 123 | 26 | protected function normalize_files ($files, $file_path = '') { |
|
| 124 | 26 | if (!isset($files['name'])) { |
|
| 125 | 26 | foreach ($files as $field => &$file) { |
|
| 126 | 2 | $file = $this->normalize_files($file, "$file_path/$field"); |
|
| 127 | } |
||
| 128 | 26 | return $files; |
|
| 129 | } |
||
| 130 | 2 | if (is_array($files['name'])) { |
|
| 131 | 2 | $result = []; |
|
| 132 | 2 | foreach (array_keys($files['name']) as $index) { |
|
| 133 | 2 | $result[] = $this->normalize_file( |
|
| 134 | [ |
||
| 135 | 2 | 'name' => $files['name'][$index], |
|
| 136 | 2 | 'type' => $files['type'][$index], |
|
| 137 | 2 | 'size' => $files['size'][$index], |
|
| 138 | 2 | 'tmp_name' => @$files['tmp_name'][$index], |
|
| 139 | 2 | 'stream' => @$files['stream'][$index], |
|
| 140 | 2 | 'error' => $files['error'][$index] |
|
| 141 | ], |
||
| 142 | 2 | "$file_path/$index" |
|
| 143 | ); |
||
| 144 | } |
||
| 145 | 2 | return $result; |
|
| 146 | } else { |
||
| 147 | 2 | 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 | 2 | protected function normalize_file ($file, $file_path) { |
|
| 157 | $file += [ |
||
| 158 | 2 | 'tmp_name' => null, |
|
| 159 | 'stream' => null |
||
| 160 | ]; |
||
| 161 | 2 | if (isset($file['tmp_name']) && $file['stream'] === null) { |
|
| 162 | 2 | $file['stream'] = fopen($file['tmp_name'], 'rb'); |
|
| 163 | } |
||
| 164 | 2 | if (isset($file['stream']) && !$file['tmp_name']) { |
|
| 165 | 2 | $file['tmp_name'] = "request-file://$file_path"; |
|
| 166 | } |
||
| 167 | 2 | if ($file['tmp_name'] === null && $file['stream'] === null) { |
|
| 168 | $file['error'] = UPLOAD_ERR_NO_FILE; |
||
| 169 | } |
||
| 170 | 2 | return $file; |
|
| 171 | } |
||
| 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 | 20 | 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) { |
||
| 219 | $parts = []; |
||
| 220 | $crlf = "\r\n"; |
||
| 221 | $position = 0; |
||
| 222 | $body = ''; |
||
| 223 | list($offset, $body) = $this->parse_multipart_find($stream, $body, "--$boundary$crlf"); |
||
| 224 | /** |
||
| 225 | * strlen doesn't take into account trailing CRLF since we'll need it in loop below |
||
| 226 | */ |
||
| 227 | $position += $offset + strlen("--$boundary"); |
||
| 228 | $body = substr($body, strlen("--$boundary")); |
||
| 229 | /** |
||
| 230 | * Each part always starts with CRLF |
||
| 231 | */ |
||
| 232 | while (strpos($body, $crlf) === 0) { |
||
| 233 | $position += 2; |
||
| 234 | $body = substr($body, 2); |
||
| 235 | $part = [ |
||
| 236 | 'headers' => [ |
||
| 237 | 'offset' => $position, |
||
| 238 | 'size' => 0 |
||
| 239 | ], |
||
| 240 | 'body' => [ |
||
| 241 | 'offset' => 0, |
||
| 242 | 'size' => 0 |
||
| 243 | ] |
||
| 244 | ]; |
||
| 245 | if (strpos($body, $crlf) === 0) { |
||
| 246 | /** |
||
| 247 | * No headers |
||
| 248 | */ |
||
| 249 | $position += 2; |
||
| 250 | $body = substr($body, 2); |
||
| 251 | } else { |
||
| 252 | /** |
||
| 253 | * Find headers end in order to determine size |
||
| 254 | */ |
||
| 255 | list($offset, $body) = $this->parse_multipart_find($stream, $body, $crlf.$crlf); |
||
| 256 | $part['headers']['size'] = $offset; |
||
| 257 | $position += $offset + 4; |
||
| 258 | $body = substr($body, 4); |
||
| 259 | } |
||
| 260 | $part['body']['offset'] = $position; |
||
| 261 | /** |
||
| 262 | * Find body end in order to determine its size |
||
| 263 | */ |
||
| 264 | list($offset, $body) = $this->parse_multipart_find($stream, $body, "$crlf--$boundary"); |
||
| 265 | $part['body']['size'] = $offset; |
||
| 266 | $position += $offset + strlen("$crlf--$boundary"); |
||
| 267 | $body = substr($body, strlen("$crlf--$boundary")); |
||
| 268 | if ($part['headers']['size']) { |
||
| 269 | $parts[] = $part; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | /** |
||
| 273 | * Last boundary after all parts ends with '--' and we don't care what rubbish happens after it |
||
| 274 | */ |
||
| 275 | $post_max_size = $this->post_max_size(); |
||
| 276 | if (0 !== strpos($body, '--')) { |
||
| 277 | return false; |
||
| 278 | } |
||
| 279 | /** |
||
| 280 | * Check whether body size is bigger than allowed limit |
||
| 281 | */ |
||
| 282 | if ($position + strlen($body) > $post_max_size) { |
||
| 283 | throw new ExitException(413); |
||
| 284 | } |
||
| 285 | return $parts; |
||
| 286 | } |
||
| 287 | /** |
||
| 288 | * @param resource $stream |
||
| 289 | * @param array[] $parts |
||
| 290 | * |
||
| 291 | * @return array[] |
||
| 292 | */ |
||
| 293 | protected function parse_multipart_analyze_parts ($stream, $parts) { |
||
| 294 | $data = []; |
||
| 295 | $files = []; |
||
| 296 | foreach ($parts as $part) { |
||
| 297 | $headers = $this->parse_multipart_headers( |
||
| 298 | stream_get_contents($stream, $part['headers']['size'], $part['headers']['offset']) |
||
| 299 | ); |
||
| 300 | if ( |
||
| 301 | !isset($headers['content-disposition'][0], $headers['content-disposition']['name']) || |
||
| 302 | $headers['content-disposition'][0] != 'form-data' |
||
| 303 | ) { |
||
| 304 | continue; |
||
| 305 | } |
||
| 306 | $name = $headers['content-disposition']['name']; |
||
| 307 | if (isset($headers['content-disposition']['filename'])) { |
||
| 308 | $file = [ |
||
| 309 | 'name' => $headers['content-disposition']['filename'], |
||
| 310 | 'type' => @$headers['content-type'] ?: 'application/octet-stream', |
||
| 311 | 'size' => $part['body']['size'], |
||
| 312 | 'stream' => Stream_slicer::slice($stream, $part['body']['offset'], $part['body']['size']), |
||
| 313 | 'error' => UPLOAD_ERR_OK |
||
| 314 | ]; |
||
| 315 | if ($headers['content-disposition']['filename'] === '') { |
||
| 316 | $file['type'] = ''; |
||
| 317 | $file['stream'] = null; |
||
| 318 | $file['error'] = UPLOAD_ERR_NO_FILE; |
||
| 319 | } elseif ($file['size'] > $this->upload_max_file_size()) { |
||
| 320 | $file['stream'] = null; |
||
| 321 | $file['error'] = UPLOAD_ERR_INI_SIZE; |
||
| 322 | } |
||
| 323 | $this->parse_multipart_set_target($files, $name, $file); |
||
| 324 | } else { |
||
| 325 | if ($part['body']['size'] == 0) { |
||
| 326 | $this->parse_multipart_set_target($data, $name, ''); |
||
| 327 | } else { |
||
| 328 | $this->parse_multipart_set_target( |
||
| 329 | $data, |
||
| 330 | $name, |
||
| 331 | stream_get_contents($stream, $part['body']['size'], $part['body']['offset']) |
||
| 332 | ); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | } |
||
| 336 | return [$data, $files]; |
||
| 337 | } |
||
| 338 | /** |
||
| 339 | * @return int |
||
| 340 | */ |
||
| 341 | protected function post_max_size () { |
||
| 342 | $size = ini_get('post_max_size') ?: ini_get('hhvm.server.max_post_size'); |
||
| 343 | return $this->convert_size_to_bytes($size); |
||
| 344 | } |
||
| 345 | /** |
||
| 346 | * @return int |
||
| 347 | */ |
||
| 348 | protected function upload_max_file_size () { |
||
| 349 | $size = ini_get('upload_max_filesize') ?: ini_get('hhvm.server.upload.upload_max_file_size'); |
||
| 350 | return $this->convert_size_to_bytes($size); |
||
| 351 | } |
||
| 352 | /** |
||
| 353 | * @param int|string $size |
||
| 354 | * |
||
| 355 | * @return int |
||
| 356 | */ |
||
| 357 | protected function convert_size_to_bytes ($size) { |
||
| 358 | switch (strtolower(substr($size, -1))) { |
||
| 359 | case 'g'; |
||
| 360 | $size = (int)$size * 1024; |
||
| 361 | case 'm'; |
||
| 362 | $size = (int)$size * 1024; |
||
| 363 | case 'k'; |
||
| 364 | $size = (int)$size * 1024; |
||
| 365 | } |
||
| 366 | return (int)$size ?: PHP_INT_MAX; |
||
| 367 | } |
||
| 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) { |
||
| 378 | $offset = 0; |
||
| 379 | $prev_data = ''; |
||
| 380 | while (($found = strpos($prev_data.$next_data, $target)) === false) { |
||
| 381 | if (feof($stream)) { |
||
| 382 | throw new UnexpectedValueException; |
||
| 383 | } |
||
| 384 | if ($prev_data) { |
||
| 385 | $offset += strlen($prev_data); |
||
| 386 | } |
||
| 387 | $prev_data = $next_data; |
||
| 388 | $next_data = fread($stream, 1024); |
||
| 389 | } |
||
| 390 | $offset += $found; |
||
| 391 | /** |
||
| 392 | * Read some more bytes so that we'll always have some remainder in place, since empty remainder might cause problems with `strpos()` call later |
||
| 393 | */ |
||
| 394 | $remainder = substr($prev_data.$next_data, $found).(fread($stream, 1024) ?: ''); |
||
| 395 | return [$offset, $remainder]; |
||
| 396 | } |
||
| 397 | /** |
||
| 398 | * @param string $content |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | protected function parse_multipart_headers ($content) { |
||
| 403 | $headers = []; |
||
| 404 | foreach (explode("\r\n", $content) as $header) { |
||
| 405 | list($name, $value) = explode(':', $header, 2); |
||
| 406 | if (!preg_match_all('/(.+)(?:="*?(.*)"?)?(?:;\s|$)/U', $value, $matches)) { |
||
| 407 | continue; |
||
| 408 | } |
||
| 409 | $name = strtolower($name); |
||
| 410 | $headers[$name] = []; |
||
| 411 | foreach (array_keys($matches[1]) as $index) { |
||
| 412 | if (isset($headers[$name][0]) || strlen($matches[2][$index])) { |
||
| 413 | $headers[$name][trim($matches[1][$index])] = urldecode(trim($matches[2][$index])); |
||
| 414 | } else { |
||
| 415 | $headers[$name][] = trim($matches[1][$index]); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | if (count($headers[$name]) == 1) { |
||
| 419 | $headers[$name] = $headers[$name][0]; |
||
| 420 | } |
||
| 421 | } |
||
| 422 | return $headers; |
||
| 423 | } |
||
| 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 |