Skip to content

Commit b06048b

Browse files
authored
fix: protobuf 4.31 deprecations (#616)
1 parent a8e25d5 commit b06048b

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ jobs:
1616
static-analysis:
1717
name: PHPStan Static Analysis
1818
uses: GoogleCloudPlatform/php-tools/.github/workflows/static-analysis.yml@main
19+
with:
20+
autoload-file: tests/bootstrap_phpstan.php

src/Serializer.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private function encodeMessageImpl(Message $message, Descriptor $messageType)
344344
$arr[$this->encodeElement($keyField, $k)] = $this->encodeElement($valueField, $vv);
345345
}
346346
$v = $arr;
347-
} elseif ($field->getLabel() === GPBLabel::REPEATED) {
347+
} elseif ($this->checkFieldRepeated($field)) {
348348
$arr = [];
349349
foreach ($v as $k => $vv) {
350350
$arr[$k] = $this->encodeElement($field, $vv);
@@ -427,7 +427,7 @@ private function decodeMessageImpl(Message $message, Descriptor $messageType, ar
427427
$arr[$this->decodeElement($keyField, $k)] = $this->decodeElement($valueField, $vv);
428428
}
429429
$value = $arr;
430-
} elseif ($field->getLabel() === GPBLabel::REPEATED) {
430+
} elseif ($this->checkFieldRepeated($field)) {
431431
$arr = [];
432432
foreach ($v as $k => $vv) {
433433
$arr[$k] = $this->decodeElement($field, $vv);
@@ -447,6 +447,17 @@ private function decodeMessageImpl(Message $message, Descriptor $messageType, ar
447447
return $message;
448448
}
449449

450+
/**
451+
* @param FieldDescriptor $field
452+
* @return bool
453+
*/
454+
private function checkFieldRepeated(FieldDescriptor $field): bool
455+
{
456+
return method_exists($field, 'isRepeated')
457+
? $field->isRepeated()
458+
: $field->getLabel() === GPBLabel::REPEATED;
459+
}
460+
450461
/**
451462
* @param string $name
452463
* @return string Getter function

tests/Unit/CredentialsWrapperTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ class CredentialsWrapperTest extends TestCase
6161
public function testBuildWithoutExplicitKeyFile($args, $expectedCredentialsWrapper)
6262
{
6363
$appDefaultCreds = getenv('GOOGLE_APPLICATION_CREDENTIALS');
64-
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/testdata/json-key-file.json');
64+
$this->setEnv('GOOGLE_APPLICATION_CREDENTIALS', __DIR__ . '/testdata/json-key-file.json');
6565

6666
$actualCredentialsWrapper = CredentialsWrapper::build($args);
6767
$this->assertEquals($expectedCredentialsWrapper, $actualCredentialsWrapper);
6868

69-
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $appDefaultCreds);
69+
$this->setEnv('GOOGLE_APPLICATION_CREDENTIALS', $appDefaultCreds);
7070
}
7171

7272
/**
@@ -81,7 +81,7 @@ public function testBuildWithKeyFile($args, $expectedCredentialsWrapper)
8181
public function buildDataWithoutExplicitKeyFile()
8282
{
8383
$appDefaultCreds = getenv('GOOGLE_APPLICATION_CREDENTIALS');
84-
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/testdata/json-key-file.json');
84+
$this->setEnv('GOOGLE_APPLICATION_CREDENTIALS', __DIR__ . '/testdata/json-key-file.json');
8585
$scopes = ['myscope'];
8686
$authHttpHandler = HttpHandlerFactory::build();
8787
$asyncAuthHttpHandler = function ($request, $options) use ($authHttpHandler) {
@@ -123,7 +123,7 @@ public function buildDataWithoutExplicitKeyFile()
123123
],
124124
];
125125

126-
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $appDefaultCreds);
126+
$this->setEnv('GOOGLE_APPLICATION_CREDENTIALS', $appDefaultCreds);
127127

128128
return $testData;
129129
}
@@ -454,8 +454,8 @@ public function getAuthorizationHeaderCallbackData()
454454
*/
455455
public function testApplicationDefaultCredentialsWithOnGCECacheTrue()
456456
{
457-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
458-
putenv(ServiceAccountCredentials::ENV_VAR); // removes it from the environment
457+
$this->setEnv('HOME', __DIR__ . '/not_exist_fixtures');
458+
$this->setEnv(ServiceAccountCredentials::ENV_VAR); // removes it from the environment
459459

460460
$mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
461461
$mockCacheItem->isHit()
@@ -484,8 +484,8 @@ public function testApplicationDefaultCredentialsWithOnGCECacheTrue()
484484
*/
485485
public function testApplicationDefaultCredentialsWithOnGCECacheFalse()
486486
{
487-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
488-
putenv(ServiceAccountCredentials::ENV_VAR); // removes it from the environment
487+
$this->setEnv('HOME', __DIR__ . '/not_exist_fixtures');
488+
$this->setEnv(ServiceAccountCredentials::ENV_VAR); // removes it from the environment
489489

490490
$this->expectException(ValidationException::class);
491491
$this->expectExceptionMessage('Could not construct ApplicationDefaultCredentials');
@@ -513,8 +513,8 @@ public function testApplicationDefaultCredentialsWithOnGCECacheFalse()
513513
*/
514514
public function testApplicationDefaultCredentialsWithOnGCECacheOptions()
515515
{
516-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
517-
putenv(ServiceAccountCredentials::ENV_VAR); // removes it from the environment
516+
$this->setEnv('HOME', __DIR__ . '/not_exist_fixtures');
517+
$this->setEnv(ServiceAccountCredentials::ENV_VAR); // removes it from the environment
518518

519519
$mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
520520
$mockCacheItem->isHit()
@@ -579,4 +579,15 @@ public function testSerializeCredentialsWrapper()
579579
$serialized = serialize($credentialsWrapper);
580580
$this->assertIsString($serialized);
581581
}
582+
583+
private function setEnv(string $env, ?string $value = null)
584+
{
585+
if ($value === null) {
586+
putenv($env);
587+
unset($_ENV[$env]);
588+
} else {
589+
putenv($env . '=' . $value);
590+
$_ENV[$env] = $value;
591+
}
592+
}
582593
}

tests/bootstrap_phpstan.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Google\Protobuf\RepeatedField;
4+
use Google\Protobuf\Internal;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
// Fix issue where PHPStan does not respect aliases
9+
if (!class_exists(Internal\RepeatedField::class)) {
10+
class_alias(RepeatedField::class, Internal\RepeatedField::class);
11+
}

0 commit comments

Comments
 (0)