GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 42a542...6d81cd )
by Burhan
02:01
created

Fastdump::execute()   D

Complexity

Conditions 16
Paths 360

Size

Total Lines 74
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 51
nc 360
nop 2
dl 0
loc 74
rs 4.0372
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Graze\Morphism\Command;
4
5
use Exception;
6
use Graze\Morphism\Parse\TokenStream;
7
use Graze\Morphism\Parse\MysqlDump;
8
use Graze\Morphism\Extractor;
9
use Graze\Morphism\Config;
10
use RuntimeException;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class Fastdump extends Command
17
{
18
    const COMMAND_NAME          = 'dump';
19
20
    // Command line arguments
21
    const ARGUMENT_CONFIG_FILE  = 'config-file';
22
    const ARGUMENT_CONNECTIONS  = 'connections';
23
24
    // Command line options
25
    const OPTION_QUOTE_NAMES    = 'quote-names';
26
    const OPTION_NO_QUOTE_NAMES = 'no-quote-names';
27
    const OPTION_WRITE          = 'write';
28
    const OPTION_NO_WRITE       = 'no-write';
29
30
    /** @var bool */
31
    private $quoteNames = true;
32
    /** @var string|null */
33
    private $configFile = null;
34
    /** @var bool */
35
    private $write = false;
36
    /** @var array */
37
    private $connectionNames = [];
38
39
    protected function configure()
40
    {
41
        $this->setName(self::COMMAND_NAME);
42
43
        $helpText = sprintf(
44
            "Usage: %s [OPTIONS] CONFIG-FILE [CONN ...]\n" .
45
            "Dumps database schemas for named connections. This tool is considerably faster\n" .
46
            "than mysqldump, especially for large schemas. You might use this tool to\n" .
47
            "(re-)initalise your project's schema directory from a local database.\n" .
48
            "If no connections are specified, all connections\n" .
49
            "in the config with 'morphism: enable: true' will be used.\n" .
50
            "\n" .
51
            "OPTIONS\n" .
52
            "  -h, -help, --help   display this message, and exit\n" .
53
            "  --[no-]quote-names  [do not] quote names with `...`; default: yes\n" .
54
            "  --[no-]write        write schema files to schema path; default: no\n" .
55
            "\n" .
56
            "CONFIG-FILE\n" .
57
            "A YAML file mapping connection names to parameters. See the morphism project's\n" .
58
            "README.md file for detailed information.\n" .
59
            "",
60
            self::COMMAND_NAME
61
        );
62
        $this->setHelp($helpText);
63
64
        $this->addArgument(
65
            self::ARGUMENT_CONFIG_FILE,
66
            InputArgument::REQUIRED
67
        );
68
69
        $this->addArgument(
70
            self::ARGUMENT_CONNECTIONS,
71
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
72
            null,
73
            []
74
        );
75
76
        $this->addOption(self::OPTION_QUOTE_NAMES);
77
        $this->addOption(self::OPTION_NO_QUOTE_NAMES);
78
79
        $this->addOption(self::OPTION_WRITE);
80
        $this->addOption(self::OPTION_NO_WRITE);
81
    }
82
83
    /**
84
     * @param InputInterface $input
85
     * @param OutputInterface $output
86
     * @throws Exception
87
     */
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
                }
163
            }
164
        }
165
    }
166
}
167