| Conditions | 16 |
| Paths | 360 |
| Total Lines | 74 |
| Code Lines | 51 |
| 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 |
||
| 88 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 89 | { |
||
| 90 | $this->configFile = $input->getArgument(self::ARGUMENT_CONFIG_FILE); |
||
| 91 | $this->connectionNames = $input->getArgument(self::ARGUMENT_CONNECTIONS); |
||
| 92 | |||
| 93 | if ($input->getOption(self::OPTION_QUOTE_NAMES)) { |
||
| 94 | $this->quoteNames = false; |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($input->getOption(self::OPTION_WRITE)) { |
||
| 98 | $this->write = true; |
||
| 99 | } |
||
| 100 | |||
| 101 | $config = new Config($this->configFile); |
||
| 102 | $config->parse(); |
||
| 103 | |||
| 104 | if (! $this->connectionNames) { |
||
| 105 | $this->connectionNames = $config->getConnectionNames(); |
||
| 106 | } |
||
| 107 | |||
| 108 | foreach ($this->connectionNames as $connectionName) { |
||
| 109 | $connection = $config->getConnection($connectionName); |
||
| 110 | |||
| 111 | $entry = $config->getEntry($connectionName); |
||
| 112 | $dbName = $entry['connection']['dbname']; |
||
| 113 | $matchTables = $entry['morphism']['matchTables']; |
||
| 114 | $schemaDefinitionPath = $entry['morphism']['schemaDefinitionPath']; |
||
| 115 | |||
| 116 | if (!$this->write) { |
||
| 117 | echo "\n"; |
||
| 118 | echo "/********* Connection: $connectionName Database: $dbName *********/\n"; |
||
| 119 | echo "\n"; |
||
| 120 | } |
||
| 121 | |||
| 122 | $extractor = new Extractor($connection); |
||
| 123 | $extractor->setDatabases([$dbName]); |
||
| 124 | $extractor->setCreateDatabases(false); |
||
| 125 | $extractor->setQuoteNames($this->quoteNames); |
||
| 126 | $text = ''; |
||
| 127 | foreach ($extractor->extract() as $query) { |
||
| 128 | $text .= "$query;\n"; |
||
| 129 | } |
||
| 130 | $stream = TokenStream::newFromText($text, ''); |
||
| 131 | |||
| 132 | $dump = new MysqlDump(); |
||
| 133 | try { |
||
| 134 | $dump->parse($stream, ['matchTables' => $matchTables]); |
||
| 135 | } catch (RuntimeException $e) { |
||
| 136 | throw new RuntimeException($stream->contextualise($e->getMessage())); |
||
| 137 | } catch (Exception $e) { |
||
| 138 | throw new Exception($e->getMessage() . "\n\n" . $e->getTraceAsString()); |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($this->write) { |
||
| 142 | if (!is_dir($schemaDefinitionPath)) { |
||
| 143 | if (!@mkdir($schemaDefinitionPath, 0755, true)) { |
||
| 144 | throw new RuntimeException("could not make directory $schemaDefinitionPath"); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | $database = reset($dump->databases); |
||
| 148 | foreach ($database->tables as $table) { |
||
| 149 | $path = "$schemaDefinitionPath/{$table->name}.sql"; |
||
| 150 | $text = ''; |
||
| 151 | foreach ($table->getDDL() as $query) { |
||
| 152 | $text .= "$query;\n\n"; |
||
| 153 | } |
||
| 154 | if (false === @file_put_contents($path, $text)) { |
||
| 155 | throw new RuntimeException("could not write $path"); |
||
| 156 | } |
||
| 157 | fprintf(STDERR, "wrote $path\n"); |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | foreach ($dump->getDDL() as $query) { |
||
| 161 | echo "$query;\n\n"; |
||
| 162 | } |
||
| 167 |