Conditions | 7 |
Paths | 3 |
Total Lines | 54 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Tests | 40 |
CRAP Score | 7 |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
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 |