1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Linna Http Message. |
||
5 | * |
||
6 | * @author Sebastian Rapetti <[email protected]> |
||
7 | * @copyright (c) 2019, Sebastian Rapetti |
||
8 | * @license http://opensource.org/licenses/MIT MIT License |
||
9 | */ |
||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace Linna\Http\Message; |
||
13 | |||
14 | use InvalidArgumentException; |
||
15 | use Psr\Http\Message\StreamInterface; |
||
16 | use Psr\Http\Message\UploadedFileInterface; |
||
17 | use RuntimeException; |
||
18 | |||
19 | /** |
||
20 | * PSR-7 Uploaded file implementation. |
||
21 | */ |
||
22 | class UploadedFile implements UploadedFileInterface |
||
23 | { |
||
24 | /** |
||
25 | * Retrieve a stream representing the uploaded file. |
||
26 | * |
||
27 | * This method MUST return a StreamInterface instance, representing the |
||
28 | * uploaded file. The purpose of this method is to allow utilizing native PHP |
||
29 | * stream functionality to manipulate the file upload, such as |
||
30 | * stream_copy_to_stream() (though the result will need to be decorated in a |
||
31 | * native PHP stream wrapper to work with such functions). |
||
32 | * |
||
33 | * If the moveTo() method has been called previously, this method MUST raise |
||
34 | * an exception. |
||
35 | * |
||
36 | * @return StreamInterface Stream representation of the uploaded file. |
||
37 | * |
||
38 | * @throws RuntimeException in cases when no stream is available or can becreated. |
||
39 | */ |
||
40 | public function getStream(): StreamInterface |
||
41 | { |
||
42 | } |
||
0 ignored issues
–
show
|
|||
43 | |||
44 | /** |
||
45 | * Move the uploaded file to a new location. |
||
46 | * |
||
47 | * Use this method as an alternative to move_uploaded_file(). This method is |
||
48 | * guaranteed to work in both SAPI and non-SAPI environments. |
||
49 | * Implementations must determine which environment they are in, and use the |
||
50 | * appropriate method (move_uploaded_file(), rename(), or a stream |
||
51 | * operation) to perform the operation. |
||
52 | * |
||
53 | * $targetPath may be an absolute path, or a relative path. If it is a |
||
54 | * relative path, resolution should be the same as used by PHP's rename() |
||
55 | * function. |
||
56 | * |
||
57 | * The original file or stream MUST be removed on completion. |
||
58 | * |
||
59 | * If this method is called more than once, any subsequent calls MUST raise |
||
60 | * an exception. |
||
61 | * |
||
62 | * When used in an SAPI environment where $_FILES is populated, when writing |
||
63 | * files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be |
||
64 | * used to ensure permissions and upload status are verified correctly. |
||
65 | * |
||
66 | * If you wish to move to a stream, use getStream(), as SAPI operations |
||
67 | * cannot guarantee writing to stream destinations. |
||
68 | * |
||
69 | * @see http://php.net/is_uploaded_file |
||
70 | * @see http://php.net/move_uploaded_file |
||
71 | * |
||
72 | * @param string $targetPath Path to which to move the uploaded file. |
||
73 | * |
||
74 | * @throws InvalidArgumentException if the $targetPath specified is invalid. |
||
75 | * @throws RuntimeException on any error during the move operation, or on |
||
76 | * the second or subsequent call to the method. |
||
77 | */ |
||
78 | public function moveTo(string $targetPath) |
||
79 | { |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Retrieve the file size. |
||
84 | * |
||
85 | * Implementations SHOULD return the value stored in the "size" key of |
||
86 | * the file in the $_FILES array if available, as PHP calculates this based |
||
87 | * on the actual size transmitted. |
||
88 | * |
||
89 | * @return int The file size in bytes or zero if unknown. |
||
90 | */ |
||
91 | public function getSize(): int |
||
92 | { |
||
93 | } |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return integer . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
94 | |||
95 | /** |
||
96 | * Retrieve the error associated with the uploaded file. |
||
97 | * |
||
98 | * The return value MUST be one of PHP's UPLOAD_ERR_XXX constants. |
||
99 | * |
||
100 | * If the file was uploaded successfully, this method MUST return |
||
101 | * UPLOAD_ERR_OK. |
||
102 | * |
||
103 | * Implementations SHOULD return the value stored in the "error" key of |
||
104 | * the file in the $_FILES array. |
||
105 | * |
||
106 | * @see http://php.net/manual/en/features.file-upload.errors.php |
||
107 | * |
||
108 | * @return int One of PHP's UPLOAD_ERR_XXX constants. |
||
109 | */ |
||
110 | public function getError(): int |
||
111 | { |
||
112 | } |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return integer . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
113 | |||
114 | /** |
||
115 | * Retrieve the filename sent by the client. |
||
116 | * |
||
117 | * Do not trust the value returned by this method. A client could send |
||
118 | * a malicious filename with the intention to corrupt or hack your |
||
119 | * application. |
||
120 | * |
||
121 | * Implementations SHOULD return the value stored in the "name" key of |
||
122 | * the file in the $_FILES array. |
||
123 | * |
||
124 | * @return string The filename sent by the client or void string if none |
||
125 | * was provided. |
||
126 | */ |
||
127 | public function getClientFilename(): string |
||
128 | { |
||
129 | } |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return string . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
130 | |||
131 | /** |
||
132 | * Retrieve the media type sent by the client. |
||
133 | * |
||
134 | * Do not trust the value returned by this method. A client could send |
||
135 | * a malicious media type with the intention to corrupt or hack your |
||
136 | * application. |
||
137 | * |
||
138 | * Implementations SHOULD return the value stored in the "type" key of |
||
139 | * the file in the $_FILES array. |
||
140 | * |
||
141 | * @return string The media type sent by the client or void string if none |
||
142 | * was provided. |
||
143 | */ |
||
144 | public function getClientMediaType(): string |
||
145 | { |
||
146 | } |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return string . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
147 | } |
||
148 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: