Conditions | 46 |
Paths | 326 |
Total Lines | 239 |
Code Lines | 153 |
Lines | 51 |
Ratio | 21.34 % |
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 |
||
39 | protected function execute(InputInterface $input, OutputInterface $output) |
||
40 | { |
||
41 | /* @var DialogHelper $dialog */ |
||
42 | $dialog = $this->getHelperSet()->get('dialog'); |
||
43 | |||
44 | $dependenciesOption = $input->getOption('dependencies'); |
||
45 | $runCommandsOption = $input->getOption('run-commands'); |
||
46 | |||
47 | $versionsPathArg = $input->getArgument('versions-path'); |
||
48 | $versionJsonPath = getcwd().'/modera-version.txt'; |
||
49 | |||
50 | $output->writeln(''); |
||
51 | |||
52 | if (!$dependenciesOption && !$runCommandsOption) { |
||
53 | $msg = [ |
||
54 | 'If you want to update dependencies then please use <info>--dependencies</info> option for the command, ', |
||
55 | 'if you need to have commands executed when a version is upgraded then use <info>--run-commands</info> option instead.', |
||
56 | ]; |
||
57 | $output->writeln(implode('', $msg)); |
||
58 | $output->writeln(''); |
||
59 | |||
60 | return; |
||
61 | } |
||
62 | |||
63 | $output->writeln("Reading upgrade instructions from '<info>$versionsPathArg</info>'."); |
||
64 | |||
65 | $basePath = dirname($this->getContainer()->get('kernel')->getRootdir()); |
||
66 | $composerFile = new JsonFile($basePath.'/composer.json'); |
||
67 | $versionsFile = new JsonFile($versionsPathArg); |
||
68 | |||
69 | $composerFileContents = $composerFile->read(); |
||
70 | $versionsFileContents = $versionsFile->read(); |
||
71 | |||
72 | $currentVersion = @file_get_contents($versionJsonPath); |
||
73 | |||
74 | if ($dependenciesOption) { |
||
75 | $newVersion = null; |
||
76 | $versions = array_keys($versionsFileContents); |
||
77 | |||
78 | if ($currentVersion && $currentVersion == $versions[count($versions) - 1]) { |
||
79 | $output->writeln("<info>You have the latest version $currentVersion</info>"); |
||
80 | $output->writeln(''); |
||
81 | |||
82 | return; |
||
83 | } |
||
84 | |||
85 | // backup composer.json |
||
86 | file_put_contents( |
||
87 | $basePath.'/composer'.($currentVersion ? '.v'.$currentVersion : '').'.backup.json', |
||
88 | file_get_contents($basePath.'/composer.json') |
||
89 | ); |
||
90 | |||
91 | // manage dependencies |
||
92 | $oldDependencies = $newDependencies = array(); |
||
93 | if (!$currentVersion) { |
||
94 | $newVersion = $versions[0]; |
||
95 | $newDependencies = $this->getArrayValue( |
||
96 | $versionsFileContents[$newVersion], 'dependencies', array() |
||
97 | ); |
||
98 | } else { |
||
99 | foreach (array_keys($versions) as $k) { |
||
100 | if ($versions[$k] == $currentVersion) { |
||
101 | $key = $k; |
||
102 | while ($key >= 0) { |
||
103 | $oldDependencies = $this->getArrayValue( |
||
104 | $versionsFileContents[$versions[$key]], 'dependencies' |
||
105 | ); |
||
106 | if (is_array($oldDependencies)) { |
||
107 | break; |
||
108 | } |
||
109 | $oldDependencies = array(); |
||
110 | --$key; |
||
111 | } |
||
112 | |||
113 | $newVersion = $versions[$k + 1]; |
||
114 | $newDependencies = $this->getArrayValue( |
||
115 | $versionsFileContents[$newVersion], 'dependencies', $oldDependencies |
||
116 | ); |
||
117 | break; |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | $dependenciesDiff = $this->diffDependencies($oldDependencies, $newDependencies); |
||
122 | $output->writeln(sprintf('<info>Upgrading from %s to %s</info>', $currentVersion ?: '-', $newVersion)); |
||
123 | |||
124 | $dependenciesOption = $composerFileContents['require']; |
||
125 | View Code Duplication | foreach ($dependenciesDiff['added'] as $name => $ver) { |
|
|
|||
126 | if (!isset($dependenciesOption[$name])) { |
||
127 | $dependenciesOption[$name] = $ver; |
||
128 | } else { |
||
129 | if ($ver !== $dependenciesOption[$name]) { |
||
130 | $msg = sprintf(implode('', [ |
||
131 | '<question>', |
||
132 | 'Dependency "%s:%s" already exists. ', |
||
133 | 'Would you like to change it to "%s:%s"? (Y/n)', |
||
134 | '</question>', |
||
135 | ]), $name, $dependenciesOption[$name], $name, $ver); |
||
136 | |||
137 | if ($dialog->askConfirmation($output, $msg)) { |
||
138 | $dependenciesOption[$name] = $ver; |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | foreach ($dependenciesDiff['changed'] as $name => $ver) { |
||
144 | if ( |
||
145 | !isset($dependenciesOption[$name]) |
||
146 | || $oldDependencies[$name] == $dependenciesOption[$name] |
||
147 | || $ver == $dependenciesOption[$name] |
||
148 | ) { |
||
149 | $dependenciesOption[$name] = $ver; |
||
150 | } else { |
||
151 | $msg = sprintf(implode('', [ |
||
152 | '<question>', |
||
153 | 'Dependency "%s:%s" already changed. ', |
||
154 | 'Would you like to change it to "%s:%s"? (Y/n)', |
||
155 | '</question>', |
||
156 | ]), $name, $dependenciesOption[$name], $name, $ver); |
||
157 | |||
158 | if ($dialog->askConfirmation($output, $msg)) { |
||
159 | $dependenciesOption[$name] = $ver; |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | foreach ($dependenciesDiff['removed'] as $name => $ver) { |
||
164 | if (isset($dependenciesOption[$name])) { |
||
165 | $msg = sprintf(implode('', [ |
||
166 | '<question>', |
||
167 | 'Dependency "%s" has been removed. ', |
||
168 | 'Would you like to remove it? (Y/n)', |
||
169 | '</question>', |
||
170 | ]), $name); |
||
171 | |||
172 | if ($dialog->askConfirmation($output, $msg)) { |
||
173 | unset($dependenciesOption[$name]); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | View Code Duplication | foreach ($dependenciesDiff['same'] as $name => $ver) { |
|
178 | if (!isset($dependenciesOption[$name])) { |
||
179 | $dependenciesOption[$name] = $ver; |
||
180 | } elseif ($ver !== $dependenciesOption[$name]) { |
||
181 | $msg = sprintf(implode('', [ |
||
182 | '<question>', |
||
183 | 'Dependency "%s:%s" has been manually changed. ', |
||
184 | 'Would you like to restore it to "%s:%s"? (Y/n)', |
||
185 | '</question>', |
||
186 | ]), $name, $dependenciesOption[$name], $name, $ver); |
||
187 | |||
188 | if ($dialog->askConfirmation($output, $msg)) { |
||
189 | $dependenciesOption[$name] = $ver; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | $composerFileContents['require'] = $dependenciesOption; |
||
194 | |||
195 | // manage repositories |
||
196 | $repositories = $this->getArrayValue( |
||
197 | $composerFileContents, 'repositories', array() |
||
198 | ); |
||
199 | $addRepositories = $this->getArrayValue( |
||
200 | $versionsFileContents[$newVersion], 'add-repositories' |
||
201 | ); |
||
202 | $rmRepositories = $this->getArrayValue( |
||
203 | $versionsFileContents[$newVersion], 'rm-repositories' |
||
204 | ); |
||
205 | if ($addRepositories) { |
||
206 | foreach ($addRepositories as $repo) { |
||
207 | if (false === array_search($repo, $repositories)) { |
||
208 | $repositories[] = $repo; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | if ($rmRepositories) { |
||
213 | foreach ($rmRepositories as $repo) { |
||
214 | if (false !== ($key = array_search($repo, $repositories))) { |
||
215 | unset($repositories[$key]); |
||
216 | } |
||
217 | } |
||
218 | $repositories = array_values($repositories); |
||
219 | } |
||
220 | $composerFileContents['repositories'] = $repositories; |
||
221 | |||
222 | // write modera-version.txt |
||
223 | file_put_contents($versionJsonPath, $newVersion); |
||
224 | |||
225 | // write composer.json |
||
226 | $composerFile->write($composerFileContents); |
||
227 | |||
228 | // interactions |
||
229 | $output->writeln("<info>composer.json 'requires' section has been updated to version $newVersion</info>"); |
||
230 | |||
231 | View Code Duplication | if (count($this->getArrayValue($versionsFileContents[$newVersion], 'add-bundles'))) { |
|
232 | $output->writeln('<comment>Add bundle(s) to app/AppKernel.php</comment>'); |
||
233 | foreach ($versionsFileContents[$newVersion]['add-bundles'] as $bundle) { |
||
234 | $output->writeln(' '.$bundle); |
||
235 | } |
||
236 | } |
||
237 | View Code Duplication | if (count($this->getArrayValue($versionsFileContents[$newVersion], 'rm-bundles'))) { |
|
238 | $output->writeln('<comment>Remove bundle(s) from app/AppKernel.php</comment>'); |
||
239 | foreach ($versionsFileContents[$newVersion]['rm-bundles'] as $bundle) { |
||
240 | $output->writeln(' '.$bundle); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | View Code Duplication | if (count($this->getArrayValue($versionsFileContents[$newVersion], 'instructions'))) { |
|
245 | foreach ($versionsFileContents[$newVersion]['instructions'] as $instruction) { |
||
246 | $output->writeln(sprintf('<comment>%s</comment>', $instruction)); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | if (count($this->getArrayValue($versionsFileContents[$newVersion], 'commands'))) { |
||
251 | $output->writeln('After composer update run:'); |
||
252 | $output->writeln('<info>php app/console '.$this->getName().' --run-commands</info>'); |
||
253 | } |
||
254 | } elseif ($runCommandsOption) { |
||
255 | if ($currentVersion) { |
||
256 | $versionData = $this->getArrayValue( |
||
257 | $versionsFileContents, $currentVersion, array() |
||
258 | ); |
||
259 | $commands = $this->getArrayValue( |
||
260 | $versionData, 'commands', array() |
||
261 | ); |
||
262 | |||
263 | if (count($commands) > 0) { |
||
264 | $this->getApplication()->setAutoExit(false); |
||
265 | foreach ($commands as $command) { |
||
266 | $output->writeln(''); |
||
267 | $output->writeln("<comment>$command</comment>"); |
||
268 | $this->getApplication()->run(new StringInput($command), $output); |
||
269 | } |
||
270 | } else { |
||
271 | $output->writeln('<comment>No commands need to be run! Aborting ...</comment>'); |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | $output->writeln(''); |
||
277 | } |
||
278 | |||
318 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.