Skip to content
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

PHP 8.0: array_walk(): Argument $key must be passed by reference, value given #1085

Closed
sebsel opened this issue Dec 2, 2020 · 3 comments
Closed

Comments

@sebsel
Copy link

sebsel commented Dec 2, 2020

While waiting on a reply on #1084, I'm stuck in a situation where I do have PHP 8.0 on my machine already, but cannot update my project because I cannot do a composer install on PHP 8.0. I can however run my already locally installed code in PHP 8.0, so there I got this bug.

Summary of problem or feature request

I got the following Exception when running stuff with Elastic in PHP 8.0:

 ErrorException

  Elasticsearch\Connections\Connection::Elasticsearch\Connections\{closure}(): Argument #2 ($key) must be passed by reference, value given

  at vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:357
    353▕                         $value = 'true';
    354▕                     } elseif ($value === false) {
    355▕                         $value = 'false';
    356▕                     }
  ➜ 357▕                 }
    358▕             );
    359▕
    360▕             $uri .= '?' . http_build_query($params);
    361▕         }

Code snippet of problem

The full method where this happens is here (comment mine):

    private function getURI(string $uri, ?array $params): string
    {
        if (isset($params) === true && !empty($params)) {
            array_walk(
                $params,
## line 346:
                function (&$value, &$key) {
                    if ($value === true) {
                        $value = 'true';
                    } elseif ($value === false) {
                        $value = 'false';
                    }
                }
            );

            $uri .= '?' . http_build_query($params);
        }

        if ($this->path !== null) {
            $uri = $this->path . $uri;
        }

        return $uri ?? '';
    }

The solution is as simple as just removing the & before $key on line 346, or removing &$key all together (it's unused).

Note that the documentation on https://www.php.net/array_walk says:

Only the values of the array may potentially be changed; its structure cannot be altered, i.e., the programmer cannot add, unset or reorder elements. If the callback does not respect this requirement, the behavior of this function is undefined, and unpredictable.

... so I think that's why in PHP 8.0 this got stricter?

System details

  • Operating System: macOS 10.15.7
  • PHP Version: v8.0.0
  • ES-PHP client version: v7.9.1
  • Elasticsearch version: v7.6.1
@mfn
Copy link

mfn commented Dec 2, 2020

Hi,
I discovered this too in #1063 (comment) and someone beat me making a PR for it (though unmerged as of now) => #1075

@Radiergummi
Copy link

@sebsel OT, but you can always do composer install --ignore-platform-reqs :)

@ezimuel
Copy link
Contributor

ezimuel commented Dec 16, 2020

@sebsel fixed in 7.x branch with #1075. I'll ship it in the next 7.11 release.

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

No branches or pull requests

4 participants