Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow cloning Stream objects #141

Draft
wants to merge 1 commit into
base: 2.26.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Stream.php
Expand Up @@ -364,4 +364,11 @@ private function isValidStreamResourceType(mixed $resource): bool

return false;
}

/**
* Disallow stream cloning.
*/
private function __clone()
{
}
}
15 changes: 15 additions & 0 deletions test/StreamTest.php
Expand Up @@ -5,6 +5,7 @@
namespace LaminasTest\Diactoros;

use CurlHandle;
use Error;
use GdImage;
use InvalidArgumentException;
use Laminas\Diactoros\Stream;
Expand Down Expand Up @@ -679,4 +680,18 @@ public function testSizeReportsNullForPhpInputStreams(): void
$stream = new Stream($resource);
$this->assertNull($stream->getSize());
}

public function testStreamsAreUnclonable(): void
{
$stream = new Stream(fopen('php://temp', 'r+'));
$stream->write('foo');

$this->assertSame('foo', $stream->__toString());

$this->expectException(Error::class);
$this->expectExceptionMessage('private Laminas\Diactoros\Stream::__clone()');

/** @psalm-suppress InvalidClone */
clone $stream;
}
}