From 5b2a7e6bad10bd2f0622973c03e907647a92b0bc Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sat, 11 Apr 2015 21:05:17 -0300 Subject: [PATCH] [bugfix] Updated ```DiagnoseCommand::execute()``` and ```DiagnoseCommand::getGithubRateLimit()``` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #3859 | Doc PR | none Fixes #3859. --- src/Composer/Command/DiagnoseCommand.php | 64 ++++++++++++++---------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/Composer/Command/DiagnoseCommand.php b/src/Composer/Command/DiagnoseCommand.php index 423213741..c24b59290 100644 --- a/src/Composer/Command/DiagnoseCommand.php +++ b/src/Composer/Command/DiagnoseCommand.php @@ -51,6 +51,9 @@ EOT ; } + /** + * {@inheritdoc} + */ protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(false); @@ -98,21 +101,27 @@ EOT } } else { $this->getIO()->write('Checking github.com rate limit: ', false); - $rate = $this->getGithubRateLimit('github.com'); - - if (10 > $rate['remaining']) { - $this->getIO()->write('WARNING'); - $this->getIO()->write(sprintf( - 'Github has a rate limit on their API. ' - . 'You currently have %u ' - . 'out of %u requests left.' . PHP_EOL - . 'See https://developer.github.com/v3/#rate-limiting and also' . PHP_EOL - . ' https://getcomposer.org/doc/articles/troubleshooting.md#api-rate-limit-and-oauth-tokens', - $rate['remaining'], - $rate['limit'] - )); - } else { - $this->getIO()->write('OK'); + try { + $rate = $this->getGithubRateLimit('github.com'); + $this->outputResult(true); + if (10 > $rate['remaining']) { + $this->getIO()->write('WARNING'); + $this->getIO()->write(sprintf( + 'Github has a rate limit on their API. ' + . 'You currently have %u ' + . 'out of %u requests left.' . PHP_EOL + . 'See https://developer.github.com/v3/#rate-limiting and also' . PHP_EOL + . ' https://getcomposer.org/doc/articles/troubleshooting.md#api-rate-limit-and-oauth-tokens', + $rate['remaining'], + $rate['limit'] + )); + } + } catch (\Exception $e) { + if ($e instanceof TransportException && $e->getCode() === 401) { + $this->outputResult('The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it'); + } else { + $this->outputResult($e); + } } } @@ -263,25 +272,23 @@ EOT } } + /** + * @param string $domain + * @param string $token + * @return array + * @throws TransportException + */ private function getGithubRateLimit($domain, $token = null) { if ($token) { $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic'); } - try { - $url = $domain === 'github.com' ? 'https://api.'.$domain.'/rate_limit' : 'https://'.$domain.'/api/rate_limit'; - $json = $this->rfs->getContents($domain, $url, false, array('retry-auth-failure' => false)); - $data = json_decode($json, true); + $url = $domain === 'github.com' ? 'https://api.'.$domain.'/rate_limit' : 'https://'.$domain.'/api/rate_limit'; + $json = $this->rfs->getContents($domain, $url, false, array('retry-auth-failure' => false)); + $data = json_decode($json, true); - return $data['resources']['core']; - } catch (\Exception $e) { - if ($e instanceof TransportException && $e->getCode() === 401) { - return 'The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it'; - } - - return $e; - } + return $data['resources']['core']; } private function checkDiskSpace($config) @@ -308,6 +315,9 @@ EOT return true; } + /** + * @param bool|string|\Exception $result + */ private function outputResult($result) { if (true === $result) {