Skip to content

Commit

Permalink
Merge pull request #195 from Xerkus/fix/detached-metadata
Browse files Browse the repository at this point in the history
Fix error while trying to get stream metadata after detach
  • Loading branch information
Xerkus committed Sep 11, 2024
2 parents 86a20b6 + 969d169 commit 2cce7e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 0 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@
<PossiblyNullArgument>
<code><![CDATA[$resource]]></code>
<code><![CDATA[$this->resource]]></code>
<code><![CDATA[$this->resource]]></code>
<code><![CDATA[$this->resource]]></code>
</PossiblyNullArgument>
<PossiblyUnusedProperty>
<code><![CDATA[$stream]]></code>
Expand Down
22 changes: 13 additions & 9 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
use function is_resource;
use function is_string;
use function sprintf;
use function str_contains;
use function stream_get_contents;
use function stream_get_meta_data;
use function strstr;

use const SEEK_SET;

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

return strstr($mode, 'x') !== false
|| strstr($mode, 'w') !== false
|| strstr($mode, 'c') !== false
|| strstr($mode, 'a') !== false
|| strstr($mode, '+') !== false;
return str_contains($mode, 'x')
|| str_contains($mode, 'w')
|| str_contains($mode, 'c')
|| str_contains($mode, 'a')
|| str_contains($mode, '+');
}

/**
Expand Down Expand Up @@ -251,7 +251,7 @@ public function isReadable(): bool
$meta = stream_get_meta_data($this->resource);
$mode = $meta['mode'];

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

/**
Expand Down Expand Up @@ -297,11 +297,15 @@ public function getContents(): string
*/
public function getMetadata(?string $key = null)
{
$metadata = [];
if (null !== $this->resource) {
$metadata = stream_get_meta_data($this->resource);
}

if (null === $key) {
return stream_get_meta_data($this->resource);
return $metadata;
}

$metadata = stream_get_meta_data($this->resource);
if (! array_key_exists($key, $metadata)) {
return null;
}
Expand Down
10 changes: 10 additions & 0 deletions test/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,16 @@ public function testGetMetadataReturnsAllMetadataWhenNoKeyPresent(): void
$this->assertSame($expected, $test);
}

public function testGetMetadataReturnsEmptyArrayAfterDetach(): void
{
self::assertNotEmpty($this->stream->getMetadata());
self::assertNotEmpty($this->stream->getMetadata('mode'));

$this->stream->detach();
self::assertSame([], $this->stream->getMetadata());
self::assertNull($this->stream->getMetadata('mode'));
}

public function testGetMetadataReturnsDataForSpecifiedKey(): void
{
$this->tmpnam = tempnam(sys_get_temp_dir(), 'diac');
Expand Down

0 comments on commit 2cce7e7

Please sign in to comment.