Passed
Push — feature/code_improvement ( 374d0e...1b7fb9 )
by Thierry
02:42
created

FileUpload::getUploadedFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * FileUpload.php - This class handles HTTP file upload.
5
 *
6
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
9
 * @link https://github.com/jaxon-php/jaxon-core
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
11
12
namespace Jaxon\Request\Support;
13
14
use Closure;
15
16
class FileUpload
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class FileUpload
Loading history...
17
{
18
    use \Jaxon\Features\Validator;
19
    use \Jaxon\Features\Translator;
20
21
    /**
22
     * A user defined function to transform uploaded file names
23
     *
24
     * @var Closure
25
     */
26
    protected $cNameSanitizer = null;
27
28
    /**
29
     * Filter uploaded file name
30
     *
31
     * @param Closure       $cNameSanitizer            The closure which filters filenames
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
32
     *
33
     * @return void
34
     */
35
    public function setNameSanitizer(Closure $cNameSanitizer)
36
    {
37
        $this->cNameSanitizer = $cNameSanitizer;
38
    }
39
40
    /**
41
     * Check uploaded files
42
     *
43
     * @param array     $aFiles     The uploaded files
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
44
     *
45
     * @return void
46
     * @throws \Jaxon\Exception\Error
47
     */
48
    private function checkFiles(array $aFiles)
49
    {
50
        foreach($aFiles as $sVarName => $aVarFiles)
51
        {
52
            foreach($aVarFiles as $aFile)
53
            {
54
                // Verify upload result
55
                if($aFile['error'] != 0)
56
                {
57
                    throw new \Jaxon\Exception\Error($this->trans('errors.upload.failed', $aFile));
58
                }
59
                // Verify file validity (format, size)
60
                if(!$this->validateUploadedFile($sVarName, $aFile))
61
                {
62
                    throw new \Jaxon\Exception\Error($this->getValidatorMessage());
63
                }
64
           }
65
        }
66
    }
67
68
    /**
69
     * Get a file from upload entry
70
     *
71
     * @param string    $sVarName       The corresponding variable
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 7 found
Loading history...
72
     * @param array     $aVarFiles      An entry in the PHP $_FILES array
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
73
     * @param integer   $nPosition      The postion of the file to be processed
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
74
     *
75
     * @return array|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
76
     */
77
    private function getUploadedFile($sVarName, array $aVarFiles, $nPosition)
78
    {
79
        if(!$aVarFiles['name'][$nPosition])
80
        {
81
            return null;
82
        }
83
84
        // Filename without the extension
85
        $sFilename = pathinfo($aVarFiles['name'][$nPosition], PATHINFO_FILENAME);
86
        if(($this->cNameSanitizer))
87
        {
88
            $sFilename = (string)call_user_func_array($this->cNameSanitizer, [$sFilename, $sVarName]);
89
        }
90
91
        return [
92
            'name' => $aVarFiles['name'][$nPosition],
93
            'type' => $aVarFiles['type'][$nPosition],
94
            'tmp_name' => $aVarFiles['tmp_name'][$nPosition],
95
            'error' => $aVarFiles['error'][$nPosition],
96
            'size' => $aVarFiles['size'][$nPosition],
97
            'filename' => $sFilename,
98
            'extension' => pathinfo($aVarFiles['name'][$nPosition], PATHINFO_EXTENSION),
99
        ];
100
    }
101
102
    /**
103
     * Read uploaded files info from HTTP request data
104
     *
105
     * @return array
106
     */
107
    public function getUploadedFiles()
108
    {
109
        // Check validity of the uploaded files
110
        $aUploadedFiles = [];
111
        foreach($_FILES as $sVarName => $aVarFiles)
112
        {
113
            // If there is only one file, transform each entry into an array,
114
            // so the same processing for multiple files can be applied.
115
            if(!is_array($aVarFiles['name']))
116
            {
117
                $aVarFiles['name'] = [$aVarFiles['name']];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
118
                $aVarFiles['type'] = [$aVarFiles['type']];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
119
                $aVarFiles['tmp_name'] = [$aVarFiles['tmp_name']];
120
                $aVarFiles['error'] = [$aVarFiles['error']];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
121
                $aVarFiles['size'] = [$aVarFiles['size']];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
122
            }
123
124
            $nFileCount = count($aVarFiles['name']);
125
            for($i = 0; $i < $nFileCount; $i++)
126
            {
127
                $aUploadedFile = $this->getUploadedFile($sVarName, $aVarFiles, $i);
128
                if(is_array($aUploadedFile))
129
                {
130
                    if(!array_key_exists($sVarName, $aUploadedFiles))
131
                    {
132
                        $aUploadedFiles[$sVarName] = [];
133
                    }
134
                    $aUploadedFiles[$sVarName][] = $aUploadedFile;
135
                }
136
            }
137
        }
138
139
        // Check uploaded files validity
140
        $this->checkFiles($aUploadedFiles);
141
142
        return $aUploadedFiles;
143
    }
144
}
145