GitLabPHP / Client

GitLab API v4 client for PHP
MIT License
933 stars 452 forks source link

ResultPager cannot unpack array with string keys #798

Closed tiptone closed 8 months ago

tiptone commented 8 months ago

ResultPager functions as expected unless I try to pass an array of parameters to fetchAll(). Using PHP 7.4.33 and m4tthumphrey/php-gitlab-api version 11.13.0.

This works as-expected:

$pager = new ResultPager($client);

try {
    $projects = $pager->fetchAll($client->projects(), 'all');
} catch (Exception $e) {
    throw new \Exception($e->getMessage());
}

This throws an error copy/pasted below the code:

$pager = new ResultPager($client);

try {
    $projects = $pager->fetchAll(
        $client->projects(),
        'all',
        [
            'search_namespaces' => true,
            'search' => $type,
        ]
    );
} catch (Exception $e) {
    throw new \Exception($e->getMessage());
}

PHP Fatal error: Uncaught Error: Cannot unpack array with string keys in /path/to/app/vendor/m4tthumphrey/php-gitlab-api/src/ResultPager.php:93 Stack trace:

0 /path/to/app/vendor/m4tthumphrey/php-gitlab-api/src/ResultPager.php(134): Gitlab\ResultPager->fetch(Object(Gitlab\Api\Projects), 'all', Array)

Line 93 in GitLab\ResultPager:

$result = self::bindPerPage($api, $this->perPage)->$method(...$parameters);

If my Googling is correct, unpacking an array with string keys was not allowed until PHP 8.1 but the current version (11.13.0) requires php: ^7.4.15 || ^8.0.2. Please correct me if I'm looking at this all wrong.

inuitviking commented 8 months ago

The works fine (for me at least) on PHP 8.3.

I believe this is the version number with the prefixed caret that allows me to use PHP 8.3. Basically means I can use anything from PHP 8.0.2 up until (but not including) a hypothetical PHP 9.0.

So, if your project supports it, you could use PHP 8.1 just fine.

tiptone commented 8 months ago

That is correct, the issue is not with PHP >= 8.1 but instead with the fact that this package advertises itself as compatible with php: ^7.4.15 || ^8.0.2 and that is not accurate. The current release of this library (I didn't investigate previous release compatibility) is only compatible with PHP >= 8.1.

inuitviking commented 8 months ago

Agreed. @GrahamCampbell, I have fixed this in #800, if you agree to this change.

GrahamCampbell commented 8 months ago

The code sample is incorrect.

        [
            'search_namespaces' => true,
            'search' => $type,
        ]

should be

        [
            [
                'search_namespaces' => true,
                'search' => $type,
            ]
        ]
GrahamCampbell commented 8 months ago

The array needs to be the list of params. The first param is an array, but you are passing the first param, instead of a list of params.

tiptone commented 7 months ago

While I don't find it intuitive, it works! Thank you.