| Conditions | 12 |
| Paths | 20 |
| Total Lines | 61 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 63 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 64 | { |
||
| 65 | $updater = new MapUpdater(); |
||
| 66 | $updater->selectBaseMap(MapUpdater::DEFAULT_BASE_MAP_CLASS); |
||
| 67 | |||
| 68 | // Executes on the base map the script commands. |
||
| 69 | $commands = Yaml::parse(file_get_contents($input->getOption('script'))); |
||
|
|
|||
| 70 | foreach ($commands as $command) { |
||
| 71 | $output->writeln("<info>{$command[0]} ...</info>"); |
||
| 72 | try { |
||
| 73 | $errors = call_user_func_array([$updater, $command[1]], $command[2]); |
||
| 74 | if (!empty($errors)) { |
||
| 75 | foreach ($errors as $error) { |
||
| 76 | $output->writeln("<comment>$error.</comment>"); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } catch (\Exception $e) { |
||
| 80 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
| 81 | exit(2); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | // Load the map to be changed. |
||
| 86 | MapHandler::setDefaultMapClass($input->getOption('class')); |
||
| 87 | $current_map = MapHandler::map(); |
||
| 88 | |||
| 89 | // Check if anything got changed. |
||
| 90 | $write = true; |
||
| 91 | if ($input->getOption('diff')) { |
||
| 92 | $write = false; |
||
| 93 | foreach ([ |
||
| 94 | 't' => 'MIME types', |
||
| 95 | 'a' => 'MIME type aliases', |
||
| 96 | 'e' => 'extensions', |
||
| 97 | ] as $key => $desc) { |
||
| 98 | try { |
||
| 99 | $output->writeln("<info>Checking changes to {$desc} ...</info>"); |
||
| 100 | $this->compareMaps($current_map, $updater->getMap(), $key); |
||
| 101 | } catch (\RuntimeException $e) { |
||
| 102 | $output->writeln("<comment>Changes to {$desc} mapping:</comment>"); |
||
| 103 | $output->writeln($e->getMessage()); |
||
| 104 | $write = true; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // Fail on diff if required. |
||
| 110 | if ($write && $input->getOption('diff') && $input->getOption('fail-on-diff')) { |
||
| 111 | throw new \RuntimeException('Changes to mapping detected, aborting.'); |
||
| 112 | } |
||
| 113 | |||
| 114 | // If changed, save the new map to the PHP file. |
||
| 115 | if ($write) { |
||
| 116 | $updater->writeMapToPhpClassFile($current_map->getFileName()); |
||
| 117 | $output->writeln('<comment>Code updated.</comment>'); |
||
| 118 | } else { |
||
| 119 | $output->writeln('<info>No changes to mapping.</info>'); |
||
| 120 | } |
||
| 121 | |||
| 122 | // Reset the new map's map array. |
||
| 123 | $updater->getMap()->reset(); |
||
| 124 | } |
||
| 165 |