-
Notifications
You must be signed in to change notification settings - Fork 28
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
Conversation
There was a problem hiding this 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])) { |
There was a problem hiding this comment.
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])) { |
There was a problem hiding this comment.
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:
Line 293 in 2ae49a4
$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.
Basically the goal of this PR was to keep the code behavior the same after adding a 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 |
if for whatever weird reason someone sets
CURLOPT_POSTFIELDS
sets it us null and you also enable strict types in php7 thenstrlen(null)
will blow up