Test Setup Failed
Push — feature/code_improvement ( 4d8315...7e1ceb )
by Thierry
03:35
created

FileUpload   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setNameSanitizer() 0 4 1
C getUploadedFiles() 0 67 11
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
     * Read uploaded files info from HTTP request data
42
     *
43
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
44
     */
45
    public function getUploadedFiles()
46
    {
47
        // Check validity of the uploaded files
48
        $aTempFiles = [];
49
        foreach($_FILES as $sVarName => $aFile)
50
        {
51
            // If there is only one file, transform each entry into an array,
52
            // so the same processing for multiple files can be applied.
53
            if(!is_array($aFile['name']))
54
            {
55
                $aFile['name'] = [$aFile['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...
56
                $aFile['type'] = [$aFile['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...
57
                $aFile['tmp_name'] = [$aFile['tmp_name']];
58
                $aFile['error'] = [$aFile['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...
59
                $aFile['size'] = [$aFile['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...
60
            }
61
62
            $nFileCount = count($aFile['name']);
63
            for($i = 0; $i < $nFileCount; $i++)
64
            {
65
                if(!$aFile['name'][$i])
66
                {
67
                    continue;
68
                }
69
                if(!array_key_exists($sVarName, $aTempFiles))
70
                {
71
                    $aTempFiles[$sVarName] = [];
72
                }
73
                // Filename without the extension
74
                $sFilename = pathinfo($aFile['name'][$i], PATHINFO_FILENAME);
75
                if(($this->cNameSanitizer))
76
                {
77
                    $sFilename = (string)call_user_func_array($$this->cNameSanitizer, [$sFilename, $sVarName]);
78
                }
79
                        // Copy the file data into the local array
80
                $aTempFiles[$sVarName][] = [
81
                    'name' => $aFile['name'][$i],
82
                    'type' => $aFile['type'][$i],
83
                    'tmp_name' => $aFile['tmp_name'][$i],
84
                    'error' => $aFile['error'][$i],
85
                    'size' => $aFile['size'][$i],
86
                    'filename' => $sFilename,
87
                    'extension' => pathinfo($aFile['name'][$i], PATHINFO_EXTENSION),
88
                ];
89
            }
90
        }
91
92
        // Check uploaded files validity
93
        foreach($aTempFiles as $sVarName => $aFiles)
94
        {
95
            foreach($aFiles as $aFile)
96
            {
97
                // Verify upload result
98
                if($aFile['error'] != 0)
99
                {
100
                    throw new \Jaxon\Exception\Error($this->trans('errors.upload.failed', $aFile));
101
                }
102
                // Verify file validity (format, size)
103
                if(!$this->validateUploadedFile($sVarName, $aFile))
104
                {
105
                    throw new \Jaxon\Exception\Error($this->getValidatorMessage());
106
                }
107
           }
108
        }
109
110
        return $aTempFiles;
111
    }
112
}
113