PHPStan Error in google/cloud-recaptcha-enterprise
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
- Create an instance of
RecaptchaEnterpriseServiceClient. - Create a
CreateAssessmentRequestobject. - Send the request using the client.
- Access the
RiskAnalysisof the returnedAssessmentand callgetReasons(). - Attempt to call
getIterator()on the result ofgetReasons(). - 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:
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.
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).
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);
}