Skip to content

Commit

Permalink
Static Code Analysis with Php Inspections (EA Extended) (googleapis#1174
Browse files Browse the repository at this point in the history
)

* language level fixes
* dead code dropped
* one-time use variables inlined
* control flow simplifications
* changes code review
* unnecessary parenthesises dropped
* fixed CS
  • Loading branch information
kalessil authored and bshaffer committed Mar 22, 2017
1 parent e430670 commit 43996f0
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 50 deletions.
5 changes: 1 addition & 4 deletions src/Google/AccessToken/Revoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ public function revokeToken($token)
$httpHandler = HttpHandlerFactory::build($this->http);

$response = $httpHandler($request);
if ($response->getStatusCode() == 200) {
return true;
}

return false;
return $response->getStatusCode() == 200;
}
}
4 changes: 2 additions & 2 deletions src/Google/AccessToken/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function __construct(
CacheItemPoolInterface $cache = null,
$jwt = null
) {
if (is_null($http)) {
if (null === $http) {
$http = new Client();
}

if (is_null($cache)) {
if (null === $cache) {
$cache = new MemoryCacheItemPool;
}

Expand Down
21 changes: 9 additions & 12 deletions src/Google/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public function refreshToken($refreshToken)
*/
public function fetchAccessTokenWithRefreshToken($refreshToken = null)
{
if (is_null($refreshToken)) {
if (null === $refreshToken) {
if (!isset($this->token['refresh_token'])) {
throw new LogicException(
'refresh token must be passed in or set as part of setAccessToken'
Expand Down Expand Up @@ -352,7 +352,7 @@ public function authorize(ClientInterface $http = null)
$credentials = null;
$token = null;
$scopes = null;
if (is_null($http)) {
if (null === $http) {
$http = $this->getHttpClient();
}

Expand All @@ -366,7 +366,7 @@ public function authorize(ClientInterface $http = null)
} elseif ($token = $this->getAccessToken()) {
$scopes = $this->prepareScopes();
// add refresh subscriber to request a new token
if ($this->isAccessTokenExpired() && isset($token['refresh_token'])) {
if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) {
$credentials = $this->createUserRefreshCredentials(
$scopes,
$token['refresh_token']
Expand Down Expand Up @@ -477,10 +477,7 @@ public function isAccessTokenExpired()
}

// If the token is set to expire in the next 30 seconds.
$expired = ($created
+ ($this->token['expires_in'] - 30)) < time();

return $expired;
return ($created + ($this->token['expires_in'] - 30)) < time();
}

public function getAuth()
Expand Down Expand Up @@ -598,7 +595,7 @@ public function setApplicationName($applicationName)
public function setRequestVisibleActions($requestVisibleActions)
{
if (is_array($requestVisibleActions)) {
$requestVisibleActions = join(" ", $requestVisibleActions);
$requestVisibleActions = implode(" ", $requestVisibleActions);
}
$this->config['request_visible_actions'] = $requestVisibleActions;
}
Expand Down Expand Up @@ -699,7 +696,7 @@ public function verifyIdToken($idToken = null)
$this->config['jwt']
);

if (is_null($idToken)) {
if (null === $idToken) {
$token = $this->getAccessToken();
if (!isset($token['id_token'])) {
throw new LogicException(
Expand Down Expand Up @@ -764,8 +761,8 @@ public function prepareScopes()
if (empty($this->requestedScopes)) {
return null;
}
$scopes = implode(' ', $this->requestedScopes);
return $scopes;

return implode(' ', $this->requestedScopes);
}

/**
Expand Down Expand Up @@ -1031,7 +1028,7 @@ public function setHttpClient(ClientInterface $http)
*/
public function getHttpClient()
{
if (is_null($this->http)) {
if (null === $this->http) {
$this->http = $this->createDefaultHttpClient();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Google/Collection.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/autoload.php';
require_once __DIR__ . '/autoload.php';
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Google/Http/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function parseResponse(ResponseInterface $response, $classes = array())
$contentType = explode(';', $contentType);
$boundary = false;
foreach ($contentType as $part) {
$part = (explode('=', $part, 2));
$part = explode('=', $part, 2);
if (isset($part[0]) && 'boundary' == trim($part[0])) {
$boundary = $part[1];
}
Expand Down Expand Up @@ -170,10 +170,6 @@ public function parseResponse(ResponseInterface $response, $classes = array())

// Need content id.
$key = $headers['content-id'];
$class = '';
if (!empty($this->expected_classes[$key])) {
$class = $this->expected_classes[$key];
}

try {
$response = Google_Http_REST::decodeHttpResponse($response, $requests[$i-1]);
Expand Down
9 changes: 3 additions & 6 deletions src/Google/Http/MediaFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ private function process()
Uri::withQueryValue($request->getUri(), 'uploadType', $uploadType)
);

$mimeType = $this->mimeType ?
$this->mimeType :
$request->getHeaderLine('content-type');
$mimeType = $this->mimeType ?: $request->getHeaderLine('content-type');

if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) {
$contentType = $mimeType;
Expand All @@ -233,7 +231,7 @@ private function process()
$postBody = $this->data;
} else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) {
// This is a multipart/related upload.
$boundary = $this->boundary ? $this->boundary : mt_rand();
$boundary = $this->boundary ?: mt_rand();
$boundary = str_replace('"', '', $boundary);
$contentType = 'multipart/related; boundary=' . $boundary;
$related = "--$boundary\r\n";
Expand Down Expand Up @@ -280,7 +278,7 @@ public function getUploadType($meta)

public function getResumeUri()
{
if (is_null($this->resumeUri)) {
if (null === $this->resumeUri) {
$this->resumeUri = $this->fetchResumeUri();
}

Expand All @@ -289,7 +287,6 @@ public function getResumeUri()

private function fetchResumeUri()
{
$result = null;
$body = $this->request->getBody();
if ($body) {
$headers = array(
Expand Down
6 changes: 3 additions & 3 deletions src/Google/Http/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function execute(
array($client, $request, $expectedClass)
);

if (!is_null($retryMap)) {
if (null !== $retryMap) {
$runner->setRetryMap($retryMap);
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public static function decodeHttpResponse(
$code = $response->getStatusCode();

// retry strategy
if ((intVal($code)) >= 400) {
if (intVal($code) >= 400) {
// if we errored out, it should be safe to grab the response body
$body = (string) $response->getBody();

Expand Down Expand Up @@ -149,7 +149,7 @@ private static function determineExpectedClass($expectedClass, RequestInterface
}

// if we don't have a request, we just use what's passed in
if (is_null($request)) {
if (null === $request) {
return $expectedClass;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Google/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ private function nullPlaceholderCheck($value)
*/
private function getMappedName($key)
{
if (isset($this->internal_gapi_mappings) &&
isset($this->internal_gapi_mappings[$key])) {
if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) {
$key = $this->internal_gapi_mappings[$key];
}
return $key;
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Service/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function createRequestUri($restPath, $params)
$queryVars = array();
foreach ($params as $paramName => $paramSpec) {
if ($paramSpec['type'] == 'boolean') {
$paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false';
$paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false';
}
if ($paramSpec['location'] == 'path') {
$uriTemplateVars[$paramName] = $paramSpec['value'];
Expand Down
11 changes: 4 additions & 7 deletions src/Google/Task/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ class Google_Task_Runner
*/
private $maxAttempts = 1;

/**
* @var string $name The name of the current task (used for logging).
*/
private $name;
/**
* @var callable $action The task to run and possibly retry.
*/
Expand Down Expand Up @@ -153,7 +149,6 @@ public function __construct(
);
}

$this->name = $name;
$this->action = $action;
$this->arguments = $arguments;
}
Expand Down Expand Up @@ -269,8 +264,10 @@ public function allowedRetries($code, $errors = array())
return $this->retryMap[$code];
}

if (!empty($errors) && isset($errors[0]['reason']) &&
isset($this->retryMap[$errors[0]['reason']])) {
if (
!empty($errors) &&
isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']])
) {
return $this->retryMap[$errors[0]['reason']];
}

Expand Down
6 changes: 3 additions & 3 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public function tryToGetAnAccessToken(Google_Client $client)

private function getClientIdAndSecret()
{
$clientId = getenv('GCLOUD_CLIENT_ID') ? getenv('GCLOUD_CLIENT_ID') : null;
$clientSecret = getenv('GCLOUD_CLIENT_SECRET') ? getenv('GCLOUD_CLIENT_SECRET') : null;
$clientId = getenv('GCLOUD_CLIENT_ID') ?: null;
$clientSecret = getenv('GCLOUD_CLIENT_SECRET') ?: null;

return array($clientId, $clientSecret);
}
Expand Down Expand Up @@ -182,7 +182,7 @@ public function checkKey()

public function loadKey()
{
if (file_exists($f = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.apiKey')) {
if (file_exists($f = __DIR__ . DIRECTORY_SEPARATOR . '.apiKey')) {
return file_get_contents($f);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private function checkAuthHandler($http, $className)
$middlewares = $property->getValue($stack);
$middleware = array_pop($middlewares);

if (is_null($className)) {
if (null === $className) {
// only the default middlewares have been added
$this->assertEquals(3, count($middlewares));
} else {
Expand All @@ -60,7 +60,7 @@ private function checkAuthHandler($http, $className)
} else {
$listeners = $http->getEmitter()->listeners('before');

if (is_null($className)) {
if (null === $className) {
$this->assertEquals(0, count($listeners));
} else {
$authClass = sprintf('Google\Auth\Subscriber\%sSubscriber', $className);
Expand Down
2 changes: 1 addition & 1 deletion tests/Google/Service/TasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testListTask()
}

$tasksArray = $tasks->listTasks($list['id']);
$this->assertTrue(sizeof($tasksArray) > 1);
$this->assertTrue(count($tasksArray) > 1);
foreach ($tasksArray['items'] as $task) {
$this->assertIsTask($task);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Google/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class_exists($class),
public function serviceProvider()
{
$classes = array();
$path = dirname(dirname(dirname(__FILE__))) . '/src/Google/Service';
$path = dirname(dirname(__DIR__)) . '/src/Google/Service';
foreach (glob($path . "/*.php") as $file) {
$classes[] = array('Google_Service_' . basename($file, '.php'));
}
Expand Down

0 comments on commit 43996f0

Please sign in to comment.