Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Feature: "relative" stream #10

Closed
wants to merge 8 commits into from

Conversation

mtymek
Copy link
Contributor

@mtymek mtymek commented May 22, 2015

Initial implementation of "relative" stream, representing subpart of another stream, to be reviewed.

It is a first step towards parsing large bodies that may not fit in PHP's available memory.

There are at least two concerns for now:

  • using relative stream will change original stream's internal pointer. This could be avoided by adding calls to tell() and seek(), but it would also complicate things a lot (need to check if stream is seekable, etc). Should I care it at all?
  • I don't like the name - can someone come up with something better than "RelativeStream" :) ?

Note that test suite is not finished - I will do it during the weekend.

}

/**
* @inheritdoc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{@inheritdoc} with {}

@mtymek
Copy link
Contributor Author

mtymek commented May 22, 2015

@samsonasik thanks for spotting - updated.

* Class constructor
*
* @param StreamInterface $decodatedStream
* @param $offset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param paramType $offset

@mtymek mtymek force-pushed the feature/relative_stream branch from a5a4895 to 116280b Compare May 24, 2015 21:21
public function __construct(StreamInterface $decodatedStream, $offset)
{
$this->decoratedStream = $decodatedStream;
$this->offset = $offset;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast to int or check that it is an unsigned integer

@Ocramius Ocramius added this to the 1.0.1 milestone May 25, 2015
@Ocramius Ocramius self-assigned this May 25, 2015
* @param StreamInterface $decoratedStream
* @param int $offset
*/
public function __construct(StreamInterface $decoratedStream, $offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to also provide $limit = null? Or maybe am I being a feature creep here? /cc @weierophinney

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave it until it is needed - can be added any time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. We can build separate stream classes for that use-case if it ever comes up.

@mtymek
Copy link
Contributor Author

mtymek commented May 26, 2015

Added test.

@Ocramius
Copy link
Member

Looking good. Can anyone else proof-read it before I merge?

@gianarb
Copy link
Contributor

gianarb commented May 26, 2015

👍 IMO

@mtymek
Copy link
Contributor Author

mtymek commented May 26, 2015

Pushed CS fix to prevent build from failing.

{
public function testToString()
{
$decorated = $this->prophesize(Stream::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first use I've seen of Prophecy. Guess I have a bit of studying to do in the near future!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you start using it, you will never want to go back :-) Start here: https://thephp.cc/news/2015/02/phpunit-4-5-and-prophecy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll actually say that I don't like neither mockery nor prophecy mainly due to the lack of autocompletion. Yes, feel free to throw stuff at me now :-P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes I "cheat" in my tests to get autocompletion:

/** @var ObjectProphecy|Stream $decorated */
        $decorated = $this->prophesize(Stream::class);
        $decorated->seek(100, SEEK_SET)->shouldBeCalled();
        $decorated->getContents()->shouldBeCalled()->willReturn('foobarbaz');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate those comments. :)

@weierophinney
Copy link
Member

👍

Ocramius added a commit that referenced this pull request May 26, 2015
@Ocramius Ocramius closed this in 7f580a1 May 26, 2015
@Ocramius
Copy link
Member

Merged, thanks!

master: 7f580a1
develop: c5d11a6

@Ocramius Ocramius changed the title [WIP] Feature: "relative" stream Feature: "relative" stream May 26, 2015
* It can be used to avoid copying full stream, conserving memory.
* @example see Zend\Diactoros\AbstractSerializer::splitStream()
*/
class RelativeStream implements StreamInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weierophinney what do you think about final here? No methods defined besides the interface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think marking it final makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did so in #20

@mtymek mtymek deleted the feature/relative_stream branch January 27, 2016 21:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants