Skip to content

Use isset over array_key_exists #48

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

Closed
wants to merge 3 commits into from

Conversation

gmponos
Copy link
Contributor

@gmponos gmponos commented Nov 27, 2018

if for whatever weird reason someone sets CURLOPT_POSTFIELDS sets it us null and you also enable strict types in php7 then strlen(null) will blow up

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.

thanks for contributing @gmponos . i am afraid i am 👎 on this change. i think isset hides problems. for one, if $options is not defined at all, we silently not see the options. but also, when the user specified a function resp. fields but its null, maybe the user had a mistake where their variable was null instead of what it should have been. with the isset we silently ignore those issues, instead of throwing a proper exception telling that this makes no sense.

i am fine adding an exception if the post fields are not a string. for the callback, we could check is_callable

@@ -330,10 +330,10 @@ private function createHeaders(RequestInterface $request, array $options)
continue;
}
if ('content-length' === $header) {
if (array_key_exists(CURLOPT_POSTFIELDS, $options)) {
if (isset($options[CURLOPT_POSTFIELDS])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the problem i have with isset is that it hides programming errors, like when $options is mistyped or something.

also, to spin your idea further, somebody could set the option to 42 or some object, and then we still fail.

i would prefer to have a check inside the if that throws an InvalidArgumentException when !is_string.

// Small body content length can be calculated here.
$values = [strlen($options[CURLOPT_POSTFIELDS])];
} elseif (!array_key_exists(CURLOPT_READFUNCTION, $options)) {
} elseif (!isset($options[CURLOPT_READFUNCTION])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we do not use the option here but only check for existence of the option. i'd not change this here.

we define this field as a function:

$options[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {

i would be ok to add an is_callable check to throw an InvalidArgumentException if you want.

@gmponos
Copy link
Contributor Author

gmponos commented Nov 28, 2018

Basically the goal of this PR was to keep the code behavior the same after adding a declare(strict_types=1).

Thinking this twice after your comments it might be better to leave things as they are and let php blow if it is null. Maybe then handling the Throwable somewhere centrally and convert it to any Exception might be better.

@gmponos gmponos closed this Nov 28, 2018
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.

2 participants