Passed
Pull Request — master (#31)
by Anatoly
39:16
created

server_request_files()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 36
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 54
ccs 40
cts 40
cp 1
crap 7
rs 8.4106

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
/**
15
 * Import classes
16
 */
17
use Sunrise\Http\Message\Stream\FileStream;
18
19
/**
20
 * Import functions
21
 */
22
use function is_array;
23
24
/**
25
 * Import constants
26
 */
27
use const UPLOAD_ERR_OK;
28
use const UPLOAD_ERR_NO_FILE;
29
30
/**
31
 * Gets the request's uploaded files
32
 *
33
 * Note that not sent files will not be handled.
34
 *
35
 * @param array|null $files
36
 *
37
 * @return array
38
 *
39
 * @link http://php.net/manual/en/reserved.variables.files.php
40
 * @link https://www.php.net/manual/ru/features.file-upload.post-method.php
41
 * @link https://www.php.net/manual/ru/features.file-upload.multiple.php
42
 * @link https://github.com/php/php-src/blob/8c5b41cefb88b753c630b731956ede8d9da30c5d/main/rfc1867.c
43
 */
44
function server_request_files(?array $files = null): array
45
{
46 39
    $files ??= $_FILES;
47
48 39
    $walker = function (
49 39
        $path,
50 39
        $size,
51 39
        $error,
52 39
        $name,
53 39
        $type
54 39
    ) use (&$walker) {
55 2
        if (!is_array($path)) {
56
            // It makes no sense to create a stream if the file has not been successfully uploaded.
57 2
            $stream = UPLOAD_ERR_OK <> $error ? null : new FileStream($path, 'rb');
58
59 2
            return new UploadedFile(
60 2
                $stream,
61 2
                $size,
62 2
                $error,
63 2
                $name,
64 2
                $type
65 2
            );
66
        }
67
68 1
        $result = [];
69 1
        foreach ($path as $key => $_) {
70 1
            if (UPLOAD_ERR_NO_FILE <> $error[$key]) {
71 1
                $result[$key] = $walker(
72 1
                    $path[$key],
73 1
                    $size[$key],
74 1
                    $error[$key],
75 1
                    $name[$key],
76 1
                    $type[$key]
77 1
                );
78
            }
79
        }
80
81 1
        return $result;
82 39
    };
83
84 39
    $result = [];
85 39
    foreach ($files as $key => $file) {
86 2
        if (UPLOAD_ERR_NO_FILE <> $file['error']) {
87 2
            $result[$key] = $walker(
88 2
                $file['tmp_name'],
89 2
                $file['size'],
90 2
                $file['error'],
91 2
                $file['name'],
92 2
                $file['type']
93 2
            );
94
        }
95
    }
96
97 39
    return $result;
98
}
99