Passed
Push — main ( 1a70e4...e654d0 )
by Thierry
01:53
created

File::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * File.php
5
 *
6
 * An uploaded file.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2017 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Upload\Manager;
16
17
use Jaxon\Request\Upload\FileInterface;
0 ignored issues
show
Bug introduced by
The type Jaxon\Request\Upload\FileInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use League\Flysystem\Filesystem;
19
use Nyholm\Psr7\UploadedFile;
20
21
use function pathinfo;
22
23
class File implements FileInterface
24
{
25
    /**
26
     * The uploaded file type
27
     *
28
     * @var string
29
     */
30
    protected $sType;
31
32
    /**
33
     * The uploaded file name, without the extension and sanitized
34
     *
35
     * @var string
36
     */
37
    protected $sName;
38
39
    /**
40
     * The uploaded file name, with the extension
41
     *
42
     * @var string
43
     */
44
    protected $sFilename;
45
46
    /**
47
     * The uploaded file path
48
     *
49
     * @var string
50
     */
51
    protected $sPath;
52
53
    /**
54
     * The uploaded file size
55
     *
56
     * @var int
57
     */
58
    protected $nSize;
59
60
    /**
61
     * The uploaded file extension
62
     *
63
     * @var string
64
     */
65
    protected $sExtension;
66
67
    /**
68
     * The filesystem where the file is stored
69
     *
70
     * @var Filesystem
71
     */
72
    protected $xFilesystem;
73
74
    /**
75
     * Create an instance of this class using data from an uploaded file.
76
     *
77
     * @param Filesystem $xFilesystem
78
     * @param string $sUploadDir
79
     * @param UploadedFile $xHttpFile
80
     *
81
     * @return File
82
     */
83
    public static function fromHttpFile(Filesystem $xFilesystem, string $sUploadDir, UploadedFile $xHttpFile): File
84
    {
85
        $xFile = new File();
86
        $xFile->xFilesystem = $xFilesystem;
87
        $xFile->sType = $xHttpFile->getClientMediaType();
88
        $xFile->sFilename = $xHttpFile->getClientFilename();
89
        $xFile->sExtension = pathinfo($xFile->sFilename, PATHINFO_EXTENSION);
0 ignored issues
show
Documentation Bug introduced by
It seems like pathinfo($xFile->sFilena...ger\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...
90
        $xFile->nSize = $xHttpFile->getSize();
91
        $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

91
        $xFile->sPath = $sUploadDir . $xFile->sName . '.' . /** @scrutinizer ignore-type */ $xFile->sExtension;
Loading history...
92
        return $xFile;
93
    }
94
95
    /**
96
     * Create an instance of this class using data from a temp file
97
     *
98
     * @param Filesystem $xFilesystem
99
     * @param array $aFile    The uploaded file data
100
     *
101
     * @return File
102
     */
103
    public static function fromTempFile(Filesystem $xFilesystem, array $aFile): File
104
    {
105
        $xFile = new File();
106
        $xFile->xFilesystem = $xFilesystem;
107
        $xFile->sType = $aFile['type'];
108
        $xFile->sName = $aFile['name'];
109
        $xFile->sFilename = $aFile['filename'];
110
        $xFile->sExtension = $aFile['extension'];
111
        $xFile->nSize = $aFile['size'];
112
        $xFile->sPath = $aFile['path'];
113
        return $xFile;
114
    }
115
116
    /**
117
     * Convert the File instance to array.
118
     *
119
     * @return array<string,string>
120
     */
121
    public function toTempData(): array
122
    {
123
        return [
124
            'type' => $this->sType,
125
            'name' => $this->sName,
126
            'filename' => $this->sFilename,
127
            'extension' => $this->sExtension,
128
            'size' => $this->nSize,
129
            'path' => $this->sPath,
130
        ];
131
    }
132
133
    /**
134
     * @param string $sName
135
     */
136
    public function setName(string $sName): void
137
    {
138
        $this->sName = $sName;
139
    }
140
141
    /**
142
     * Get the filesystem where the file is stored
143
     *
144
     * @return Filesystem
145
     */
146
    public function filesystem(): Filesystem
147
    {
148
        return $this->xFilesystem;
149
    }
150
151
    /**
152
     * Get the uploaded file type
153
     *
154
     * @return string
155
     */
156
    public function type(): string
157
    {
158
        return $this->sType;
159
    }
160
161
    /**
162
     * Get the uploaded file name, without the extension and slugified
163
     *
164
     * @return string
165
     */
166
    public function name(): string
167
    {
168
        return $this->sName;
169
    }
170
171
    /**
172
     * Get the uploaded file name, with the extension
173
     *
174
     * @return string
175
     */
176
    public function filename(): string
177
    {
178
        return $this->sFilename;
179
    }
180
181
    /**
182
     * Get the uploaded file path
183
     *
184
     * @return string
185
     */
186
    public function path(): string
187
    {
188
        return $this->sPath;
189
    }
190
191
    /**
192
     * Get the uploaded file size
193
     *
194
     * @return int
195
     */
196
    public function size(): int
197
    {
198
        return $this->nSize;
199
    }
200
201
    /**
202
     * Get the uploaded file extension
203
     *
204
     * @return string
205
     */
206
    public function extension(): string
207
    {
208
        return $this->sExtension;
209
    }
210
}
211