Passed
Push — master ( 1d699e...0f9ddd )
by Thierry
04:26 queued 02:06
created

File   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 198
rs 10
c 0
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A toTempData() 0 9 1
A extension() 0 3 1
A setNameSanitizer() 0 3 1
A fromHttpFile() 0 17 2
A slugify() 0 4 1
A path() 0 3 1
A filename() 0 3 1
A size() 0 3 1
A fromTempFile() 0 10 1
A type() 0 3 1
A name() 0 3 1
1
<?php
2
3
/**
4
 * File.php - This class represents an uploaded file.
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
 * @copyright 2017 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
12
13
namespace Jaxon\Request\Upload;
14
15
use Nyholm\Psr7\UploadedFile;
16
17
use Closure;
18
19
use function call_user_func_array;
20
use function pathinfo;
21
22
class File
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class File
Loading history...
23
{
24
    /**
25
     * The uploaded file type
26
     *
27
     * @var string
28
     */
29
    protected $sType;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
30
31
    /**
32
     * The uploaded file name, without the extension and slugified
33
     *
34
     * @var string
35
     */
36
    protected $sName;
37
38
    /**
39
     * The uploaded file name, with the extension
40
     *
41
     * @var string
42
     */
43
    protected $sFilename;
44
45
    /**
46
     * The uploaded file path
47
     *
48
     * @var string
49
     */
50
    protected $sPath;
51
52
    /**
53
     * The uploaded file size
54
     *
55
     * @var string
56
     */
57
    protected $sSize;
58
59
    /**
60
     * The uploaded file extension
61
     *
62
     * @var string
63
     */
64
    protected $sExtension;
65
66
    /**
67
     * A user defined function to transform uploaded file names
68
     *
69
     * @var Closure
70
     */
71
    protected static $cNameSanitizer = null;
72
73
    /**
74
     * Filter uploaded file name
75
     *
76
     * @param Closure $cNameSanitizer    The closure which filters filenames
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
77
     *
78
     * @return void
79
     */
80
    public static function setNameSanitizer(Closure $cNameSanitizer)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
81
    {
82
        self::$cNameSanitizer = $cNameSanitizer;
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
86
     * Slugify a text
87
     *
88
     * @param string  $sText
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
89
     *
90
     * @return string
91
     */
92
    protected static function slugify(string $sText): string
93
    {
94
        // Todo: slugify the text.
95
        return $sText;
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Create an instance of this class using data from an uploaded file.
100
     *
101
     * @param string $sVarName
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
102
     * @param string $sUploadDir    The directory where to save the uploaded file
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
103
     * @param UploadedFile $xHttpFile    The uploaded file
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
104
     *
105
     * @return File
106
     */
107
    public static function fromHttpFile(string $sVarName, string $sUploadDir, UploadedFile $xHttpFile): File
108
    {
109
        // Filename without the extension
110
        $sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME);
111
        if(self::$cNameSanitizer !== null)
112
        {
113
            $sName = (string)call_user_func_array(self::$cNameSanitizer, [$sName, $sVarName]);
114
        }
115
116
        $xFile = new File();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 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...
117
        $xFile->sType = $xHttpFile->getClientMediaType();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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
        $xFile->sName = self::slugify($sName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
Bug introduced by
It seems like $sName can also be of type array; however, parameter $sText of Jaxon\Request\Upload\File::slugify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
        $xFile->sName = self::slugify(/** @scrutinizer ignore-type */ $sName);
Loading history...
119
        $xFile->sFilename = $xHttpFile->getClientFilename();
120
        $xFile->sExtension = pathinfo($xHttpFile->getClientFilename(), PATHINFO_EXTENSION);
0 ignored issues
show
Documentation Bug introduced by
It seems like pathinfo($xHttpFile->get...oad\PATHINFO_EXTENSION) can also be of type array. However, the property $sExtension is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
121
        $xFile->sSize = $xHttpFile->getSize();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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
        $xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
Bug introduced by
Are you sure $xFile->sExtension of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
        $xFile->sPath = $sUploadDir . $xFile->sName . '.' . /** @scrutinizer ignore-type */ $xFile->sExtension;
Loading history...
123
        return $xFile;
124
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
125
126
    /**
127
     * Create an instance of this class using data from a temp file
128
     *
129
     * @param array $aFile    The uploaded file data
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
130
     *
131
     * @return File
132
     */
133
    public static function fromTempFile(array $aFile): File
134
    {
135
        $xFile = new File();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 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...
136
        $xFile->sType = $aFile['type'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
137
        $xFile->sName = $aFile['name'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
138
        $xFile->sFilename = $aFile['filename'];
139
        $xFile->sExtension = $aFile['extension'];
140
        $xFile->sSize = $aFile['size'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
141
        $xFile->sPath = $aFile['path'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
142
        return $xFile;
143
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
144
145
    /**
146
     * Convert the File instance to array.
147
     *
148
     * @return array<string,string>
149
     */
150
    public function toTempData(): array
151
    {
152
        return [
153
            'type' => $this->sType,
154
            'name' => $this->sName,
155
            'filename' => $this->sFilename,
156
            'extension' => $this->sExtension,
157
            'size' => $this->sSize,
158
            'path' => $this->sPath,
159
        ];
160
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
161
162
    /**
163
     * Get the uploaded file type
164
     *
165
     * @return string
166
     */
167
    public function type(): string
168
    {
169
        return $this->sType;
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
173
     * Get the uploaded file name, without the extension and slugified
174
     *
175
     * @return string
176
     */
177
    public function name(): string
178
    {
179
        return $this->sName;
180
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
181
182
    /**
183
     * Get the uploaded file name, with the extension
184
     *
185
     * @return string
186
     */
187
    public function filename(): string
188
    {
189
        return $this->sFilename;
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * Get the uploaded file path
194
     *
195
     * @return string
196
     */
197
    public function path(): string
198
    {
199
        return $this->sPath;
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * Get the uploaded file size
204
     *
205
     * @return string
206
     */
207
    public function size(): string
208
    {
209
        return $this->sSize;
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
211
212
    /**
213
     * Get the uploaded file extension
214
     *
215
     * @return string
216
     */
217
    public function extension(): string
218
    {
219
        return $this->sExtension;
220
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
221
}
222