Passed
Push — main ( 2fb77f...5ec0fc )
by Thierry
01:55
created

File   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 180
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fromTempFile() 0 11 1
A fromHttpFile() 0 11 1
A type() 0 3 1
A filename() 0 3 1
A size() 0 3 1
A extension() 0 3 1
A filesystem() 0 3 1
A name() 0 3 1
A toTempData() 0 9 1
A path() 0 3 1
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;
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 UploadedFile $xHttpFile
79
     * @param string $sUploadDir
80
     * @param string $sName
81
     *
82
     * @return File
83
     */
84
    public static function fromHttpFile(Filesystem $xFilesystem, UploadedFile $xHttpFile, string $sUploadDir, string $sName): File
85
    {
86
        $xFile = new File();
87
        $xFile->xFilesystem = $xFilesystem;
88
        $xFile->sType = $xHttpFile->getClientMediaType();
89
        $xFile->sName = $sName;
90
        $xFile->sFilename = $xHttpFile->getClientFilename();
91
        $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...
92
        $xFile->nSize = $xHttpFile->getSize();
93
        $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

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