Skip to content

Strange error response when passing null rather then nothing. #1309

Closed

Description

@LindaLawton

$service->files->ListFiles(null);

results in

Warning: array_merge(): Argument #2 is not an array in C:\xampp\htdocs\Google-PHP-Genreated-Samples\vendor\google\apiclient-services\src\Google\Service\Drive\Resource\Files.php on line 204

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Google-PHP-Genreated-Samples\vendor\google\apiclient\src\Google\Service\Resource.php on line 136

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Google-PHP-Genreated-Samples\vendor\google\apiclient\src\Google\Service\Resource.php on line 268

The following two options work corectly

$parameters['pageSize'] = 1000;
$service->files->ListFiles($parameters);

and

$service->files->ListFiles();

While i agree there is no real reason to send null as the parameter i think the error response from the library is a little strange.

Activity

  1. bshaffer commented on Sep 25, 2017

    @bshaffer
    Contributor

    The reason is when you pass null in, you are replacing the default parameter. The default parameter is array(), as you can see here.

    It would be better to use typehints, so listFiles would throw a type exception instead.

  2. LindaLawton commented on Sep 25, 2017

    @LindaLawton
    Author

    so sending an empty array would be acceptable?

  3. bshaffer commented on Sep 25, 2017

    @bshaffer
    Contributor

    Yes, the parameters are optional. Sending an empty array is equivalent to calling the method with no parameters:

    $response = $service->files->ListFiles();
    // is equivalent to
    $parameters = [];
    $response = $service->files->ListFiles($parameters);
  4. LindaLawton commented on Sep 26, 2017

    @LindaLawton
    Author

    In your opinion what is the best way for users to do this? I was hoping to be able to have a single function which would take the parameters. I am worried your option two will confuse users.

    I am in the process of building the generated sample templates for this library. Any guidance you can to give in building solid samples which will follow your best practice guidelines would be appreciated. Google-APIs-PHP-Samples

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions