Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
cs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Dec 4, 2020
1 parent b663fe4 commit 2482da9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 70 deletions.
3 changes: 2 additions & 1 deletion src/Exception/UnopenableStreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
* UnopenableStreamException
*/
class UnopenableStreamException extends RuntimeException
{}
{
}
3 changes: 2 additions & 1 deletion src/Exception/UnreadableStreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
* UnreadableStreamException
*/
class UnreadableStreamException extends RuntimeException
{}
{
}
3 changes: 2 additions & 1 deletion src/Exception/UnseekableStreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
* UnseekableStreamException
*/
class UnseekableStreamException extends RuntimeException
{}
{
}
3 changes: 2 additions & 1 deletion src/Exception/UntellableStreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
* UntellableStreamException
*/
class UntellableStreamException extends RuntimeException
{}
{
}
3 changes: 2 additions & 1 deletion src/Exception/UnwritableStreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
* UnwritableStreamException
*/
class UnwritableStreamException extends RuntimeException
{}
{
}
94 changes: 31 additions & 63 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class Stream implements StreamInterface
*/
public function __construct($resource)
{
if (! \is_resource($resource))
{
if (! \is_resource($resource)) {
throw new \InvalidArgumentException('Invalid stream resource');
}

Expand Down Expand Up @@ -73,8 +72,7 @@ public function detach()
*/
public function close() : void
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
return;
}

Expand All @@ -92,8 +90,7 @@ public function close() : void
*/
public function eof() : bool
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
return true;
}

Expand All @@ -111,15 +108,13 @@ public function eof() : bool
*/
public function tell() : int
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
throw new Exception\UntellableStreamException('Stream is not resourceable');
}

$result = \ftell($this->resource);

if (false === $result)
{
if (false === $result) {
throw new Exception\UntellableStreamException('Unable to get the stream pointer position');
}

Expand All @@ -133,8 +128,7 @@ public function tell() : int
*/
public function isSeekable() : bool
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
return false;
}

Expand All @@ -154,20 +148,17 @@ public function isSeekable() : bool
*/
public function rewind() : void
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
throw new Exception\UnseekableStreamException('Stream is not resourceable');
}

if (! $this->isSeekable())
{
if (! $this->isSeekable()) {
throw new Exception\UnseekableStreamException('Stream is not seekable');
}

$result = \fseek($this->resource, 0, \SEEK_SET);

if (! (0 === $result))
{
if (! (0 === $result)) {
throw new Exception\UnseekableStreamException('Unable to move the stream pointer to beginning');
}
}
Expand All @@ -186,20 +177,17 @@ public function rewind() : void
*/
public function seek($offset, $whence = \SEEK_SET) : void
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
throw new Exception\UnseekableStreamException('Stream is not resourceable');
}

if (! $this->isSeekable())
{
if (! $this->isSeekable()) {
throw new Exception\UnseekableStreamException('Stream is not seekable');
}

$result = \fseek($this->resource, $offset, $whence);

if (! (0 === $result))
{
if (! (0 === $result)) {
throw new Exception\UnseekableStreamException('Unable to move the stream pointer to the given position');
}
}
Expand All @@ -211,8 +199,7 @@ public function seek($offset, $whence = \SEEK_SET) : void
*/
public function isWritable() : bool
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
return false;
}

Expand All @@ -236,20 +223,17 @@ public function isWritable() : bool
*/
public function write($string) : int
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
throw new Exception\UnwritableStreamException('Stream is not resourceable');
}

if (! $this->isWritable())
{
if (! $this->isWritable()) {
throw new Exception\UnwritableStreamException('Stream is not writable');
}

$result = \fwrite($this->resource, $string);

if (false === $result)
{
if (false === $result) {
throw new Exception\UnwritableStreamException('Unable to write to the stream');
}

Expand All @@ -263,8 +247,7 @@ public function write($string) : int
*/
public function isReadable() : bool
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
return false;
}

Expand All @@ -286,20 +269,17 @@ public function isReadable() : bool
*/
public function read($length) : string
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
throw new Exception\UnreadableStreamException('Stream is not resourceable');
}

if (! $this->isReadable())
{
if (! $this->isReadable()) {
throw new Exception\UnreadableStreamException('Stream is not readable');
}

$result = \fread($this->resource, $length);

if (false === $result)
{
if (false === $result) {
throw new Exception\UnreadableStreamException('Unable to read from the stream');
}

Expand All @@ -317,20 +297,17 @@ public function read($length) : string
*/
public function getContents() : string
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
throw new Exception\UnreadableStreamException('Stream is not resourceable');
}

if (! $this->isReadable())
{
if (! $this->isReadable()) {
throw new Exception\UnreadableStreamException('Stream is not readable');
}

$result = \stream_get_contents($this->resource);

if (false === $result)
{
if (false === $result) {
throw new Exception\UnreadableStreamException('Unable to read remainder of the stream');
}

Expand All @@ -348,15 +325,13 @@ public function getContents() : string
*/
public function getMetadata($key = null)
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
return null;
}

$metadata = \stream_get_meta_data($this->resource);

if (! (null === $key))
{
if (! (null === $key)) {
return $metadata[$key] ?? null;
}

Expand All @@ -374,15 +349,13 @@ public function getMetadata($key = null)
*/
public function getSize() : ?int
{
if (! \is_resource($this->resource))
{
if (! \is_resource($this->resource)) {
return null;
}

$stats = \fstat($this->resource);

if (false === $stats)
{
if (false === $stats) {
return null;
}

Expand All @@ -398,20 +371,15 @@ public function getSize() : ?int
*/
public function __toString()
{
try
{
if ($this->isReadable())
{
if ($this->isSeekable())
{
try {
if ($this->isReadable()) {
if ($this->isSeekable()) {
$this->rewind();
}

return $this->getContents();
}
}
catch (\Throwable $e)
{
} catch (\Throwable $e) {
// ignore...
}

Expand Down
3 changes: 1 addition & 2 deletions src/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function createStreamFromFile(string $filename, string $mode = 'r') : Str
// See http://php.net/manual/en/function.fopen.php
$resource = @ \fopen($filename, $mode);

if (false === $resource)
{
if (false === $resource) {
throw new Exception\UnopenableStreamException(
\sprintf('Unable to open file "%s" in mode "%s"', $filename, $mode)
);
Expand Down

0 comments on commit 2482da9

Please sign in to comment.