| Conditions | 11 |
| Paths | 16 |
| Total Lines | 53 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 47 | public function updates($version = self::LATEST) { |
||
| 48 | $tag = $version; |
||
| 49 | if(self::LATEST == $version) { |
||
| 50 | $tag = 'master'; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (ini_get('phar.readonly') == true) { |
||
|
|
|||
| 54 | throw new \RuntimeException('Unable to update the PHAR, phar.readonly is set, use \'-d phar.readonly=0\''); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (ini_get('allow_url_fopen') == false) { |
||
| 58 | throw new \RuntimeException('Unable to update the PHAR, allow_url_fopen is not set, use \'-d allow_url_fopen=1\''); |
||
| 59 | } |
||
| 60 | |||
| 61 | $currentPharLocation = \Phar::running(false); |
||
| 62 | if(!file_exists($currentPharLocation) ||strlen($currentPharLocation) == 0) { |
||
| 63 | throw new \LogicException("You're not currently using Phar. If you have installed PhpMetrics with Composer, please updates it using Composer."); |
||
| 64 | } |
||
| 65 | |||
| 66 | if(!is_writable($currentPharLocation)) { |
||
| 67 | throw new \RuntimeException(sprintf('%s is not writable', $currentPharLocation)); |
||
| 68 | } |
||
| 69 | |||
| 70 | // downloading |
||
| 71 | $url = sprintf($this->url, $tag); |
||
| 72 | $ctx = stream_context_create(); |
||
| 73 | stream_context_set_params($ctx, array("notification" => array($this, 'stream_notification_callback'))); |
||
| 74 | $content = file_get_contents($url, false, $ctx); |
||
| 75 | |||
| 76 | // replacing file |
||
| 77 | if(!$content) { |
||
| 78 | throw new \RuntimeException('Download failed'); |
||
| 79 | } |
||
| 80 | |||
| 81 | // check if all is OK |
||
| 82 | $tmpfile = tempnam(sys_get_temp_dir(), 'phar'); |
||
| 83 | file_put_contents($tmpfile, $content); |
||
| 84 | $output = shell_exec(sprintf('"%s" "%s" --version', PHP_BINARY, $tmpfile)); |
||
| 85 | if(!preg_match('!(v\d+\.\d+\.\d+)!', $output, $matches)) { |
||
| 86 | throw new \RuntimeException('Phar is corrupted. Please retry'); |
||
| 87 | } |
||
| 88 | |||
| 89 | // compare versions |
||
| 90 | $downloadedVersion = $matches[1]; |
||
| 91 | if(self::LATEST !==$version &&$downloadedVersion !== $version) { |
||
| 92 | throw new \RuntimeException('Incorrect version. Please retry'); |
||
| 93 | } |
||
| 94 | |||
| 95 | // at this step, all is ok |
||
| 96 | file_put_contents($currentPharLocation, $content); |
||
| 97 | return $version; |
||
| 98 | |||
| 99 | } |
||
| 100 | |||
| 152 | } |