Conditions | 14 |
Paths | 212 |
Total Lines | 97 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
53 | protected function execute(InputInterface $input, OutputInterface $output) |
||
|
|||
54 | { |
||
55 | $localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0]; |
||
56 | $tempFilename = dirname($localFilename) . '/' . basename($localFilename, '.phar') . '-temp.phar'; |
||
57 | |||
58 | // check for permissions in local filesystem before start connection process |
||
59 | if (!is_writable($tempDirectory = dirname($tempFilename))) { |
||
60 | throw new FilesystemException( |
||
61 | 'n98-magerun update failed: the "' . $tempDirectory . |
||
62 | '" directory used to download the temp file could not be written' |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | if (!is_writable($localFilename)) { |
||
67 | throw new FilesystemException( |
||
68 | 'n98-magerun update failed: the "' . $localFilename . '" file could not be written' |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | $io = new ConsoleIO($input, $output, $this->getHelperSet()); |
||
73 | $rfs = new RemoteFilesystem($io); |
||
74 | |||
75 | $loadUnstable = $input->getOption('unstable'); |
||
76 | if ($loadUnstable) { |
||
77 | $versionTxtUrl = 'https://raw.githubusercontent.com/netz98/n98-magerun/develop/version.txt'; |
||
78 | $remoteFilename = 'https://files.magerun.net/n98-magerun-dev.phar'; |
||
79 | } else { |
||
80 | $versionTxtUrl = 'https://raw.githubusercontent.com/netz98/n98-magerun/master/version.txt'; |
||
81 | $remoteFilename = 'https://files.magerun.net/n98-magerun.phar'; |
||
82 | } |
||
83 | |||
84 | $latest = trim($rfs->getContents('raw.githubusercontent.com', $versionTxtUrl, false)); |
||
85 | |||
86 | if ($this->getApplication()->getVersion() !== $latest || $loadUnstable) { |
||
87 | $output->writeln(sprintf("Updating to version <info>%s</info>.", $latest)); |
||
88 | |||
89 | $rfs->copy('raw.github.com', $remoteFilename, $tempFilename); |
||
90 | |||
91 | if (!file_exists($tempFilename)) { |
||
92 | $output->writeln('<error>The download of the new n98-magerun version failed for an unexpected reason'); |
||
93 | |||
94 | return 1; |
||
95 | } |
||
96 | |||
97 | try { |
||
98 | \error_reporting(E_ALL); // supress notices |
||
99 | |||
100 | @chmod($tempFilename, 0777 & ~umask()); |
||
101 | // test the phar validity |
||
102 | $phar = new \Phar($tempFilename); |
||
103 | // free the variable to unlock the file |
||
104 | unset($phar); |
||
105 | @rename($tempFilename, $localFilename); |
||
106 | $output->writeln('<info>Successfully updated n98-magerun</info>'); |
||
107 | |||
108 | if ($loadUnstable) { |
||
109 | $changeLogContent = $rfs->getContents( |
||
110 | 'raw.github.com', |
||
111 | 'https://raw.github.com/netz98/n98-magerun/develop/CHANGELOG.md', |
||
112 | false |
||
113 | ); |
||
114 | } else { |
||
115 | $changeLogContent = $rfs->getContents( |
||
116 | 'raw.github.com', |
||
117 | 'https://raw.github.com/netz98/n98-magerun/master/CHANGELOG.md', |
||
118 | false |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | if ($changeLogContent) { |
||
123 | $output->writeln($changeLogContent); |
||
124 | } |
||
125 | |||
126 | if ($loadUnstable) { |
||
127 | $unstableFooterMessage = <<<UNSTABLE_FOOTER |
||
128 | <comment> |
||
129 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
||
130 | !! DEVELOPMENT VERSION. DO NOT USE IN PRODUCTION !! |
||
131 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
||
132 | </comment> |
||
133 | UNSTABLE_FOOTER; |
||
134 | $output->writeln($unstableFooterMessage); |
||
135 | } |
||
136 | |||
137 | $this->_exit(); |
||
138 | } catch (Exception $e) { |
||
139 | @unlink($tempFilename); |
||
140 | if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) { |
||
141 | throw $e; |
||
142 | } |
||
143 | $output->writeln('<error>The download is corrupted (' . $e->getMessage() . ').</error>'); |
||
144 | $output->writeln('<error>Please re-run the self-update command to try again.</error>'); |
||
145 | } |
||
146 | } else { |
||
147 | $output->writeln("<info>You are using the latest n98-magerun version.</info>"); |
||
148 | } |
||
149 | } |
||
150 | |||
164 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: