Skip to content

Commit b98bbf5

Browse files
committed
:octocat: stream mode constant cleanup
1 parent 19a8df0 commit b98bbf5

File tree

3 files changed

+11
-35
lines changed

3 files changed

+11
-35
lines changed

src/Psr17/StreamFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Psr\Http\Message\{StreamFactoryInterface, StreamInterface};
1515
use InvalidArgumentException, RuntimeException;
1616

17-
use function fopen, is_file;
17+
use function fopen, in_array, is_file;
1818

1919
final class StreamFactory implements StreamFactoryInterface{
2020

@@ -40,7 +40,7 @@ public function createStreamFromFile(string $filename, string $mode = 'r'):Strea
4040
throw new RuntimeException('invalid file');
4141
}
4242

43-
if(!isset(STREAM_MODES_WRITE[$mode]) && !isset(STREAM_MODES_READ[$mode])){
43+
if(!in_array($mode, STREAM_MODES_WRITE) && !in_array($mode, STREAM_MODES_READ)){
4444
throw new InvalidArgumentException('invalid mode');
4545
}
4646

src/Psr17/factory_helpers.php

+5-30
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,13 @@
1212
use chillerlan\HTTP\Psr7\Stream;
1313
use InvalidArgumentException;
1414

15-
use function is_scalar, method_exists;
15+
use function in_array, is_scalar, method_exists;
1616

1717
const CHILLERLAN_PSR17_INCLUDES = true;
1818

19-
const STREAM_MODES_READ_WRITE = [
20-
'a+' => true,
21-
'c+' => true,
22-
'c+b' => true,
23-
'c+t' => true,
24-
'r+' => true,
25-
'r+b' => true,
26-
'r+t' => true,
27-
'w+' => true,
28-
'w+b' => true,
29-
'w+t' => true,
30-
'x+' => true,
31-
'x+b' => true,
32-
'x+t' => true,
33-
];
34-
35-
const STREAM_MODES_READ = STREAM_MODES_READ_WRITE + [
36-
'r' => true,
37-
'rb' => true,
38-
'rt' => true,
39-
];
40-
41-
const STREAM_MODES_WRITE = STREAM_MODES_READ_WRITE + [
42-
'a' => true,
43-
'rw' => true,
44-
'w' => true,
45-
'wb' => true,
46-
];
19+
const STREAM_MODES_READ_WRITE = ['a+', 'c+', 'c+b', 'c+t', 'r+' , 'r+b', 'r+t', 'w+' , 'w+b', 'w+t', 'x+' , 'x+b', 'x+t'];
20+
const STREAM_MODES_READ = [...STREAM_MODES_READ_WRITE, 'r', 'rb', 'rt'];
21+
const STREAM_MODES_WRITE = [...STREAM_MODES_READ_WRITE, 'a', 'rw', 'w', 'wb'];
4722

4823
/**
4924
* Create a new writable stream from a string.
@@ -58,7 +33,7 @@
5833
*/
5934
function create_stream(string $content = '', string $mode = 'r+', bool $rewind = true):StreamInterface{
6035

61-
if(!isset(STREAM_MODES_WRITE[$mode])){
36+
if(!in_array($mode, STREAM_MODES_WRITE)){
6237
throw new InvalidArgumentException('invalid mode');
6338
}
6439

src/Psr7/Stream.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
use Exception, InvalidArgumentException, RuntimeException;
1414

15-
use function clearstatcache, fclose, feof, fread, fstat, ftell, fwrite, is_resource, stream_get_contents, stream_get_meta_data;
15+
use function clearstatcache, fclose, feof, fread, fstat, ftell, fwrite, in_array,
16+
is_resource, stream_get_contents, stream_get_meta_data;
1617

1718
use const SEEK_SET;
1819
use const chillerlan\HTTP\Psr17\{STREAM_MODES_READ, STREAM_MODES_WRITE};
@@ -48,8 +49,8 @@ public function __construct($stream){
4849
$meta = stream_get_meta_data($this->stream);
4950

5051
$this->seekable = $meta['seekable'];
51-
$this->readable = isset(STREAM_MODES_READ[$meta['mode']]);
52-
$this->writable = isset(STREAM_MODES_WRITE[$meta['mode']]);
52+
$this->readable = in_array($meta['mode'], STREAM_MODES_READ);
53+
$this->writable = in_array($meta['mode'], STREAM_MODES_WRITE);
5354
$this->uri = $meta['uri'] ?? null;
5455
}
5556

0 commit comments

Comments
 (0)