Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error while trying to get stream metadata after detach #195

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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