Skip to content

Do not pass arguments using default values to the Filter\fun function #89

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 3 commits into from
Jul 9, 2018
Merged

Do not pass arguments using default values to the Filter\fun function #89

merged 3 commits into from
Jul 9, 2018

Conversation

ste93cry
Copy link
Contributor

@ste93cry ste93cry commented Nov 27, 2017

Q A
Bug fix? yes
New feature? no
BC breaks? yes
Deprecations? no
Related tickets fixes #78
License MIT

What's in this PR?

This PR addresses the fact that the stream_filter_append function does not necessarily accept null as value of the $params argument for all filters. Instead the FilteredStream constructor always passed them to the function that in those cases throw the following error:

Unable to access built-in filter: stream_filter_append(): unable to create or locate filter

The cause of this (it's an assumption I made based on my tests) is that it seems that some filters do not have null as default value for the $params argument, for example the convert.base64-encode and convert.base64-decode filters have an empty array. To workaround the problem I applied the same fix that was made in clue/stream-filter#15 by using only the arguments that were explicitly passed from the user to the constructor of the FilteredStream class.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Good. This will not change the default behavior.

Could you also add a small test that proves the correctness?

@@ -57,8 +57,20 @@
*/
public function __construct(StreamInterface $stream, $readFilterOptions = null, $writeFilterOptions = null)
{
$this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions);
$this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions);
switch (func_num_args()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if we pass explicit null to the constructor, this will fail right? what if i want write filter options but no read filter options? we could instead do:

if ($readFilterOptions) {
    $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions);
} else {
    $this->readFilterCallback = Filter\fun($this->readFilter());
}
and the same for write filter

@dbu
Copy link
Contributor

dbu commented Nov 28, 2017

thanks! can you please rebase on master? that will make the styleci issue go away.

@ste93cry
Copy link
Contributor Author

ste93cry commented Dec 1, 2017

I've rebased on master and now all is green. However, I don't know how to make PHPSpec tests that verify this works fine. I think I should test that the Filter\func is called with the right arguments but I don't think it's feasible

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

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

looking at this i am pretty sure its correct...

for the test: could you do a functional test that uses an actual file stream as a base and tests the various cases with it? or does file stream not expose the quirks about null for options?

@Nyholm
Copy link
Member

Nyholm commented Dec 16, 2017

@ste93cry what is the status on the test?

@Nyholm
Copy link
Member

Nyholm commented Feb 12, 2018

Ping

@ste93cry
Copy link
Contributor Author

ste93cry commented Feb 14, 2018

I've added a proof of concept for the unit tests, as I said some time go I never used PHPSpec so I don't know how to write tests with it. I saw that the Filter\fun function throws now an exception if the filter can't be created (e.g. in the case invalid options are passed), so I'm checking that. However, a test with invalid options and without the shouldThrow line was still passing so I'm not sure I'm doing all correct

@joelwurtz
Copy link
Member

Good for me 👍 poke @dbu @Nyholm failing test are only on hhvm, i think we dropped it recently so it should be good to merge ?

@ste93cry
Copy link
Contributor Author

ste93cry commented Jul 9, 2018

@joelwurtz Please note the following part of my previous message:

However, a test with invalid options and without the shouldThrow line was still passing so I'm not sure I'm doing all correct

Before merging you should double-check the tests I wrote because I'm not sure they work properly

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank yuo

@Nyholm
Copy link
Member

Nyholm commented Jul 9, 2018

I think the tests looks good.

@Nyholm Nyholm merged commit 5e187c0 into php-http:master Jul 9, 2018
@joelwurtz
Copy link
Member

joelwurtz commented Jul 9, 2018

Before merging you should double-check the tests I wrote because I'm not sure they work properly

I check them, it's good, nice job :)

To be more clear, withtout the shouldThrow phpspec was not calling the constructor (as there is no test on it, so this was normal), however if you change shouldThrow to shouldNotThrow there you will see that there is an exception raised

@ste93cry ste93cry deleted the fix/issue-78 branch July 9, 2018 10:40
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.

False positive trigger of deprecation notice in FilteredStream
4 participants