Conditions | 11 |
Paths | 64 |
Total Lines | 68 |
Code Lines | 34 |
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 |
||
31 | protected function execute(InputInterface $input, OutputInterface $output) |
||
32 | { |
||
33 | $start = microtime(true); |
||
34 | |||
35 | // as per https://www.php.net/manual/en/function.ignore-user-abort.php: for cli scripts, it is probably a good idea |
||
36 | // to use ignore_user_abort |
||
37 | ignore_user_abort(true); |
||
38 | |||
39 | $this->setOutput($output); |
||
40 | $this->setVerbosity($output->getVerbosity()); |
||
41 | |||
42 | $instanceList = $this->parseCommonOptions($input); |
||
43 | |||
44 | $dbName = $input->getOption('database'); |
||
45 | $userName = $input->getOption('user'); |
||
46 | $password = $input->getOption('password'); |
||
47 | $charset = $input->getOption('charset'); |
||
48 | |||
49 | // BC api |
||
50 | if ($dbName == null && $userName != null) { |
||
51 | $dbName = $userName; |
||
52 | } |
||
53 | |||
54 | if ($dbName == null) { |
||
55 | throw new \Exception("Please provide a database name"); |
||
56 | } |
||
57 | |||
58 | if ($userName == null) { |
||
59 | if ($password != null) { |
||
60 | throw new \Exception("Option 'password' is only valid together with option 'user'"); |
||
61 | } |
||
62 | } else { |
||
63 | if ($password == null) { |
||
64 | // 30 chars = max length for Oracle (at least up to 11g) |
||
65 | /// @todo move to a constant |
||
66 | $password = bin2hex(random_bytes(15)); |
||
67 | |||
68 | // Should we warn the user always? To avoid breaking non-text-format, we can send it to stderr... |
||
69 | // Otoh we give the password in the structured output that we produce |
||
70 | //$this->writeErrorln("<info>Assigned password to the user: $password</info>"); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | if ($this->outputFormat === 'text') { |
||
75 | $this->writeln('<info>Creating databases...</info>'); |
||
76 | } |
||
77 | |||
78 | $newDbSpecs = []; |
||
79 | foreach($instanceList as $instanceName => $instanceSpecs) { |
||
80 | $newDbSpecs[$instanceName] = [ |
||
81 | 'dbname' => $dbName |
||
82 | ]; |
||
83 | if ($userName != null) { |
||
84 | $newDbSpecs[$instanceName]['user'] = $userName; |
||
85 | $newDbSpecs[$instanceName]['password'] = $password; |
||
86 | } |
||
87 | if ($charset != '') { |
||
88 | $newDbSpecs[$instanceName]['charset'] = $charset; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | $results = $this->createDatabases($instanceList, $newDbSpecs); |
||
93 | |||
94 | $time = microtime(true) - $start; |
||
95 | |||
96 | $this->writeResults($results, $time); |
||
97 | |||
98 | return (int)$results['failed']; |
||
99 | } |
||
101 |