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

Commit

Permalink
Removed Stream::isResourceable(), Stream::truncate() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Nov 6, 2018
1 parent 1159eb0 commit 7e652c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 110 deletions.
67 changes: 13 additions & 54 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ public function __construct($resource)
$this->resource = $resource;
}

/**
* Checks if the stream is resourceable
*
* @return bool
*/
public function isResourceable() : bool
{
return \is_resource($this->resource);
}

/**
* Detaches a resource from the stream
*
Expand All @@ -83,7 +73,7 @@ public function detach()
*/
public function close() : void
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
return;
}
Expand All @@ -102,7 +92,7 @@ public function close() : void
*/
public function eof() : bool
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
return true;
}
Expand All @@ -121,7 +111,7 @@ public function eof() : bool
*/
public function tell() : int
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
throw new Exception\UntellableStreamException('Stream is not resourceable');
}
Expand All @@ -143,7 +133,7 @@ public function tell() : int
*/
public function isSeekable() : bool
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
return false;
}
Expand All @@ -164,7 +154,7 @@ public function isSeekable() : bool
*/
public function rewind() : void
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
throw new Exception\UnseekableStreamException('Stream is not resourceable');
}
Expand Down Expand Up @@ -196,7 +186,7 @@ public function rewind() : void
*/
public function seek($offset, $whence = \SEEK_SET) : void
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
throw new Exception\UnseekableStreamException('Stream is not resourceable');
}
Expand All @@ -221,7 +211,7 @@ public function seek($offset, $whence = \SEEK_SET) : void
*/
public function isWritable() : bool
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
return false;
}
Expand All @@ -246,7 +236,7 @@ public function isWritable() : bool
*/
public function write($string) : int
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
throw new Exception\UnwritableStreamException('Stream is not resourceable');
}
Expand All @@ -266,45 +256,14 @@ public function write($string) : int
return $result;
}

/**
* Truncates the stream to the given length
*
* @param int $length
*
* @return void
*
* @throws Exception\UnwritableStreamException
*
* @link http://php.net/manual/en/function.ftruncate.php
*/
public function truncate(int $length = 0) : void
{
if (! $this->isResourceable())
{
throw new Exception\UnwritableStreamException('Stream is not resourceable');
}

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

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

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

/**
* Checks if the stream is readable
*
* @return bool
*/
public function isReadable() : bool
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
return false;
}
Expand All @@ -327,7 +286,7 @@ public function isReadable() : bool
*/
public function read($length) : string
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
throw new Exception\UnreadableStreamException('Stream is not resourceable');
}
Expand Down Expand Up @@ -358,7 +317,7 @@ public function read($length) : string
*/
public function getContents() : string
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
throw new Exception\UnreadableStreamException('Stream is not resourceable');
}
Expand Down Expand Up @@ -389,7 +348,7 @@ public function getContents() : string
*/
public function getMetadata($key = null)
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
return null;
}
Expand All @@ -415,7 +374,7 @@ public function getMetadata($key = null)
*/
public function getSize() : ?int
{
if (! $this->isResourceable())
if (! \is_resource($this->resource))
{
return null;
}
Expand Down
68 changes: 12 additions & 56 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,7 @@ public function testConstructor()
$stream = new Stream($handle);

$this->assertInstanceOf(StreamInterface::class, $stream);

\fclose($handle);
}

public function testIsResourceable()
{
$handle = \fopen('php://memory', 'r+b');
$stream = new Stream($handle);

$this->assertTrue($stream->isResourceable());

$stream->detach();
$this->assertFalse($stream->isResourceable());
$this->assertStreamResourceEquals($stream, $handle);

\fclose($handle);
}
Expand All @@ -44,8 +32,7 @@ public function testDetach()
$stream = new Stream($handle);

$this->assertEquals($handle, $stream->detach());

$this->assertFalse($stream->isResourceable());
$this->assertStreamResourceEquals($stream, null);
$this->assertEquals(null, $stream->detach());

\fclose($handle);
Expand All @@ -57,6 +44,7 @@ public function testClose()
$stream = new Stream($handle);

$stream->close();
$this->assertStreamResourceEquals($stream, null);
$this->assertFalse(\is_resource($handle));

if (\is_resource($handle)) {
Expand Down Expand Up @@ -170,26 +158,6 @@ public function testWrite()
\fclose($handle);
}

public function testTruncate()
{
$string = 'Hello, world!';
$length = \strlen($string);

$handle = \fopen('php://memory', 'r+b');
$stream = new Stream($handle);

\fwrite($handle, $string);
$stream->truncate(4);
\rewind($handle);
$this->assertEquals(\substr($string, 0, 4), \fread($handle, $length));

$stream->truncate(0);
\rewind($handle);
$this->assertEquals('', \fread($handle, $length));

\fclose($handle);
}

public function testIsReadable()
{
$stream = new Stream(\STDIN);
Expand Down Expand Up @@ -347,18 +315,6 @@ public function testWriteUnresourceable()
$stream->write('0', 1);
}

public function testTruncateUnresourceable()
{
$this->expectException(UnwritableStreamException::class);
$this->expectExceptionMessage('Stream is not resourceable');

$handle = \fopen('php://memory', 'r+b');
$stream = new Stream($handle);

$stream->close();
$stream->truncate(0);
}

public function testReadUnresourceable()
{
$this->expectException(UnreadableStreamException::class);
Expand Down Expand Up @@ -392,15 +348,6 @@ public function testWriteUnwritable()
$stream->write('0', 1);
}

public function testTruncateUnwritable()
{
$this->expectException(UnwritableStreamException::class);
$this->expectExceptionMessage('Stream is not writable');

$stream = new Stream(\STDIN);
$stream->truncate(0);
}

public function testReadUnreadable()
{
$this->expectException(UnreadableStreamException::class);
Expand Down Expand Up @@ -463,4 +410,13 @@ public function testExceptions()
$this->assertInstanceOf(Exception::class, new UntellableStreamException(''));
$this->assertInstanceOf(Exception::class, new UnwritableStreamException(''));
}

private function assertStreamResourceEquals(StreamInterface $stream, $expected)
{
$property = new \ReflectionProperty($stream, 'resource');

$property->setAccessible(true);

return $this->assertEquals($property->getValue($stream), $expected);
}
}

0 comments on commit 7e652c5

Please sign in to comment.