| Conditions | 17 |
| Paths | 39 |
| Total Lines | 102 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 declare(strict_types=1); |
||
| 64 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 65 | { |
||
| 66 | $io = new SymfonyStyle($input, $output); |
||
| 67 | |||
| 68 | $updater = new MapUpdater(); |
||
| 69 | $updater->selectBaseMap(MapUpdater::DEFAULT_BASE_MAP_CLASS); |
||
| 70 | |||
| 71 | $scriptFile = $input->getOption('script'); |
||
| 72 | if (!is_string($scriptFile)) { |
||
| 73 | $io->error('Invalid value for --script option.'); |
||
| 74 | return (2); |
||
| 75 | } |
||
| 76 | |||
| 77 | $mapClass = $input->getOption('class'); |
||
| 78 | if (!is_string($mapClass)) { |
||
| 79 | $io->error('Invalid value for --class option.'); |
||
| 80 | return (2); |
||
| 81 | } |
||
| 82 | |||
| 83 | $diff = $input->getOption('diff'); |
||
| 84 | assert(is_bool($diff)); |
||
| 85 | $failOnDiff = $input->getOption('fail-on-diff'); |
||
| 86 | assert(is_bool($failOnDiff)); |
||
| 87 | |||
| 88 | // Executes on the base map the script commands. |
||
| 89 | $contents = file_get_contents($scriptFile); |
||
| 90 | if ($contents === false) { |
||
| 91 | $io->error('Failed loading update script file ' . $scriptFile); |
||
| 92 | return (2); |
||
| 93 | } |
||
| 94 | |||
| 95 | $commands = Yaml::parse($contents); |
||
| 96 | if (!is_array($commands)) { |
||
| 97 | $io->error('Invalid update script file ' . $scriptFile); |
||
| 98 | return (2); |
||
| 99 | } |
||
| 100 | |||
| 101 | foreach ($commands as $command) { |
||
| 102 | $output->writeln("<info>{$command[0]} ...</info>"); |
||
| 103 | try { |
||
| 104 | $callable = [$updater, $command[1]]; |
||
| 105 | assert(is_callable($callable)); |
||
| 106 | $errors = call_user_func_array($callable, $command[2]); |
||
| 107 | assert(is_array($errors)); |
||
| 108 | if (!empty($errors)) { |
||
| 109 | foreach ($errors as $error) { |
||
| 110 | $output->writeln("<comment>$error.</comment>"); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } catch (\Exception $e) { |
||
| 114 | $io->error($e->getMessage()); |
||
| 115 | return(1); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // Load the map to be changed. |
||
| 120 | MapHandler::setDefaultMapClass($mapClass); |
||
| 121 | $current_map = MapHandler::map(); |
||
| 122 | |||
| 123 | // Check if anything got changed. |
||
| 124 | $write = true; |
||
| 125 | if ($diff) { |
||
| 126 | $write = false; |
||
| 127 | foreach ([ |
||
| 128 | 't' => 'MIME types', |
||
| 129 | 'a' => 'MIME type aliases', |
||
| 130 | 'e' => 'extensions', |
||
| 131 | ] as $key => $desc) { |
||
| 132 | try { |
||
| 133 | $output->writeln("<info>Checking changes to {$desc} ...</info>"); |
||
| 134 | $this->compareMaps($current_map, $updater->getMap(), $key); |
||
| 135 | } catch (\RuntimeException $e) { |
||
| 136 | $output->writeln("<comment>Changes to {$desc} mapping:</comment>"); |
||
| 137 | $output->writeln($e->getMessage()); |
||
| 138 | $write = true; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // Fail on diff if required. |
||
| 144 | if ($write && $diff && $failOnDiff) { |
||
| 145 | $io->error('Changes to mapping detected and --fail-on-diff requested, aborting.'); |
||
| 146 | return(2); |
||
| 147 | } |
||
| 148 | |||
| 149 | // If changed, save the new map to the PHP file. |
||
| 150 | if ($write) { |
||
| 151 | try { |
||
| 152 | $updater->writeMapToPhpClassFile($current_map->getFileName()); |
||
| 153 | $output->writeln('<comment>Code updated.</comment>'); |
||
| 154 | } catch (\RuntimeException $e) { |
||
| 155 | $io->error($e->getMessage() . '.'); |
||
| 156 | return(2); |
||
| 157 | } |
||
| 158 | } else { |
||
| 159 | $output->writeln('<info>No changes to mapping.</info>'); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Reset the new map's map array. |
||
| 163 | $updater->getMap()->reset(); |
||
| 164 | |||
| 165 | return(0); |
||
| 166 | } |
||
| 207 |