Skip to content

Conversation

@negoziator
Copy link

Sometimes the request's parameters will end up with the last element is an integer.
That will result in a PHP error.

You cannot access an array with a type Integer

Tested on: PHP 7.4

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added the cla: no This human has *not* signed the Contributor License Agreement. label Dec 16, 2019
@negoziator
Copy link
Author

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes This human has signed the Contributor License Agreement. and removed cla: no This human has *not* signed the Contributor License Agreement. labels Dec 16, 2019
@jdpedrie
Copy link
Contributor

Thanks for the contribution @negoziator. Could you provide an example of where this is causing an error? I'm rather surprised it hasn't been seen before.

$uriTemplateVars = array();
$queryVars = array();
foreach ($params as $paramName => $paramSpec) {
if (gettype($paramSpec) !== 'array') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm interested to see that you started with is_array before switching. What was the issue with is_array?

Copy link
Author

Choose a reason for hiding this comment

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

Personally i like the is_array over the gettype, but i actually didn't think that is_array was compatible with php 5.4

I can change it back to is_array

Copy link
Contributor

Choose a reason for hiding this comment

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

is_array has been available since PHP 4. Definitely prefer it over gettype.

@negoziator
Copy link
Author

@jdpedrie I closed this PR, since i did some digging. I found out that it was actually the "Colors" class that have a fun unexpected null behavior.

I'll open another PR, that takes care of the root-cause instead.

@negoziator
Copy link
Author

@jdpedrie I've opened another PR :)

googleapis/google-api-php-client-services#188

@jdpedrie
Copy link
Contributor

Copying my comment from the other pull request since I had to close it. :(


@negoziator unfortunately, this repository contains generated code and can't be modified directly. I'm curious about this though:

PHP will default the empty array to null

When I run similar code, I don't see it casting anything to null:

function foo($arg = []) {
    $other = [];
    print_r(array_merge($other, $arg));
}

foo();

// Array
// (
// )

I think there might be an issue a bit deeper which we need to keep looking for.

If it happens that there is a generator problem, we can work on addressing it there, but I'm not entirely sure that's the case yet.

Let's continue the discussion and research in the original PR since I think it's likely a fix will originate there. :)

@negoziator
Copy link
Author

@jdpedrie

So i created a snippet here, where i try to explain the issue.
The idea is that when i loop over a lot of elements - the variable might or might not be initialized to null - which gives me the unexpected behavior.

See the snippet here, and let me know what you think :)

https://web.tinkerwell.app/#/snippets/fe2fc249-1a4e-4ed8-a005-42134f5bd7ac

@negoziator
Copy link
Author

negoziator commented Dec 20, 2019

@jdpedrie ping :)

I know the snippet is handling it, where it cant't be handled.
so it can't be fixed by that.

I'm not sure how to fix the root cause, since it's in the ->get() method.

@jdpedrie
Copy link
Contributor

jdpedrie commented Dec 20, 2019

It seems like a global issue of each method accepting any type when it only actually works with an array value. Ideally the generator should be updated to typehint array $optParams. That should resolve the problem for you, right?

In the interim, why not avoid sending null to the $optParams argument? It's incorrect usage even if it's technically accepted by the signature. Could you check for null and replace it with an empty array?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants