Skip to content

Add check for empty string to stream factories #70

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

Merged
merged 2 commits into from
Jan 8, 2017
Merged

Conversation

sagikazarmark
Copy link
Member

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets fixes #69
Documentation does not apply
License MIT

What's in this PR?

Stream factories now check empty strings as well, not just null values.

@@ -24,7 +24,7 @@ public function createStream($body = null)
} else {
$stream = new Stream('php://memory', 'rw');

if (null !== $body) {
if (null !== $body || '' !== $body) {
Copy link
Member

Choose a reason for hiding this comment

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

Why logical or? We should write to the stream if the body has content.

if (null !== $body && '' !== $body) {

Copy link
Contributor

Choose a reason for hiding this comment

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

we could also do if (null === $body || '' === $body) {return new Stream('php://memory', 'rw');} as the rest of the code is irrelevant if the body is empty

Copy link
Member

Choose a reason for hiding this comment

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

Also this doesn't work if body is an object and casting the object to string return an empty value. We should do the following :

$content = (string) $body;

if (strlen($content) > 0) {
    $stream->write($content);
}

return $stream

Copy link
Member Author

Choose a reason for hiding this comment

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

@dbu indeed, an empty stream does not have to be rewinded, or does it?

@joelwurtz According to the interface the allowed types are string|resource|StreamInterface|null, so an object would not be valid in the first place, you would have to cast it to string first.

@@ -28,7 +28,7 @@ public function createStream($body = null)
$resource = fopen('php://memory', 'r+');
$stream = new Stream($resource);

if (null !== $body) {
if (null !== $body || '' !== $body) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here. Use &&

@dbu
Copy link
Contributor

dbu commented Jan 6, 2017

According to the interface the allowed types are string|resource|StreamInterface|null, so an object would not be valid in the first place, you would have to cast it to string first.

yes, but there is no type check to avoid objects. what exactly do we fix with this? is writing '' to a stream a bug? or is it just to be strict?

@sagikazarmark
Copy link
Member Author

Well, I guess you can technically pass objects there. There is a string casting before writting.

is writing '' to a stream a bug? or is it just to be strict?

I don't think it's a bug. But we spare an unnecessary write (and now a rewind) operation. Not sure if adding an extra cast just to check if the string value of something is empty worth it.

@dbu dbu merged commit 016359f into master Jan 8, 2017
@dbu dbu deleted the empty_stream_body branch January 8, 2017 11:51
@dbu dbu removed the in progress label Jan 8, 2017
@dbu
Copy link
Contributor

dbu commented Jan 8, 2017

if objects are not supposed to be passed, i think we are good here. if you incorrectly pass an object, the behaviour does not change compared to previous, and should still not fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check if body is empty string in stream factories
4 participants