| Conditions | 12 |
| Paths | 71 |
| Total Lines | 93 |
| 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 |
||
| 58 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 59 | { |
||
| 60 | MapHandler::setDefaultMapClass($input->getOption('class')); |
||
| 61 | $current_map = MapHandler::map(); |
||
| 62 | $updater = new MapUpdater(); |
||
| 63 | |||
| 64 | // Loads the map from the source file. |
||
| 65 | try { |
||
| 66 | $new_map = $updater->createMapFromSourceFile($input->getOption('source')); |
||
| 67 | } catch (\RuntimeException $e) { |
||
| 68 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
| 69 | exit(2); |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | $xml = simplexml_load_string(file_get_contents('https://raw.github.com/minad/mimemagic/master/script/freedesktop.org.xml')); |
||
| 79 | foreach ($xml as $node) { |
||
| 80 | $exts = []; |
||
| 81 | foreach ($node->glob as $glob) { |
||
| 82 | $pattern = (string) $glob['pattern']; |
||
| 83 | if ('*' != $pattern[0] || '.' != $pattern[1]) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | $exts[] = substr($pattern, 2); |
||
| 87 | } |
||
| 88 | if (!$exts) { |
||
|
|
|||
| 89 | continue; |
||
| 90 | } |
||
| 91 | $mt = (string) $node['type']; |
||
| 92 | foreach ($exts as $ext) { |
||
| 93 | $new_map->addMapping($mt, $ext); |
||
| 94 | } |
||
| 95 | //$new[$mt] = $exts; |
||
| 96 | /* foreach ($node->alias as $alias) { |
||
| 97 | $mt = strtolower((string) $alias['type']); |
||
| 98 | $new[$mt] = array_merge($new[$mt] ?? [], $exts); |
||
| 99 | }*/ |
||
| 100 | } |
||
| 101 | //dump($new); |
||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | // Applies the overrides. |
||
| 115 | try { |
||
| 116 | $content = file_get_contents($input->getOption('override')); |
||
| 117 | $updater->applyOverrides($new_map, Yaml::parse($content)); |
||
| 118 | } catch (\Exception $e) { |
||
| 119 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
| 120 | exit(2); |
||
| 121 | } |
||
| 122 | |||
| 123 | // Check if anything got changed. |
||
| 124 | $write = false; |
||
| 125 | try { |
||
| 126 | $this->compareMaps($current_map, $new_map, 'types'); |
||
| 127 | } catch (\RuntimeException $e) { |
||
| 128 | $output->writeln('<comment>Changes to MIME types mapping</comment>'); |
||
| 129 | $output->writeln($e->getMessage()); |
||
| 130 | $write = true; |
||
| 131 | } |
||
| 132 | try { |
||
| 133 | $this->compareMaps($current_map, $new_map, 'extensions'); |
||
| 134 | } catch (\RuntimeException $e) { |
||
| 135 | $output->writeln('<comment>Changes to extensions mapping</comment>'); |
||
| 136 | $output->writeln($e->getMessage()); |
||
| 137 | $write = true; |
||
| 138 | } |
||
| 139 | |||
| 140 | // If changed, save the new map to the PHP file. |
||
| 141 | if ($write) { |
||
| 142 | $updater->writeMapToPhpClassFile($new_map, $current_map->getFileName()); |
||
| 143 | $output->writeln('<comment>Code updated.</comment>'); |
||
| 144 | } else { |
||
| 145 | $output->writeln('<info>No changes to mapping.</info>'); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Reset the new map's map array. |
||
| 149 | $new_map->reset(); |
||
| 150 | } |
||
| 151 | |||
| 191 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.