for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Thruster\Component\HttpMessage;
use Psr\Http\Message\StreamInterface;
/**
* Class DroppingStream
*
* @package Thruster\Component\HttpMessage
* @author Aurimas Niekis <[email protected]>
*/
class DroppingStream implements StreamInterface
{
use StreamDecoratorTrait;
private $maxLength;
* @param StreamInterface $stream Underlying stream to decorate.
* @param int $maxLength Maximum size before dropping data.
public function __construct(StreamInterface $stream, int $maxLength)
$this->stream = $stream;
stream
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
$this->maxLength = $maxLength;
}
public function write($string)
$diff = $this->maxLength - $this->stream->getSize();
// Begin returning 0 when the underlying stream is too large.
if (0 >= $diff) {
return 0;
// Write the stream or a subset of the stream if needed.
if (strlen($string) < $diff) {
return $this->stream->write($string);
return $this->stream->write(substr($string, 0, $diff));
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: