Skip to content

Commit 2cce7e7

Browse files
authored
Merge pull request #195 from Xerkus/fix/detached-metadata
Fix error while trying to get stream metadata after detach
2 parents 86a20b6 + 969d169 commit 2cce7e7

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@
214214
<PossiblyNullArgument>
215215
<code><![CDATA[$resource]]></code>
216216
<code><![CDATA[$this->resource]]></code>
217-
<code><![CDATA[$this->resource]]></code>
218-
<code><![CDATA[$this->resource]]></code>
219217
</PossiblyNullArgument>
220218
<PossiblyUnusedProperty>
221219
<code><![CDATA[$stream]]></code>

src/Stream.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
use function is_resource;
2525
use function is_string;
2626
use function sprintf;
27+
use function str_contains;
2728
use function stream_get_contents;
2829
use function stream_get_meta_data;
29-
use function strstr;
3030

3131
use const SEEK_SET;
3232

@@ -210,11 +210,11 @@ public function isWritable(): bool
210210
$meta = stream_get_meta_data($this->resource);
211211
$mode = $meta['mode'];
212212

213-
return strstr($mode, 'x') !== false
214-
|| strstr($mode, 'w') !== false
215-
|| strstr($mode, 'c') !== false
216-
|| strstr($mode, 'a') !== false
217-
|| strstr($mode, '+') !== false;
213+
return str_contains($mode, 'x')
214+
|| str_contains($mode, 'w')
215+
|| str_contains($mode, 'c')
216+
|| str_contains($mode, 'a')
217+
|| str_contains($mode, '+');
218218
}
219219

220220
/**
@@ -251,7 +251,7 @@ public function isReadable(): bool
251251
$meta = stream_get_meta_data($this->resource);
252252
$mode = $meta['mode'];
253253

254-
return strstr($mode, 'r') !== false || strstr($mode, '+') !== false;
254+
return str_contains($mode, 'r') || str_contains($mode, '+');
255255
}
256256

257257
/**
@@ -297,11 +297,15 @@ public function getContents(): string
297297
*/
298298
public function getMetadata(?string $key = null)
299299
{
300+
$metadata = [];
301+
if (null !== $this->resource) {
302+
$metadata = stream_get_meta_data($this->resource);
303+
}
304+
300305
if (null === $key) {
301-
return stream_get_meta_data($this->resource);
306+
return $metadata;
302307
}
303308

304-
$metadata = stream_get_meta_data($this->resource);
305309
if (! array_key_exists($key, $metadata)) {
306310
return null;
307311
}

test/StreamTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,16 @@ public function testGetMetadataReturnsAllMetadataWhenNoKeyPresent(): void
596596
$this->assertSame($expected, $test);
597597
}
598598

599+
public function testGetMetadataReturnsEmptyArrayAfterDetach(): void
600+
{
601+
self::assertNotEmpty($this->stream->getMetadata());
602+
self::assertNotEmpty($this->stream->getMetadata('mode'));
603+
604+
$this->stream->detach();
605+
self::assertSame([], $this->stream->getMetadata());
606+
self::assertNull($this->stream->getMetadata('mode'));
607+
}
608+
599609
public function testGetMetadataReturnsDataForSpecifiedKey(): void
600610
{
601611
$this->tmpnam = tempnam(sys_get_temp_dir(), 'diac');

0 commit comments

Comments
 (0)