Skip to content

Commit

Permalink
Added docs for Utils::tryGetContents
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Jun 9, 2022
1 parent 3d12ad5 commit 0d6725b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,17 @@ When fopen fails, PHP normally raises a warning. This function adds an
error handler that checks for errors and throws an exception instead.


## `GuzzleHttp\Psr7\Utils::tryGetContents`

`public static function tryGetContents(resource $stream): string`

Safely gets the contents of a given stream.

When stream_get_contents fails, PHP normally raises a warning. This
function adds an error handler that checks for errors and throws an
exception instead.


## `GuzzleHttp\Psr7\Utils::uriFor`

`public static function uriFor(string|UriInterface $uri): UriInterface`
Expand Down
2 changes: 1 addition & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getContents(): string
throw new \RuntimeException('Cannot read from non-readable stream');
}

return Utils::tryStreamGetContents($this->stream);
return Utils::tryGetContents($this->stream);
}

public function close(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public static function tryFopen(string $filename, string $mode)
*
* @throws \RuntimeException if the stream cannot be read
*/
public static function tryStreamGetContents($stream): string
public static function tryGetContents($stream): string
{
$ex = null;
set_error_handler(static function (int $errno, string $errstr) use (&$ex): bool {
Expand Down
4 changes: 2 additions & 2 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function testGetsContentsThrowExceptionWhenNotReadable(): void
$this->expectExceptionMessage('Unable to read stream contents');

try {
Psr7\Utils::tryStreamGetContents($r);
Psr7\Utils::tryGetContents($r);
} finally {
fclose($r);
}
Expand All @@ -226,7 +226,7 @@ public function testGetsContentsThrowExceptionWhenCLosed(): void
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to read stream contents');

Psr7\Utils::tryStreamGetContents($r);
Psr7\Utils::tryGetContents($r);
}

public function testCreatesUriForValue(): void
Expand Down

0 comments on commit 0d6725b

Please sign in to comment.