server_request_files()   B
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 25
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 41
ccs 28
cts 28
cp 1
crap 7
rs 8.5866
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message;
13
14
use Sunrise\Http\Message\Stream\FileStream;
15
16
use function is_array;
17
18
use const UPLOAD_ERR_OK;
19
use const UPLOAD_ERR_NO_FILE;
20
21
/**
22
 * @link http://php.net/manual/en/reserved.variables.files.php
23
 * @link https://www.php.net/manual/ru/features.file-upload.post-method.php
24
 * @link https://www.php.net/manual/ru/features.file-upload.multiple.php
25
 * @link https://github.com/php/php-src/blob/8c5b41cefb88b753c630b731956ede8d9da30c5d/main/rfc1867.c
26
 */
27
function server_request_files(?array $files = null): array
28
{
29 39
    $files ??= $_FILES;
30
31 39
    $walker = static function ($path, $size, $error, $name, $type) use (&$walker) {
32 2
        if (!is_array($path)) {
33 2
            $stream = $error === UPLOAD_ERR_OK ? new FileStream($path, 'rb') : null;
34
35 2
            return new UploadedFile($stream, $size, $error, $name, $type);
36
        }
37
38 1
        $result = [];
39 1
        foreach ($path as $key => $_) {
40 1
            if ($error[$key] !== UPLOAD_ERR_NO_FILE) {
41 1
                $result[$key] = $walker(
42 1
                    $path[$key],
43 1
                    $size[$key],
44 1
                    $error[$key],
45 1
                    $name[$key],
46 1
                    $type[$key],
47 1
                );
48
            }
49
        }
50
51 1
        return $result;
52 39
    };
53
54 39
    $result = [];
55 39
    foreach ($files as $key => $file) {
56 2
        if ($file['error'] !== UPLOAD_ERR_NO_FILE) {
57 2
            $result[$key] = $walker(
58 2
                $file['tmp_name'],
59 2
                $file['size'],
60 2
                $file['error'],
61 2
                $file['name'],
62 2
                $file['type'],
63 2
            );
64
        }
65
    }
66
67 39
    return $result;
68
}
69