diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 000000000..3921698b0 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,18 @@ +name: Lint +on: + push: + branches: [ main ] + pull_request: + +permissions: + contents: read +jobs: + style: + name: PHP Style Check + uses: GoogleCloudPlatform/php-tools/.github/workflows/code-standards.yml@main + + staticanalysis: + name: PHPStan Static Analysis + uses: GoogleCloudPlatform/php-tools/.github/workflows/static-analysis.yml@main + with: + autoload-file: tests/phpstan-autoload.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 42578bfa7..3da2d2dfc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -49,13 +49,3 @@ jobs: command: composer update --prefer-lowest - name: Run Script run: vendor/bin/phpunit - - style: - name: PHP Style Check - uses: GoogleCloudPlatform/php-tools/.github/workflows/code-standards.yml@main - - staticanalysis: - name: PHPStan Static Analysis - uses: GoogleCloudPlatform/php-tools/.github/workflows/static-analysis.yml@main - with: - autoload-file: tests/phpstan-autoload.php diff --git a/src/CredentialSource/AwsNativeSource.php b/src/CredentialSource/AwsNativeSource.php index 6d9244ba2..e99d0ee6f 100644 --- a/src/CredentialSource/AwsNativeSource.php +++ b/src/CredentialSource/AwsNativeSource.php @@ -103,7 +103,7 @@ public function fetchSubjectToken(callable $httpHandler = null): string $headers['x-goog-cloud-target-resource'] = $this->audience; // Format headers as they're expected in the subject token - $formattedHeaders= array_map( + $formattedHeaders = array_map( fn ($k, $v) => ['key' => $k, 'value' => $v], array_keys($headers), $headers, diff --git a/src/Credentials/GCECredentials.php b/src/Credentials/GCECredentials.php index 8b7547816..49030a845 100644 --- a/src/Credentials/GCECredentials.php +++ b/src/Credentials/GCECredentials.php @@ -426,12 +426,12 @@ private static function detectResidencyWindows(string $registryProductKey): bool try { $productName = $shell->regRead($registryProductKey); - } catch(com_exception) { + } catch (com_exception) { // This means that we tried to read a key that doesn't exist on the registry // which might mean that it is a windows instance that is not on GCE return false; } - + return 0 === strpos($productName, self::PRODUCT_NAME); } diff --git a/src/Middleware/AuthTokenMiddleware.php b/src/Middleware/AuthTokenMiddleware.php index 798766efa..3bbda7a23 100644 --- a/src/Middleware/AuthTokenMiddleware.php +++ b/src/Middleware/AuthTokenMiddleware.php @@ -132,7 +132,8 @@ private function addAuthHeaders(RequestInterface $request) ) { $token = $this->fetcher->fetchAuthToken(); $request = $request->withHeader( - 'authorization', 'Bearer ' . ($token['access_token'] ?? $token['id_token'] ?? '') + 'authorization', + 'Bearer ' . ($token['access_token'] ?? $token['id_token'] ?? '') ); } else { $headers = $this->fetcher->updateMetadata($request->getHeaders(), null, $this->httpHandler); diff --git a/src/OAuth2.php b/src/OAuth2.php index 4019e258a..2463854e0 100644 --- a/src/OAuth2.php +++ b/src/OAuth2.php @@ -724,7 +724,7 @@ public function getSubjectTokenFetcher(): ?ExternalAccountCredentialSourceInterf */ public function parseTokenResponse(ResponseInterface $resp) { - $body = (string)$resp->getBody(); + $body = (string) $resp->getBody(); if ($resp->hasHeader('Content-Type') && $resp->getHeaderLine('Content-Type') == 'application/x-www-form-urlencoded' ) { @@ -1009,13 +1009,13 @@ public function setRedirectUri($uri) if (!$this->isAbsoluteUri($uri)) { // "postmessage" is a reserved URI string in Google-land // @see https://developers.google.com/identity/sign-in/web/server-side-flow - if ('postmessage' !== (string)$uri) { + if ('postmessage' !== (string) $uri) { throw new InvalidArgumentException( 'Redirect URI must be absolute' ); } } - $this->redirectUri = (string)$uri; + $this->redirectUri = (string) $uri; } /** @@ -1127,7 +1127,7 @@ public function setGrantType($grantType) 'invalid grant type' ); } - $this->grantType = (string)$grantType; + $this->grantType = (string) $grantType; } } @@ -1460,7 +1460,7 @@ public function setExpiresIn($expiresIn) $this->issuedAt = null; } else { $this->issuedAt = time(); - $this->expiresIn = (int)$expiresIn; + $this->expiresIn = (int) $expiresIn; } } @@ -1768,7 +1768,8 @@ private function getFirebaseJwtKeys($publicKey, $allowedAlgs) throw new \InvalidArgumentException( 'To have multiple allowed algorithms, You must provide an' . ' array of Firebase\JWT\Key objects.' - . ' See https://github.com/firebase/php-jwt for more information.'); + . ' See https://github.com/firebase/php-jwt for more information.' + ); } $allowedAlg = array_pop($allowedAlgs); } else { diff --git a/tests/ApplicationDefaultCredentialsTest.php b/tests/ApplicationDefaultCredentialsTest.php index fa537691f..c1583ed06 100644 --- a/tests/ApplicationDefaultCredentialsTest.php +++ b/tests/ApplicationDefaultCredentialsTest.php @@ -168,7 +168,8 @@ public function testImpersonatedServiceAccountCredentials() ); $this->assertInstanceOf( 'Google\Auth\Credentials\ImpersonatedServiceAccountCredentials', - $creds); + $creds + ); $this->assertEquals('service_account_name@namespace.iam.gserviceaccount.com', $creds->getClientName()); @@ -179,7 +180,8 @@ public function testImpersonatedServiceAccountCredentials() $sourceCredentials = $sourceCredentialsProperty->getValue($creds); $this->assertInstanceOf( 'Google\Auth\Credentials\UserRefreshCredentials', - $sourceCredentials); + $sourceCredentials + ); } public function testUserRefreshCredentials() diff --git a/tests/Cache/FileSystemCacheItemPoolTest.php b/tests/Cache/FileSystemCacheItemPoolTest.php index a3214587a..86b3e4eb5 100644 --- a/tests/Cache/FileSystemCacheItemPoolTest.php +++ b/tests/Cache/FileSystemCacheItemPoolTest.php @@ -43,7 +43,7 @@ public function tearDown(): void { $files = scandir($this->defaultCacheDirectory); - foreach($files as $fileName) { + foreach ($files as $fileName) { if ($fileName === '.' || $fileName === '..') { continue; } diff --git a/tests/CredentialSource/FileSourceTest.php b/tests/CredentialSource/FileSourceTest.php index e2c79bde7..9cdcbb9cc 100644 --- a/tests/CredentialSource/FileSourceTest.php +++ b/tests/CredentialSource/FileSourceTest.php @@ -45,7 +45,6 @@ public function provideFetchSubjectToken() $file1 = tempnam(sys_get_temp_dir(), 'test1'); file_put_contents($file1, 'abc'); - $file2 = tempnam(sys_get_temp_dir(), 'test2'); file_put_contents($file2, json_encode(['token' => 'def'])); diff --git a/tests/Credentials/ExternalAccountCredentialsTest.php b/tests/Credentials/ExternalAccountCredentialsTest.php index 09cac05db..4d1f8ae0e 100644 --- a/tests/Credentials/ExternalAccountCredentialsTest.php +++ b/tests/Credentials/ExternalAccountCredentialsTest.php @@ -561,7 +561,7 @@ public function testUrlSourceCacheKey() $expectedKey = 'fakeUrl.scope1...'; $this->assertEquals($expectedKey, $cacheKey); } - + public function testExecutableSourceCacheKey() { $this->baseCreds['credential_source'] = [ diff --git a/tests/Credentials/GCECredentialsTest.php b/tests/Credentials/GCECredentialsTest.php index 7aca40510..f6d9c2266 100644 --- a/tests/Credentials/GCECredentialsTest.php +++ b/tests/Credentials/GCECredentialsTest.php @@ -138,7 +138,7 @@ public function testOnWindowsGceWithResidencyWithNoCom() $method = (new ReflectionClass(GCECredentials::class)) ->getMethod('detectResidencyWindows'); - + $method->setAccessible(true); $this->assertFalse($method->invoke(null, 'thisShouldBeFalse')); diff --git a/tests/OAuth2Test.php b/tests/OAuth2Test.php index e00ab647f..14263fce0 100644 --- a/tests/OAuth2Test.php +++ b/tests/OAuth2Test.php @@ -290,7 +290,7 @@ public function testRedirectUriPostmessageIsAllowed() ]); $this->assertEquals('postmessage', $o->getRedirectUri()); $url = $o->buildFullAuthorizationUri(); - $parts = parse_url((string)$url); + $parts = parse_url((string) $url); parse_str($parts['query'], $query); $this->assertArrayHasKey('redirect_uri', $query); $this->assertEquals('postmessage', $query['redirect_uri']); @@ -726,7 +726,7 @@ public function testGeneratesAuthorizationCodeRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('authorization_code', $fields['grant_type']); $this->assertEquals('an_auth_code', $fields['code']); } @@ -745,7 +745,7 @@ public function testGeneratesPasswordRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('password', $fields['grant_type']); $this->assertEquals('a_password', $fields['password']); $this->assertEquals('a_username', $fields['username']); @@ -764,7 +764,7 @@ public function testGeneratesRefreshTokenRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('refresh_token', $fields['grant_type']); $this->assertEquals('a_refresh_token', $fields['refresh_token']); } @@ -780,7 +780,7 @@ public function testClientSecretAddedIfSetForAuthorizationCodeRequests() $o = new OAuth2($testConfig); $o->setCode('an_auth_code'); $request = $o->generateCredentialsRequest(); - $fields = Query::parse((string)$request->getBody()); + $fields = Query::parse((string) $request->getBody()); $this->assertEquals('a_client_secret', $fields['client_secret']); } @@ -794,7 +794,7 @@ public function testClientSecretAddedIfSetForRefreshTokenRequests() $o = new OAuth2($testConfig); $o->setRefreshToken('a_refresh_token'); $request = $o->generateCredentialsRequest(); - $fields = Query::parse((string)$request->getBody()); + $fields = Query::parse((string) $request->getBody()); $this->assertEquals('a_client_secret', $fields['client_secret']); } @@ -809,7 +809,7 @@ public function testClientSecretAddedIfSetForPasswordRequests() $o->setUsername('a_username'); $o->setPassword('a_password'); $request = $o->generateCredentialsRequest(); - $fields = Query::parse((string)$request->getBody()); + $fields = Query::parse((string) $request->getBody()); $this->assertEquals('a_client_secret', $fields['client_secret']); } @@ -827,7 +827,7 @@ public function testGeneratesAssertionRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals(OAuth2::JWT_URN, $fields['grant_type']); $this->assertArrayHasKey('assertion', $fields); } @@ -846,7 +846,7 @@ public function testGeneratesExtendedRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('my_value', $fields['my_param']); $this->assertEquals('urn:my_test_grant_type', $fields['grant_type']); } @@ -1289,7 +1289,7 @@ public function testStsCredentialsRequestMinimal() $request = $o->generateCredentialsRequest(); $this->assertEquals('POST', $request->getMethod()); $this->assertEquals($this->stsMinimal['tokenCredentialUri'], (string) $request->getUri()); - parse_str((string)$request->getBody(), $requestParams); + parse_str((string) $request->getBody(), $requestParams); $this->assertCount(4, $requestParams); $this->assertEquals(OAuth2::STS_URN, $requestParams['grant_type']); $this->assertEquals('xyz', $requestParams['subject_token']); @@ -1314,7 +1314,7 @@ public function testStsCredentialsRequestFull() $request = $o->generateCredentialsRequest(); $this->assertEquals('POST', $request->getMethod()); $this->assertEquals($this->stsMinimal['tokenCredentialUri'], (string) $request->getUri()); - parse_str((string)$request->getBody(), $requestParams); + parse_str((string) $request->getBody(), $requestParams); $this->assertCount(9, $requestParams); $this->assertEquals(OAuth2::STS_URN, $requestParams['grant_type']); diff --git a/tests/mocks/TestFileCacheItemPool.php b/tests/mocks/TestFileCacheItemPool.php index 42c8d5a5c..65fbc8a77 100644 --- a/tests/mocks/TestFileCacheItemPool.php +++ b/tests/mocks/TestFileCacheItemPool.php @@ -37,7 +37,6 @@ final class TestFileCacheItemPool implements CacheItemPoolInterface */ private $deferredItems; - public function __construct(string $cacheDir) { $this->cacheDir = $cacheDir; diff --git a/tests/phpstan-autoload.php b/tests/phpstan-autoload.php index 22a38a245..38e79cbcc 100644 --- a/tests/phpstan-autoload.php +++ b/tests/phpstan-autoload.php @@ -10,7 +10,7 @@ public function __construct(string $command) { //do nothing } - + public function regRead(string $key): string { // do nothing