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