Skip to content

Commit

Permalink
Merge 3a0a6eb into ee13c72
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Dec 12, 2021
2 parents ee13c72 + 3a0a6eb commit 6c0326d
Show file tree
Hide file tree
Showing 30 changed files with 92 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/Psl/File/ReadHandle.php
Expand Up @@ -32,9 +32,9 @@ public function __construct(string $path)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->readHandle->readImmediately($max_bytes);
return $this->readHandle->tryRead($max_bytes);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Psl/File/ReadWriteHandle.php
Expand Up @@ -57,9 +57,9 @@ public function __construct(string $path, WriteMode $write_mode = WriteMode::OPE
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->readWriteHandle->readImmediately($max_bytes);
return $this->readWriteHandle->tryRead($max_bytes);
}

/**
Expand All @@ -73,9 +73,9 @@ public function read(?int $max_bytes = null, ?float $timeout = null): string
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->readWriteHandle->writeImmediately($bytes);
return $this->readWriteHandle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/File/WriteHandle.php
Expand Up @@ -52,9 +52,9 @@ public function __construct(string $path, WriteMode $write_mode = WriteMode::OPE
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->writeHandle->writeImmediately($bytes);
return $this->writeHandle->tryWrite($bytes);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Psl/IO/Internal/ResourceHandle.php
Expand Up @@ -135,7 +135,7 @@ public function __construct(mixed $stream, bool $read, bool $write, bool $seek,
*/
public function write(string $bytes, ?float $timeout = null): int
{
$written = $this->writeImmediately($bytes);
$written = $this->tryWrite($bytes);
if ($this->blocks || $written === strlen($bytes)) {
return $written;
}
Expand Down Expand Up @@ -176,13 +176,13 @@ static function () use (&$deferred) {
}
}

return $written + $this->writeImmediately($bytes);
return $written + $this->tryWrite($bytes);
}

/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
// there's a pending write operation, wait for it first.
$this->writeDeferred?->getAwaitable()->then(static fn() => null, static fn() => null)->await();
Expand Down Expand Up @@ -246,7 +246,7 @@ public function tell(): int
*/
public function read(?int $max_bytes = null, ?float $timeout = null): string
{
$chunk = $this->readImmediately($max_bytes);
$chunk = $this->tryRead($max_bytes);
if ('' !== $chunk || $this->blocks) {
return $chunk;
}
Expand Down Expand Up @@ -287,13 +287,13 @@ static function () use (&$deferred, $read_watcher) {
}
}

return $this->readImmediately($max_bytes);
return $this->tryRead($max_bytes);
}

/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
// there's a pending read operation, wait for it.
$this->readDeferred?->getAwaitable()->then(static fn() => null, static fn() => null)->await();
Expand Down
8 changes: 4 additions & 4 deletions src/Psl/IO/MemoryHandle.php
Expand Up @@ -37,7 +37,7 @@ public function __construct(string $buffer = '')
*
* @return string the read data on success, or an empty string if the end of file is reached.
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
$this->assertHandleIsOpen();

Expand Down Expand Up @@ -72,7 +72,7 @@ public function readImmediately(?int $max_bytes = null): string
*/
public function read(?int $max_bytes = null, ?float $timeout = null): string
{
return $this->readImmediately($max_bytes);
return $this->tryRead($max_bytes);
}

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ public function tell(): int
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes, ?float $timeout = null): int
public function tryWrite(string $bytes, ?float $timeout = null): int
{
$this->assertHandleIsOpen();
$length = strlen($this->buffer);
Expand All @@ -127,7 +127,7 @@ public function writeImmediately(string $bytes, ?float $timeout = null): int
*/
public function write(string $bytes, ?float $timeout = null): int
{
return $this->writeImmediately($bytes);
return $this->tryWrite($bytes);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Psl/IO/ReadHandleInterface.php
Expand Up @@ -10,7 +10,7 @@
interface ReadHandleInterface extends HandleInterface
{
/**
* An immediate, unordered read.
* Try to read from the handle immediately, without waiting.
*
* Up to `$max_bytes` may be allocated in a buffer; large values may lead to
* unnecessarily hitting the request memory limit.
Expand All @@ -20,12 +20,12 @@ interface ReadHandleInterface extends HandleInterface
* @throws Exception\AlreadyClosedException If the handle has been already closed.
* @throws Exception\RuntimeException If an error occurred during the operation.
*
* @return string the read data on success, or an empty string if the end of file is reached.
* @return string the read data on success, or an empty string if the handle is not ready for read, or the end of file is reached.
*
* @see ReadHandleInterface::read()
* @see ReadHandleInterface::readAll()
*/
public function readImmediately(?int $max_bytes = null): string;
public function tryRead(?int $max_bytes = null): string;

/**
* Read from the handle, waiting for data if necessary.
Expand Down
8 changes: 3 additions & 5 deletions src/Psl/IO/Reader.php
Expand Up @@ -203,13 +203,13 @@ public function read(?int $max_bytes = null, ?float $timeout = null): string

// We either have a buffer, or reached EOF; either way, behavior matches
// read, so just delegate
return $this->readImmediately($max_bytes);
return $this->tryRead($max_bytes);
}

/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
if (null !== $max_bytes) {
/** @psalm-suppress MissingThrowsDocblock */
Expand All @@ -220,15 +220,13 @@ public function readImmediately(?int $max_bytes = null): string
return '';
}

// @codeCoverageIgnoreStart
if ($this->buffer === '') {
$this->buffer = $this->getHandle()->readImmediately();
$this->buffer = $this->getHandle()->tryRead();
if ($this->buffer === '') {
$this->eof = true;
return '';
}
}
// @codeCoverageIgnoreEnd

$buffer = $this->buffer;
if ($max_bytes === null || $max_bytes >= strlen($buffer)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/CloseReadHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Psl/IO/Stream/CloseReadWriteHandle.php
Expand Up @@ -28,9 +28,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand All @@ -44,9 +44,9 @@ public function read(?int $max_bytes = null, ?float $timeout = null): string
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/CloseSeekReadHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Psl/IO/Stream/CloseSeekReadWriteHandle.php
Expand Up @@ -28,9 +28,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand All @@ -44,9 +44,9 @@ public function read(?int $max_bytes = null, ?float $timeout = null): string
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/CloseSeekWriteHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/CloseWriteHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/ReadHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Psl/IO/Stream/ReadWriteHandle.php
Expand Up @@ -28,9 +28,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand All @@ -44,9 +44,9 @@ public function read(?int $max_bytes = null, ?float $timeout = null): string
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/SeekReadHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Psl/IO/Stream/SeekReadWriteHandle.php
Expand Up @@ -28,9 +28,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function readImmediately(?int $max_bytes = null): string
public function tryRead(?int $max_bytes = null): string
{
return $this->handle->readImmediately($max_bytes);
return $this->handle->tryRead($max_bytes);
}

/**
Expand All @@ -44,9 +44,9 @@ public function read(?int $max_bytes = null, ?float $timeout = null): string
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/SeekWriteHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/IO/Stream/WriteHandle.php
Expand Up @@ -27,9 +27,9 @@ public function __construct(mixed $stream)
/**
* {@inheritDoc}
*/
public function writeImmediately(string $bytes): int
public function tryWrite(string $bytes): int
{
return $this->handle->writeImmediately($bytes);
return $this->handle->tryWrite($bytes);
}

/**
Expand Down

0 comments on commit 6c0326d

Please sign in to comment.