Skip to content

Commit

Permalink
Add checking on resource type
Browse files Browse the repository at this point in the history
  • Loading branch information
devanych committed Nov 16, 2023
1 parent f3a836f commit 7b95be7
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/StreamTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ public function __toString(): string
* Closes the stream and any underlying resources.
*
* @return void
* @psalm-suppress PossiblyNullArgument
*/
public function close(): void
{
if ($this->resource) {
$resource = $this->detach();
fclose($resource);

if (is_resource($resource)) {
fclose($resource);
}
}
}

Expand Down Expand Up @@ -364,16 +366,11 @@ public function getContents(): string
*/
public function getMetadata($key = null)
{
if (!$this->resource) {
if (!is_resource($this->resource)) {
return $key ? null : [];
}

try {
$metadata = stream_get_meta_data($this->resource);
} catch (Throwable $e) {
$this->detach();
return $key ? null : [];
}
$metadata = stream_get_meta_data($this->resource);

if ($key === null) {
return $metadata;
Expand Down

0 comments on commit 7b95be7

Please sign in to comment.