Passed
Push — master ( 5198fb...6eb156 )
by Thierry
02:34
created

File   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 159
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toTempData() 0 9 1
A extension() 0 3 1
A fromHttpFile() 0 10 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 function pathinfo;
18
19
class File
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class File
Loading history...
20
{
21
    /**
22
     * The uploaded file type
23
     *
24
     * @var string
25
     */
26
    protected $sType;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
27
28
    /**
29
     * The uploaded file name, without the extension and sanitized
30
     *
31
     * @var string
32
     */
33
    protected $sName;
34
35
    /**
36
     * The uploaded file name, with the extension
37
     *
38
     * @var string
39
     */
40
    protected $sFilename;
41
42
    /**
43
     * The uploaded file path
44
     *
45
     * @var string
46
     */
47
    protected $sPath;
48
49
    /**
50
     * The uploaded file size
51
     *
52
     * @var string
53
     */
54
    protected $sSize;
55
56
    /**
57
     * The uploaded file extension
58
     *
59
     * @var string
60
     */
61
    protected $sExtension;
62
63
    /**
64
     * Create an instance of this class using data from an uploaded file.
65
     *
66
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
67
     * @param string $sUploadDir    The directory where to save the uploaded file
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
68
     * @param UploadedFile $xHttpFile    The uploaded file
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
69
     *
70
     * @return File
71
     */
72
    public static function fromHttpFile(string $sName, string $sUploadDir, UploadedFile $xHttpFile): File
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
73
    {
74
        $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...
75
        $xFile->sName = $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...
76
        $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...
77
        $xFile->sFilename = $xHttpFile->getClientFilename();
78
        $xFile->sExtension = pathinfo($xFile->sFilename, PATHINFO_EXTENSION);
0 ignored issues
show
Documentation Bug introduced by
It seems like pathinfo($xFile->sFilena...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...
79
        $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...
80
        $xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension;
0 ignored issues
show
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

80
        $xFile->sPath = $sUploadDir . $xFile->sName . '.' . /** @scrutinizer ignore-type */ $xFile->sExtension;
Loading history...
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...
81
        return $xFile;
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * Create an instance of this class using data from a temp file
86
     *
87
     * @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...
88
     *
89
     * @return File
90
     */
91
    public static function fromTempFile(array $aFile): File
92
    {
93
        $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...
94
        $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...
95
        $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...
96
        $xFile->sFilename = $aFile['filename'];
97
        $xFile->sExtension = $aFile['extension'];
98
        $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...
99
        $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...
100
        return $xFile;
101
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
102
103
    /**
104
     * Convert the File instance to array.
105
     *
106
     * @return array<string,string>
107
     */
108
    public function toTempData(): array
109
    {
110
        return [
111
            'type' => $this->sType,
112
            'name' => $this->sName,
113
            'filename' => $this->sFilename,
114
            'extension' => $this->sExtension,
115
            'size' => $this->sSize,
116
            'path' => $this->sPath,
117
        ];
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Get the uploaded file type
122
     *
123
     * @return string
124
     */
125
    public function type(): string
126
    {
127
        return $this->sType;
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Get the uploaded file name, without the extension and slugified
132
     *
133
     * @return string
134
     */
135
    public function name(): string
136
    {
137
        return $this->sName;
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
139
140
    /**
141
     * Get the uploaded file name, with the extension
142
     *
143
     * @return string
144
     */
145
    public function filename(): string
146
    {
147
        return $this->sFilename;
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Get the uploaded file path
152
     *
153
     * @return string
154
     */
155
    public function path(): string
156
    {
157
        return $this->sPath;
158
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
159
160
    /**
161
     * Get the uploaded file size
162
     *
163
     * @return string
164
     */
165
    public function size(): string
166
    {
167
        return $this->sSize;
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
171
     * Get the uploaded file extension
172
     *
173
     * @return string
174
     */
175
    public function extension(): string
176
    {
177
        return $this->sExtension;
178
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
179
}
180