1 | <?php |
||||
2 | /** |
||||
3 | * This file is part of the O2System Framework package. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Steeve Andrian Salim |
||||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
10 | */ |
||||
11 | |||||
12 | // ------------------------------------------------------------------------ |
||||
13 | |||||
14 | namespace O2System\Kernel\Http\Message; |
||||
15 | |||||
16 | // ------------------------------------------------------------------------ |
||||
17 | |||||
18 | use O2System\Psr\Http\Message\StreamInterface; |
||||
19 | |||||
20 | /** |
||||
21 | * Class Stream |
||||
22 | * |
||||
23 | * @package O2System\Curl\Message |
||||
24 | */ |
||||
25 | class Stream implements StreamInterface |
||||
26 | { |
||||
27 | /** |
||||
28 | * Stream::$context |
||||
29 | * |
||||
30 | * Stream Resource |
||||
31 | * |
||||
32 | * @var resource |
||||
33 | */ |
||||
34 | public $context; |
||||
35 | |||||
36 | // ------------------------------------------------------------------------ |
||||
37 | |||||
38 | /** |
||||
39 | * Stream::__construct |
||||
40 | */ |
||||
41 | public function __construct() |
||||
42 | { |
||||
43 | $this->context = fopen('php://memory', 'r+'); |
||||
0 ignored issues
–
show
|
|||||
44 | } |
||||
45 | |||||
46 | // ------------------------------------------------------------------------ |
||||
47 | |||||
48 | /** |
||||
49 | * Stream::__toString |
||||
50 | * |
||||
51 | * Reads all data from the stream into a string, from the beginning to end. |
||||
52 | * |
||||
53 | * This method MUST attempt to seek to the beginning of the stream before |
||||
54 | * reading data and read the stream until the end is reached. |
||||
55 | * |
||||
56 | * Warning: This could attempt to load a large amount of data into memory. |
||||
57 | * |
||||
58 | * This method MUST NOT raise an exception in order to conform with PHP's |
||||
59 | * string casting operations. |
||||
60 | * |
||||
61 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||||
62 | * @return string |
||||
63 | */ |
||||
64 | public function __toString() |
||||
65 | { |
||||
66 | $this->seek(0); |
||||
67 | |||||
68 | return (string)stream_get_contents($this->context); |
||||
69 | } |
||||
70 | |||||
71 | // ------------------------------------------------------------------------ |
||||
72 | |||||
73 | /** |
||||
74 | * Stream::seek |
||||
75 | * |
||||
76 | * Seek to a position in the stream. |
||||
77 | * |
||||
78 | * @see http://www.php.net/manual/en/function.fseek.php |
||||
79 | * |
||||
80 | * @param int $offset Stream offset |
||||
81 | * @param int $whence Specifies how the cursor position will be calculated |
||||
82 | * based on the seek offset. Valid values are identical to the built-in |
||||
83 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to |
||||
84 | * offset bytes SEEK_CUR: Set position to current location plus offset |
||||
85 | * SEEK_END: Set position to end-of-stream plus offset. |
||||
86 | * |
||||
87 | * @throws \RuntimeException on failure. |
||||
88 | */ |
||||
89 | public function seek($offset, $whence = SEEK_SET) |
||||
90 | { |
||||
91 | if (fseek($this->context, $offset, $whence) == -1) { |
||||
92 | throw new \RuntimeException('Not seekable'); |
||||
93 | } |
||||
94 | } |
||||
95 | |||||
96 | // ------------------------------------------------------------------------ |
||||
97 | |||||
98 | /** |
||||
99 | * Stream::close |
||||
100 | * |
||||
101 | * Closes the stream and any underlying resources. |
||||
102 | * |
||||
103 | * @return void |
||||
104 | */ |
||||
105 | public function close() |
||||
106 | { |
||||
107 | if (is_resource($this->context)) { |
||||
108 | fclose($this->context); |
||||
109 | } |
||||
110 | |||||
111 | $this->detach(); |
||||
112 | } |
||||
113 | |||||
114 | // ------------------------------------------------------------------------ |
||||
115 | |||||
116 | /** |
||||
117 | * Stream::detach |
||||
118 | * |
||||
119 | * Separates any underlying resources from the stream. |
||||
120 | * |
||||
121 | * After the stream has been detached, the stream is in an unusable state. |
||||
122 | * |
||||
123 | * @return resource|null Underlying PHP stream, if any |
||||
124 | */ |
||||
125 | public function detach() |
||||
126 | { |
||||
127 | $result = clone $this->context; |
||||
128 | $this->context = null; |
||||
129 | |||||
130 | return $result; |
||||
0 ignored issues
–
show
|
|||||
131 | } |
||||
132 | |||||
133 | // ------------------------------------------------------------------------ |
||||
134 | |||||
135 | /** |
||||
136 | * Stream::getSize |
||||
137 | * |
||||
138 | * Get the size of the stream if known. |
||||
139 | * |
||||
140 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||||
141 | */ |
||||
142 | public function getSize() |
||||
143 | { |
||||
144 | $stats = fstat($this->context); |
||||
145 | |||||
146 | if (isset($stats[ 'size' ])) { |
||||
147 | return (int)$stats[ 'size' ]; |
||||
148 | } |
||||
149 | |||||
150 | return null; |
||||
151 | } |
||||
152 | |||||
153 | // ------------------------------------------------------------------------ |
||||
154 | |||||
155 | /** |
||||
156 | * Stream::tell |
||||
157 | * |
||||
158 | * Returns the current position of the file read/write pointer |
||||
159 | * |
||||
160 | * @return int Position of the file pointer |
||||
161 | * @throws \RuntimeException on error. |
||||
162 | */ |
||||
163 | public function tell() |
||||
164 | { |
||||
165 | if (false !== ($position = ftell($this->context))) { |
||||
166 | return $position; |
||||
167 | } |
||||
168 | |||||
169 | throw new \RuntimeException('cannot find pointer'); |
||||
170 | } |
||||
171 | |||||
172 | // ------------------------------------------------------------------------ |
||||
173 | |||||
174 | /** |
||||
175 | * Stream::eof |
||||
176 | * |
||||
177 | * Returns true if the stream is at the end of the stream. |
||||
178 | * |
||||
179 | * @return bool |
||||
180 | */ |
||||
181 | public function eof() |
||||
182 | { |
||||
183 | return feof($this->context); |
||||
184 | } |
||||
185 | |||||
186 | // ------------------------------------------------------------------------ |
||||
187 | |||||
188 | /** |
||||
189 | * Stream::isSeekable |
||||
190 | * |
||||
191 | * Returns whether or not the stream is seekable. |
||||
192 | * |
||||
193 | * @return bool |
||||
194 | */ |
||||
195 | public function isSeekable() |
||||
196 | { |
||||
197 | return (bool)$this->getMetadata('seekable'); |
||||
198 | } |
||||
199 | |||||
200 | // ------------------------------------------------------------------------ |
||||
201 | |||||
202 | /** |
||||
203 | * Stream::getMetadata |
||||
204 | * |
||||
205 | * Get stream metadata as an associative array or retrieve a specific key. |
||||
206 | * |
||||
207 | * The keys returned are identical to the keys returned from PHP's |
||||
208 | * stream_get_meta_data() function. |
||||
209 | * |
||||
210 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||||
211 | * |
||||
212 | * @param string $key Specific metadata to retrieve. |
||||
213 | * |
||||
214 | * @return array|mixed|null Returns an associative array if no key is |
||||
215 | * provided. Returns a specific key value if a key is provided and the |
||||
216 | * value is found, or null if the key is not found. |
||||
217 | */ |
||||
218 | public function getMetadata($key = null) |
||||
219 | { |
||||
220 | $metadata = stream_get_meta_data($this->context); |
||||
221 | |||||
222 | if (empty($key)) { |
||||
223 | return $metadata; |
||||
224 | } elseif (isset($metadata[ $key ])) { |
||||
225 | return $metadata[ $key ]; |
||||
226 | } |
||||
227 | |||||
228 | return null; |
||||
229 | } |
||||
230 | |||||
231 | // ------------------------------------------------------------------------ |
||||
232 | |||||
233 | /** |
||||
234 | * Stream::rewind |
||||
235 | * |
||||
236 | * Seek to the beginning of the stream. |
||||
237 | * |
||||
238 | * If the stream is not seekable, this method will raise an exception; |
||||
239 | * otherwise, it will perform a seek(0). |
||||
240 | * |
||||
241 | * @see seek() |
||||
242 | * @see http://www.php.net/manual/en/function.fseek.php |
||||
243 | * @throws \RuntimeException on failure. |
||||
244 | */ |
||||
245 | public function rewind() |
||||
246 | { |
||||
247 | if (rewind($this->context) === false) { |
||||
248 | throw new \RuntimeException('Cannot rewind stream'); |
||||
249 | } |
||||
250 | } |
||||
251 | |||||
252 | // ------------------------------------------------------------------------ |
||||
253 | |||||
254 | /** |
||||
255 | * Stream::isWritable |
||||
256 | * |
||||
257 | * Returns whether or not the stream is writable. |
||||
258 | * |
||||
259 | * @return bool |
||||
260 | */ |
||||
261 | public function isWritable() |
||||
262 | { |
||||
263 | if (null !== ($uri = $this->getMetadata('uri'))) { |
||||
264 | if (is_writable($uri)) { |
||||
0 ignored issues
–
show
It seems like
$uri can also be of type array ; however, parameter $filename of is_writable() 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
![]() |
|||||
265 | return true; |
||||
266 | } elseif (null !== ($mode = $this->getMetadata('mode'))) { |
||||
267 | preg_match_all('/[a-z]\D?/', $mode, $matches); |
||||
268 | |||||
269 | if (isset($matches[ 0 ])) { |
||||
270 | foreach ($matches[ 0 ] as $match) { |
||||
271 | if (in_array($match, ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'])) { |
||||
272 | return true; |
||||
273 | |||||
274 | break; |
||||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other ![]() |
|||||
275 | } |
||||
276 | } |
||||
277 | } |
||||
278 | } |
||||
279 | } |
||||
280 | |||||
281 | return false; |
||||
282 | } |
||||
283 | |||||
284 | // ------------------------------------------------------------------------ |
||||
285 | |||||
286 | /** |
||||
287 | * Stream::write |
||||
288 | * |
||||
289 | * Write data to the stream. |
||||
290 | * |
||||
291 | * @param string $string The string that is to be written. |
||||
292 | * |
||||
293 | * @return int Returns the number of bytes written to the stream. |
||||
294 | * @throws \RuntimeException on failure. |
||||
295 | */ |
||||
296 | public function write($string) |
||||
297 | { |
||||
298 | return fwrite($this->context, $string); |
||||
299 | } |
||||
300 | |||||
301 | // ------------------------------------------------------------------------ |
||||
302 | |||||
303 | /** |
||||
304 | * Stream::isReadable |
||||
305 | * |
||||
306 | * Returns whether or not the stream is readable. |
||||
307 | * |
||||
308 | * @return bool |
||||
309 | */ |
||||
310 | public function isReadable() |
||||
311 | { |
||||
312 | if (null !== ($mode = $this->getMetadata('mode'))) { |
||||
313 | preg_match_all('/[a-z]\D?/', $mode, $matches); |
||||
314 | |||||
315 | if (isset($matches[ 0 ])) { |
||||
316 | foreach ($matches[ 0 ] as $match) { |
||||
317 | if (in_array($match, ['r', 'r+', 'w+', 'a+', 'x+', 'c+'])) { |
||||
318 | return true; |
||||
319 | break; |
||||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other ![]() |
|||||
320 | } |
||||
321 | } |
||||
322 | } |
||||
323 | } |
||||
324 | |||||
325 | return false; |
||||
326 | } |
||||
327 | |||||
328 | // ------------------------------------------------------------------------ |
||||
329 | |||||
330 | /** |
||||
331 | * Stream::read |
||||
332 | * |
||||
333 | * Read data from the stream. |
||||
334 | * |
||||
335 | * @param int $length Read up to $length bytes from the object and return |
||||
336 | * them. Fewer than $length bytes may be returned if underlying stream |
||||
337 | * call returns fewer bytes. |
||||
338 | * |
||||
339 | * @return string Returns the data read from the stream, or an empty string |
||||
340 | * if no bytes are available. |
||||
341 | * @throws \RuntimeException if an error occurs. |
||||
342 | */ |
||||
343 | public function read($length) |
||||
344 | { |
||||
345 | if (null !== ($data = fread($this->context, $length))) { |
||||
346 | return $data; |
||||
347 | } |
||||
348 | |||||
349 | throw new \RuntimeException('Cannot read'); |
||||
350 | } |
||||
351 | |||||
352 | // ------------------------------------------------------------------------ |
||||
353 | |||||
354 | /** |
||||
355 | * Stream::getContents |
||||
356 | * |
||||
357 | * Returns the remaining contents in a string |
||||
358 | * |
||||
359 | * @return string |
||||
360 | * @throws \RuntimeException if unable to read. |
||||
361 | * @throws \RuntimeException if error occurs while reading. |
||||
362 | */ |
||||
363 | public function getContents() |
||||
364 | { |
||||
365 | return $this->context ? stream_get_contents($this->context) : ''; |
||||
366 | } |
||||
367 | } |
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 theid
property of an instance of theAccount
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.