Skip to content

Commit

Permalink
Fix stream metadata after detach
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksei Khudiakov <[email protected]>
  • Loading branch information
Xerkus committed Sep 10, 2024
1 parent 86a20b6 commit b270b28
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
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 b270b28

Please sign in to comment.