From c527f51efa75cb0e5893bd32ffd2881f18e3fef0 Mon Sep 17 00:00:00 2001 From: fenric Date: Fri, 2 Nov 2018 13:17:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B5=D1=82=D0=BE=D0=B4=20Stream::toStri?= =?UTF-8?q?ng()=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20=D0=B2=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=20=5F=5FtoString()=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=B3=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/Stream.php | 18 +++++------------- src/StreamFactory.php | 11 +++-------- tests/StreamFactoryTest.php | 2 +- tests/StreamTest.php | 18 ++---------------- 5 files changed, 12 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 062b528..f119c2b 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ $stream = (new StreamFactory)->createStreamFromFile('http://php.net/', 'rb'); $stream = (new StreamFactory)->createStreamFromResource(fopen(...)); // converts the stream to string without a magic -$stream->toString(); +(string) $stream; // closes the stream $stream->close(); diff --git a/src/Stream.php b/src/Stream.php index 2086489..575ada3 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -18,6 +18,8 @@ /** * Stream + * + * @link https://www.php-fig.org/psr/psr-7/ */ class Stream implements StreamInterface { @@ -431,11 +433,11 @@ public function getSize() : ?int /** * Converts the stream to string * - * This method SHOULD NOT throw an exception. - * * @return string + * + * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring */ - public function toString() : string + public function __toString() { try { @@ -456,14 +458,4 @@ public function toString() : string return ''; } - - /** - * Converts the object to string - * - * @return string - */ - public function __toString() - { - return $this->toString(); - } } diff --git a/src/StreamFactory.php b/src/StreamFactory.php index 3b793ef..cbff554 100644 --- a/src/StreamFactory.php +++ b/src/StreamFactory.php @@ -19,14 +19,14 @@ /** * StreamFactory + * + * @link https://www.php-fig.org/psr/psr-17/ */ class StreamFactory implements StreamFactoryInterface { /** * {@inheritDoc} - * - * @link http://php.net/manual/en/wrappers.php.php#wrappers.php.memory */ public function createStream(string $content = '') : StreamInterface { @@ -42,9 +42,6 @@ public function createStream(string $content = '') : StreamInterface * Creates a stream from the request body * * @return StreamInterface - * - * @link http://php.net/manual/en/wrappers.php.php#wrappers.php.memory - * @link http://php.net/manual/en/wrappers.php.php#wrappers.php.input */ public function createStreamFromRequestBody() : StreamInterface { @@ -64,9 +61,7 @@ public function createStreamFromRequestBody() : StreamInterface */ public function createStreamFromFile(string $filename, string $mode = 'r') : StreamInterface { - // If the open fails, an error of level E_WARNING is generated. - // You may use @ to suppress this warning. - // http://php.net/manual/en/function.fopen.php + // See http://php.net/manual/en/function.fopen.php $resource = @ \fopen($filename, $mode); if (false === $resource) diff --git a/tests/StreamFactoryTest.php b/tests/StreamFactoryTest.php index 6fc66c7..924af93 100644 --- a/tests/StreamFactoryTest.php +++ b/tests/StreamFactoryTest.php @@ -33,7 +33,7 @@ public function testCreateStream() $this->assertEquals(0, $stream->tell()); - $this->assertEquals($content, $stream->toString()); + $this->assertEquals($content, (string) $stream); $stream->close(); } diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 98d12cf..0e67aa5 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -285,20 +285,6 @@ public function testToString() $handle = \fopen('php://memory', 'r+b'); $stream = new Stream($handle); - \fwrite($handle, $string); - $this->assertEquals($string, $stream->toString()); - - \fclose($handle); - } - - public function testMagicToString() - { - $string = 'Hello, world!'; - $length = \strlen($string); - - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - \fwrite($handle, $string); $this->assertEquals($string, (string) $stream); @@ -457,14 +443,14 @@ public function testToStringUnresourceable() $stream = new Stream($handle); $stream->close(); - $this->assertEquals('', $stream->toString()); + $this->assertEquals('', (string) $stream); } public function testToStringUnreadable() { $stream = new Stream(\STDOUT); - $this->assertEquals('', $stream->toString()); + $this->assertEquals('', (string) $stream); } public function testExceptions()