| Conditions | 8 |
| Paths | 36 |
| Total Lines | 63 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| Bugs | 2 | Features | 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 |
||
| 102 | protected function executeSqlStatements($sqlStatements, $fileName, OutputInterface $output) |
||
| 103 | { |
||
| 104 | $replacements = [ |
||
| 105 | ';' => '', |
||
| 106 | '{database}' => $this->getDic()['Yapeal.Sql.database'], |
||
| 107 | '{engine}' => $this->getDic()['Yapeal.Sql.engine'], |
||
| 108 | '{ engine}' => $this->getDic()['Yapeal.Sql.engine'], |
||
| 109 | '{table_prefix}' => $this->getDic()['Yapeal.Sql.tablePrefix'], |
||
| 110 | '$$' => ';' |
||
| 111 | ]; |
||
| 112 | $pdo = $this->getPdo(); |
||
| 113 | // Split up SQL into statements on ';'. |
||
| 114 | // Replace {database}, {table_prefix}, {engine}, ';', and '$$' in statements. |
||
| 115 | /** |
||
| 116 | * @var string[] $statements |
||
| 117 | */ |
||
| 118 | $statements = str_replace(array_keys($replacements), array_values($replacements), explode(';', $sqlStatements)); |
||
| 119 | // 5 is a 'magic' number that I think is shorter than any legal SQL statement. |
||
| 120 | $statements = array_filter($statements, function ($value) { |
||
| 121 | return 5 <= strlen(trim($value)); |
||
| 122 | }); |
||
| 123 | $progress = null; |
||
| 124 | if ($output::VERBOSITY_QUIET !== $output->getVerbosity()) { |
||
| 125 | if (false === strpos($fileName, '::')) { |
||
| 126 | $mess = sprintf('<info>Execute %1$s/%2$s</info>', basename(dirname($fileName)), |
||
| 127 | basename($fileName, '.sql')); |
||
| 128 | } else { |
||
| 129 | $mess = sprintf('<info>Execute %s</info>', $fileName); |
||
| 130 | } |
||
| 131 | $output->writeln($mess); |
||
| 132 | $progress = $this->createProgressBar($output, count($statements)); |
||
| 133 | } |
||
| 134 | foreach ($statements as $statement => $sql) { |
||
| 135 | $sql = trim($sql); |
||
| 136 | try { |
||
| 137 | $pdo->exec($sql); |
||
| 138 | if (null !== $progress) { |
||
| 139 | $progress->setMessage('<comment>executing</comment>'); |
||
| 140 | $progress->advance(); |
||
| 141 | } |
||
| 142 | } catch (\PDOException $exc) { |
||
| 143 | if (null !== $progress) { |
||
| 144 | $progress->setMessage('<error>Failed</error>'); |
||
| 145 | $progress->finish(); |
||
| 146 | $output->writeln(''); |
||
| 147 | } |
||
| 148 | $mess = $sql . PHP_EOL; |
||
| 149 | $mess .= sprintf( |
||
| 150 | 'Sql failed in %1$s on statement %2$s with (%3$s) %4$s', |
||
| 151 | $fileName, |
||
| 152 | $statement, |
||
| 153 | $exc->getCode(), |
||
| 154 | $exc->getMessage() |
||
| 155 | ); |
||
| 156 | throw new YapealDatabaseException($mess, 2); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | if (null !== $progress) { |
||
| 160 | $progress->setMessage(''); |
||
| 161 | $progress->finish(); |
||
| 162 | $output->writeln(''); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | /** |
||
| 206 |