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

Commit

Permalink
Метод Stream::toString() удален в пользу __toString() магии
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Nov 2, 2018
1 parent 0ac1a18 commit c527f51
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 5 additions & 13 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* Stream
*
* @link https://www.php-fig.org/psr/psr-7/
*/
class Stream implements StreamInterface
{
Expand Down Expand Up @@ -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
{
Expand All @@ -456,14 +458,4 @@ public function toString() : string

return '';
}

/**
* Converts the object to string
*
* @return string
*/
public function __toString()
{
return $this->toString();
}
}
11 changes: 3 additions & 8 deletions src/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/StreamFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testCreateStream()

$this->assertEquals(0, $stream->tell());

$this->assertEquals($content, $stream->toString());
$this->assertEquals($content, (string) $stream);

$stream->close();
}
Expand Down
18 changes: 2 additions & 16 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c527f51

Please sign in to comment.