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

chore: updates from new cs rules #577

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 0 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/CredentialSource/AwsNativeSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/Credentials/GCECredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/AuthTokenMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions src/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -1127,7 +1127,7 @@ public function setGrantType($grantType)
'invalid grant type'
);
}
$this->grantType = (string)$grantType;
$this->grantType = (string) $grantType;
}
}

Expand Down Expand Up @@ -1460,7 +1460,7 @@ public function setExpiresIn($expiresIn)
$this->issuedAt = null;
} else {
$this->issuedAt = time();
$this->expiresIn = (int)$expiresIn;
$this->expiresIn = (int) $expiresIn;
}
}

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions tests/ApplicationDefaultCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public function testImpersonatedServiceAccountCredentials()
);
$this->assertInstanceOf(
'Google\Auth\Credentials\ImpersonatedServiceAccountCredentials',
$creds);
$creds
);

$this->assertEquals('[email protected]', $creds->getClientName());

Expand All @@ -179,7 +180,8 @@ public function testImpersonatedServiceAccountCredentials()
$sourceCredentials = $sourceCredentialsProperty->getValue($creds);
$this->assertInstanceOf(
'Google\Auth\Credentials\UserRefreshCredentials',
$sourceCredentials);
$sourceCredentials
);
}

public function testUserRefreshCredentials()
Expand Down
2 changes: 1 addition & 1 deletion tests/Cache/FileSystemCacheItemPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function tearDown(): void
{
$files = scandir($this->defaultCacheDirectory);

foreach($files as $fileName) {
foreach ($files as $fileName) {
if ($fileName === '.' || $fileName === '..') {
continue;
}
Expand Down
1 change: 0 additions & 1 deletion tests/CredentialSource/FileSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));

Expand Down
2 changes: 1 addition & 1 deletion tests/Credentials/ExternalAccountCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public function testUrlSourceCacheKey()
$expectedKey = 'fakeUrl.scope1...';
$this->assertEquals($expectedKey, $cacheKey);
}

public function testExecutableSourceCacheKey()
{
$this->baseCreds['credential_source'] = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Credentials/GCECredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testOnWindowsGceWithResidencyWithNoCom()

$method = (new ReflectionClass(GCECredentials::class))
->getMethod('detectResidencyWindows');

$method->setAccessible(true);

$this->assertFalse($method->invoke(null, 'thisShouldBeFalse'));
Expand Down
22 changes: 11 additions & 11 deletions tests/OAuth2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
}
Expand All @@ -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']);
Expand All @@ -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']);
}
Expand All @@ -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']);
}

Expand All @@ -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']);
}

Expand All @@ -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']);
}

Expand All @@ -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);
}
Expand All @@ -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']);
}
Expand Down Expand Up @@ -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']);
Expand All @@ -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']);
Expand Down
1 change: 0 additions & 1 deletion tests/mocks/TestFileCacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ final class TestFileCacheItemPool implements CacheItemPoolInterface
*/
private $deferredItems;


public function __construct(string $cacheDir)
{
$this->cacheDir = $cacheDir;
Expand Down
2 changes: 1 addition & 1 deletion tests/phpstan-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(string $command)
{
//do nothing
}

public function regRead(string $key): string
{
// do nothing
Expand Down