Completed
Push — master ( 79d89d...1bfd8b )
by Adrian
02:23
created

Arr::remapFilesArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Sirius\Upload\Util;
4
5
class Arr extends \Sirius\Validation\Util\Arr
6
{
7 2
    public static function remapFilesArray(array $files)
8
    {
9 2
        $result = array();
10 2
        foreach ($files['name'] as $k => $v) {
11 2
            $result[$k] = array(
12 2
                'name' => $files['name'][$k],
13 2
                'type' => @$files['type'][$k],
14 2
                'size' => @$files['size'][$k],
15 2
                'error' => @$files['error'][$k],
16 2
                'tmp_name' => $files['tmp_name'][$k]
17 2
            );
18 2
        }
19
20 2
        return $result;
21
    }
22
23
    /**
24
     * Fixes the $_FILES array problem and ensures the result is an array of files
25
     *
26
     * PHP's $_FILES variable is not properly formated for iteration when
27
     * multiple files are uploaded under the same name
28
     * @see http://www.php.net/manual/en/features.file-upload.php
29
     *
30
     * @param  array $files
31
     * @return array
32
     */
33 12
    public static function normalizeFiles(array $files)
34
    {
35
        // The caller passed $_FILES['some_field_name']
36 12
        if (isset($files['name'])) {
37
            // we have a single file
38 10
            if(!is_array($files['name'])) {
39 8
                return array($files);
40
            }
41
            // we have list of files, which PHP messes up
42
            else {
43 2
                return Arr::remapFilesArray($files);
44
            }
45
        }
46
        // The caller passed $_FILES
47
        else {
48 2
            $keys = array_keys($files);
49 2
            if (isset($keys[0]) && isset($files[$keys[0]]['name'])) {
50 1
                if (!is_array($files[$keys[0]]['name'])) {
51
                    // $files is in the correct format already, even in the
52
                    // case it contains a single element.
53 1
                    return $files;
54
                }
55
                // we have list of files, which PHP messes up
56
                else {
57
                    return Arr::remapFilesArray($files[$keys[0]]);
0 ignored issues
show
Documentation introduced by
$files[$keys[0]] is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
                }
59
            }
60
        }
61
62
        // if we got here, the $file argument is wrong
63 1
        return array();
64
    }
65
66
}
67