Conditions | 15 |
Paths | 1760 |
Total Lines | 74 |
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 |
||
54 | protected function execute(InputInterface $input, OutputInterface $output) |
||
55 | { |
||
56 | $connectionName = $input->getOption('connection'); |
||
57 | if (empty($connectionName)) { |
||
58 | $connectionName = $this->getDoctrine()->getDefaultConnectionName(); |
||
59 | } |
||
60 | $connection = $this->getDoctrineConnection($connectionName); |
||
61 | |||
62 | $ifExists = $input->getOption('if-exists'); |
||
63 | |||
64 | $params = $connection->getParams(); |
||
65 | if (isset($params['master'])) { |
||
66 | $params = $params['master']; |
||
67 | } |
||
68 | |||
69 | if (isset($params['shards'])) { |
||
70 | $shards = $params['shards']; |
||
71 | // Default select global |
||
72 | $params = array_merge($params, $params['global']); |
||
73 | if ($input->getOption('shard')) { |
||
74 | foreach ($shards as $shard) { |
||
75 | if ($shard['id'] === (int) $input->getOption('shard')) { |
||
76 | // Select sharded database |
||
77 | $params = $shard; |
||
78 | unset($params['id']); |
||
79 | break; |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false); |
||
86 | if (! $name) { |
||
87 | throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped."); |
||
88 | } |
||
89 | unset($params['dbname'], $params['url']); |
||
90 | |||
91 | if (! $input->getOption('force')) { |
||
92 | $output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.'); |
||
93 | $output->writeln(''); |
||
94 | $output->writeln(sprintf('<info>Would drop the database <comment>%s</comment> for connection named <comment>%s</comment>.</info>', $name, $connectionName)); |
||
95 | $output->writeln('Please run the operation with --force to execute'); |
||
96 | $output->writeln('<error>All data will be lost!</error>'); |
||
97 | |||
98 | return self::RETURN_CODE_NO_FORCE; |
||
99 | } |
||
100 | |||
101 | // Reopen connection without database name set |
||
102 | // as some vendors do not allow dropping the database connected to. |
||
103 | $connection->close(); |
||
104 | $connection = DriverManager::getConnection($params); |
||
105 | $shouldDropDatabase = ! $ifExists || in_array($name, $connection->getSchemaManager()->listDatabases()); |
||
106 | |||
107 | // Only quote if we don't have a path |
||
108 | if (! isset($params['path'])) { |
||
109 | $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name); |
||
110 | } |
||
111 | |||
112 | try { |
||
113 | if ($shouldDropDatabase) { |
||
114 | $connection->getSchemaManager()->dropDatabase($name); |
||
115 | $output->writeln(sprintf('<info>Dropped database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName)); |
||
116 | } else { |
||
117 | $output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> doesn\'t exist. Skipped.</info>', $name, $connectionName)); |
||
118 | } |
||
119 | |||
120 | return 0; |
||
121 | } catch (Exception $e) { |
||
122 | $output->writeln(sprintf('<error>Could not drop database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName)); |
||
123 | $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
||
124 | |||
125 | return self::RETURN_CODE_NOT_DROP; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 |