Skip to content

Commit a1882eb

Browse files
authored
Fix all psalm errors (#171)
1 parent 7f83483 commit a1882eb

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/Bigint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function getLowFF(bool $force = false): float
108108
/**
109109
* Check if is over 32
110110
*
111+
* @psalm-suppress ArgumentTypeCoercion
111112
* @param bool $force
112113
* @return bool
113114
*/

src/File.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace ZipStream;
55

66
use Psr\Http\Message\StreamInterface;
7+
use RuntimeException;
78
use ZipStream\Exception\EncodingException;
89
use ZipStream\Exception\FileNotFoundException;
910
use ZipStream\Exception\FileNotReadableException;
@@ -75,8 +76,9 @@ class File
7576
* @var resource
7677
*/
7778
private $deflate;
79+
7880
/**
79-
* @var resource
81+
* @var \HashContext
8082
*/
8183
private $hash;
8284

@@ -366,7 +368,11 @@ protected function readStream(StreamInterface $stream, ?int $options = null): vo
366368

367369
protected function deflateInit(): void
368370
{
369-
$this->hash = hash_init(self::HASH_ALGORITHM);
371+
$hash = hash_init(self::HASH_ALGORITHM);
372+
if ($hash === false) {
373+
throw new RuntimeException('Could not initialize hashing context!');
374+
}
375+
$this->hash = $hash;
370376
if ($this->method->equals(Method::DEFLATE())) {
371377
$this->deflate = deflate_init(
372378
ZLIB_ENCODING_RAW,

src/Stream.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function __toString(): string
6767
{
6868
try {
6969
$this->seek(0);
70-
} catch (\RuntimeException $e) {}
70+
} catch (RuntimeException $e) {}
7171
return (string) stream_get_contents($this->stream);
7272
}
7373

@@ -81,7 +81,7 @@ public function __toString(): string
8181
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
8282
* offset bytes SEEK_CUR: Set position to current location plus offset
8383
* SEEK_END: Set position to end-of-stream plus offset.
84-
* @throws \RuntimeException on failure.
84+
* @throws RuntimeException on failure.
8585
*/
8686
public function seek($offset, $whence = SEEK_SET): void
8787
{
@@ -136,7 +136,7 @@ public function getSize(): ?int
136136
* Returns the current position of the file read/write pointer
137137
*
138138
* @return int Position of the file pointer
139-
* @throws \RuntimeException on error.
139+
* @throws RuntimeException on error.
140140
*/
141141
public function tell(): int
142142
{
@@ -165,7 +165,7 @@ public function eof(): bool
165165
*
166166
* @see seek()
167167
* @link http://www.php.net/manual/en/function.fseek.php
168-
* @throws \RuntimeException on failure.
168+
* @throws RuntimeException on failure.
169169
*/
170170
public function rewind(): void
171171
{
@@ -177,7 +177,7 @@ public function rewind(): void
177177
*
178178
* @param string $string The string that is to be written.
179179
* @return int Returns the number of bytes written to the stream.
180-
* @throws \RuntimeException on failure.
180+
* @throws RuntimeException on failure.
181181
*/
182182
public function write($string): int
183183
{
@@ -197,7 +197,11 @@ public function write($string): int
197197
*/
198198
public function isWritable(): bool
199199
{
200-
return preg_match('/[waxc+]/', $this->getMetadata('mode')) === 1;
200+
$mode = $this->getMetadata('mode');
201+
if (!is_string($mode)) {
202+
throw new RuntimeException('Could not get stream mode from metadata!');
203+
}
204+
return preg_match('/[waxc+]/', $mode) === 1;
201205
}
202206

203207
/**
@@ -229,7 +233,11 @@ public function read($length): string
229233
*/
230234
public function isReadable(): bool
231235
{
232-
return preg_match('/[r+]/', $this->getMetadata('mode')) === 1;
236+
$mode = $this->getMetadata('mode');
237+
if (!is_string($mode)) {
238+
throw new RuntimeException('Could not get stream mode from metadata!');
239+
}
240+
return preg_match('/[r+]/', $mode) === 1;
233241
}
234242

235243
/**

0 commit comments

Comments
 (0)