google-cloud-php icon indicating copy to clipboard operation
google-cloud-php copied to clipboard

PHPStan Error in google/cloud-recaptcha-enterprise

Open ykbt opened this issue 6 months ago • 2 comments

Since the package is deprecated, I was unsure which issue type would be appropriate, but I am submitting this as a Bug Report because a PHPStan error occurs.

Environment details

  • OS: Debian GNU/Linux 12 (bookworm)
  • PHP version: 8.3.22
  • Package name and version: google/cloud-recaptcha-enterprise:2.1.1, google/protobuf:4.31.1

Steps to reproduce

  1. Create an instance of RecaptchaEnterpriseServiceClient.
  2. Create a CreateAssessmentRequest object.
  3. Send the request using the client.
  4. Access the RiskAnalysis of the returned Assessment and call getReasons().
  5. Attempt to call getIterator() on the result of getReasons().
  6. A PHPStan error occurs.

Code example

use Google\Cloud\RecaptchaEnterprise\V1\Assessment;  
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;  
use Google\Cloud\RecaptchaEnterprise\V1\CreateAssessmentRequest;

// Credentials are actually passed when creating the client
$client = new RecaptchaEnterpriseServiceClient();

// Omitted: building the Assessment instance
$assessment = new Assessment();
$assessmentRequest = CreateAssessmentRequest::build($parent, $assessment);

$createdAssessment = $client->createAssessment($assessmentRequest);
$createdAssessment->getRiskAnalysis()?->getReasons()->getIterator(); // Call to an undefined method Google\Protobuf\Internal\RepeatedField::getIterator().

I’ve been following the protobuf updates and it appears this issue may be due to changes introduced here:

https://github.com/protocolbuffers/protobuf-php/commit/9a6ad10d77de8a6d3320f10bd097bed8d6021ac1#diff-f4adb7d6333d82634864e3424d178a65e8c6e413c257198fd8371622e1e0b303

I understand that this package is deprecated and things still function for now, but I wanted to report this for future compatibility and also because this causes a PHPStan error. I’d appreciate it if you could consider addressing it.

ykbt avatar Jun 11 '25 03:06 ykbt

Thank you for your report!

This should be fixed when this package is upgraded to the latest version of protobuf... something that unfortunately may take a while (~3 months).

It's one of the more frustrating issues I've noticed with PHPStan... it doesn't seem to respect aliases. There may be a way to configure it to understand them. If so, I'm more than happy to include something like that in this library (although it may be easier for you to add that to your own configuration).

bshaffer avatar Jun 16 '25 19:06 bshaffer

It's worth mentioning that you can fix this using phpstan's --autoload-file option:

$ phpstan analyse src --autoload-file=phpstan_boostrap.php

// phpstan_bootstrap.php
use Google\Protobuf\RepeatedField;
use Google\Protobuf\Internal;

require __DIR__ . '/vendor/autoload.php';

// Fix issue where PHPStan does not respect aliases
if (!class_exists(Internal\RepeatedField::class)) {
    class_alias(RepeatedField::class, Internal\RepeatedField::class);
}

bshaffer avatar Sep 10 '25 15:09 bshaffer