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

class_exists() seems to be called on a null value, which causes a deprecation warning in PHP >=8.1 #2372

Closed
leonboot opened this issue Jan 23, 2023 · 1 comment · Fixed by #2405

Comments

@leonboot
Copy link

Environment details

  • OS: N/A
  • PHP version: 8.1 and up
  • Package name and version: google/apiclient 2.13.0

Description

In https://github.com/googleapis/google-api-php-client/blob/main/src/Model.php#L296 class_exists() is called on a dynamic property. Looking at the code, the method expects the property to either not exist, or contain a valid class name as a value. However, it seems it is possible the property exists, but with a null value. PHP 8.1 and up now displays the following deprecation notice when class_exists() is called with a null value as the first parameter:

Deprecated: class_exists(): Passing null to parameter #1 ($class) of type string is deprecated

Example code

This piece of pseudo code is based on the code that triggers the warning for me:

use Google\Service\Storage;
use Google\Service\Storage\Bucket;

$bucket = new Bucket();
$bucket->setName('my-test-bucket');
$bucket->setLocation('europe-west4');
$bucket->setStorageClass('REGIONAL');
$bucket->setOwner('user:test@example.com');

/** @var Google\Client $client */
$storageSvc = new Storage($client);
$bucket = $storageSvc->buckets->insert($my_project_id, $bucket);

The last line triggers the deprecation warning.

I'm not too familiar with the inner workings of the Google API client, so I'm not sure whether my assumption is correct (model property can exist, but with a null value, which is not accounted for in the code) or that properties with null values should not exist in the first place. Therefore I can't provide a good suggestion on how the issue could be resolved.

@mikkamp
Copy link

mikkamp commented Feb 3, 2023

Getting the same warning when testing Google Listings & Ads with PHP 8.1.

I can work around it by replacing the line:

        if (property_exists($this, $keyType) && class_exists($this->$keyType)) {

with the following code:

        if (property_exists($this, $keyType) && ! is_null($this->$keyType) && class_exists($this->$keyType)) {

However it would be nice to get this fixed so we can support "current" PHP versions.

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

Successfully merging a pull request may close this issue.

2 participants