Skip to content

Code improvements found with code analysis #765

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

Merged
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
4 changes: 2 additions & 2 deletions lib/Github/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function get($path, array $parameters = [], array $requestHeaders = []
if (null !== $this->perPage && !isset($parameters['per_page'])) {
$parameters['per_page'] = $this->perPage;
}
if (array_key_exists('ref', $parameters) && is_null($parameters['ref'])) {
if (array_key_exists('ref', $parameters) && null === $parameters['ref']) {
unset($parameters['ref']);
}

Expand All @@ -122,7 +122,7 @@ protected function get($path, array $parameters = [], array $requestHeaders = []
*/
protected function head($path, array $parameters = [], array $requestHeaders = [])
{
if (array_key_exists('ref', $parameters) && is_null($parameters['ref'])) {
if (array_key_exists('ref', $parameters) && null === $parameters['ref']) {
unset($parameters['ref']);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/AcceptHeaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
trait AcceptHeaderTrait
{
protected $acceptHeaderValue = null;
protected $acceptHeaderValue;

protected function get($path, array $parameters = [], array $requestHeaders = [])
{
Expand Down
4 changes: 2 additions & 2 deletions lib/Github/Api/CurrentUser/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function add($emails)
if (is_string($emails)) {
$emails = [$emails];
} elseif (0 === count($emails)) {
throw new InvalidArgumentException();
throw new InvalidArgumentException('The user emails parameter should be a single email or an array of emails');
}

return $this->post('/user/emails', $emails);
Expand All @@ -74,7 +74,7 @@ public function remove($emails)
if (is_string($emails)) {
$emails = [$emails];
} elseif (0 === count($emails)) {
throw new InvalidArgumentException();
throw new InvalidArgumentException('The user emails parameter should be a single email or an array of emails');
}

return $this->delete('/user/emails', $emails);
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/Gists.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Gists extends AbstractApi
*/
public function configure($bodyType = null)
{
if (!in_array($bodyType, ['base64'])) {
if ('base64' !== $bodyType) {
$bodyType = 'raw';
}

Expand Down
10 changes: 5 additions & 5 deletions lib/Github/Api/Issue/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public function update($username, $repository, $label, $newName, $color)
*
* @link https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue
*
* @param string $username
* @param string $repository
* @param int $issue
* @param string $labels
* @param string $username
* @param string $repository
* @param int $issue
* @param string|array $labels
*
* @return array
*
Expand All @@ -134,7 +134,7 @@ public function add($username, $repository, $issue, $labels)
if (is_string($labels)) {
$labels = [$labels];
} elseif (0 === count($labels)) {
throw new InvalidArgumentException();
throw new InvalidArgumentException('The labels parameter should be a single label or an array of labels');
}

return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/issues/'.rawurlencode($issue).'/labels', $labels);
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/PullRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PullRequest extends AbstractApi
*/
public function configure($bodyType = null, $apiVersion = null)
{
if (!in_array($apiVersion, [])) {
if (null === $apiVersion) {
$apiVersion = $this->client->getApiVersion();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/PullRequest/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Comments extends AbstractApi
*/
public function configure($bodyType = null, $apiVersion = null)
{
if (!in_array($apiVersion, ['squirrel-girl-preview'])) {
if ($apiVersion !== 'squirrel-girl-preview') {
$apiVersion = $this->client->getApiVersion();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/Repository/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function exists($username, $repository, $path, $reference = null)
'ref' => $reference,
]);

if ($response->getStatusCode() != 200) {
if ($response->getStatusCode() !== 200) {
return false;
}
} catch (TwoFactorAuthenticationRequiredException $ex) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
throw new InvalidArgumentException('You need to specify authentication method!');
}

if (null === $authMethod && in_array($password, [self::AUTH_URL_TOKEN, self::AUTH_URL_CLIENT_ID, self::AUTH_HTTP_PASSWORD, self::AUTH_HTTP_TOKEN, self::AUTH_JWT])) {
if (null === $authMethod && in_array($password, [self::AUTH_URL_TOKEN, self::AUTH_URL_CLIENT_ID, self::AUTH_HTTP_PASSWORD, self::AUTH_HTTP_TOKEN, self::AUTH_JWT], true)) {
$authMethod = $password;
$password = null;
}
Expand Down
16 changes: 8 additions & 8 deletions lib/Github/HttpClient/Plugin/GithubExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ public function handleRequest(RequestInterface $request, callable $next, callabl

// If error:
$remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining');
if (null != $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) {
if (null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) {
$limit = ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
$reset = ResponseMediator::getHeader($response, 'X-RateLimit-Reset');

throw new ApiLimitExceedException($limit, $reset);
}

if (401 === $response->getStatusCode()) {
if ($response->hasHeader('X-GitHub-OTP') && 0 === strpos((string) ResponseMediator::getHeader($response, 'X-GitHub-OTP'), 'required;')) {
$type = substr((string) ResponseMediator::getHeader($response, 'X-GitHub-OTP'), 9);
if ((401 === $response->getStatusCode()) && $response->hasHeader('X-GitHub-OTP') && 0 === strpos((string) ResponseMediator::getHeader($response, 'X-GitHub-OTP'), 'required;')) {
$type = substr((string) ResponseMediator::getHeader($response, 'X-GitHub-OTP'), 9);

throw new TwoFactorAuthenticationRequiredException($type);
}
throw new TwoFactorAuthenticationRequiredException($type);
}

$content = ResponseMediator::getContent($response);
if (is_array($content) && isset($content['message'])) {
if (400 == $response->getStatusCode()) {
if (400 === $response->getStatusCode()) {
throw new ErrorException($content['message'], 400);
} elseif (422 == $response->getStatusCode() && isset($content['errors'])) {
}

if (422 === $response->getStatusCode() && isset($content['errors'])) {
$errors = [];
foreach ($content['errors'] as $error) {
switch ($error['code']) {
Expand Down