Conditions | 13 |
Paths | 64 |
Total Lines | 87 |
Code Lines | 49 |
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 |
||
44 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
45 | { |
||
46 | if (!$this->lock()) { |
||
47 | $output->writeln('The command is already running in another process.'); |
||
48 | |||
49 | return Command::SUCCESS; |
||
50 | } |
||
51 | |||
52 | $io = new SymfonyStyle($input, $output); |
||
53 | |||
54 | $dumpSql = true === $input->getOption('dump-sql'); |
||
55 | $force = true === $input->getOption('force'); |
||
56 | |||
57 | /** @var DoctrineProvider $provider */ |
||
58 | $provider = $this->auditor->getProvider(DoctrineProvider::class); |
||
59 | $updateManager = new SchemaManager($provider); |
||
60 | |||
61 | $sqls = $updateManager->getUpdateAuditSchemaSql(); |
||
62 | |||
63 | $count = 0; |
||
64 | foreach ($sqls as $queries) { |
||
65 | $count += is_countable($queries) ? \count($queries) : 0; |
||
66 | } |
||
67 | |||
68 | if (0 === $count) { |
||
69 | $io->success('Nothing to update.'); |
||
70 | $this->release(); |
||
71 | |||
72 | return Command::SUCCESS; |
||
73 | } |
||
74 | |||
75 | if ($dumpSql) { |
||
76 | $io->text('The following SQL statements will be executed:'); |
||
77 | $io->newLine(); |
||
78 | |||
79 | foreach ($sqls as $queries) { |
||
80 | foreach ($queries as $sql) { |
||
81 | $io->text(sprintf(' %s;', $sql)); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if ($force) { |
||
87 | if ($dumpSql) { |
||
88 | $io->newLine(); |
||
89 | } |
||
90 | |||
91 | $io->text('Updating database schema...'); |
||
92 | $io->newLine(); |
||
93 | |||
94 | $progressBar = new ProgressBar($output, \count($sqls)); |
||
95 | $progressBar->start(); |
||
96 | |||
97 | $updateManager->updateAuditSchema($sqls, static function (array $progress) use ($progressBar): void { |
||
98 | $progressBar->advance(); |
||
99 | }); |
||
100 | |||
101 | $progressBar->finish(); |
||
102 | $io->newLine(2); |
||
103 | |||
104 | $pluralization = (1 === $count) ? 'query was' : 'queries were'; |
||
105 | |||
106 | $io->text(sprintf(' <info>%s</info> %s executed', $count, $pluralization)); |
||
107 | $io->success('Database schema updated successfully!'); |
||
108 | } |
||
109 | |||
110 | if ($dumpSql || $force) { |
||
111 | $this->release(); |
||
112 | |||
113 | return Command::SUCCESS; |
||
114 | } |
||
115 | |||
116 | $io->caution('This operation should not be executed in a production environment!'); |
||
117 | $io->text( |
||
118 | [ |
||
119 | sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', $count), |
||
120 | '', |
||
121 | 'Please run the operation by passing one - or both - of the following options:', |
||
122 | '', |
||
123 | sprintf(' <info>%s --force</info> to execute the command', $this->getName()), |
||
124 | sprintf(' <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()), |
||
125 | ] |
||
126 | ); |
||
127 | |||
128 | $this->release(); |
||
129 | |||
130 | return Command::FAILURE; |
||
131 | } |
||
133 |