diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 791d7e1e..5fd17f72 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -214,8 +214,6 @@ resource]]> - resource]]> - resource]]> diff --git a/src/Stream.php b/src/Stream.php index f5cecbb5..d21fea3a 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -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; @@ -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, '+'); } /** @@ -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, '+'); } /** @@ -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; } diff --git a/test/StreamTest.php b/test/StreamTest.php index e7efe2ae..ab73feb8 100644 --- a/test/StreamTest.php +++ b/test/StreamTest.php @@ -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');