| Conditions | 14 |
| Paths | 394 |
| Total Lines | 74 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 187 | protected function applyMigration(array $migration, $down = false, $fake = false) |
||
| 188 | { |
||
| 189 | $this->connection->beginTransaction(); |
||
| 190 | |||
| 191 | try { |
||
| 192 | /** @var $migrationObject AbstractMigration */ |
||
| 193 | $migrationObject = new $migration['class']($this->metadata, $this->outputWriter); |
||
| 194 | |||
| 195 | if ($migrationObject instanceof ServiceLocatorAwareInterface) { |
||
| 196 | if (is_null($this->serviceLocator)) { |
||
| 197 | throw new \RuntimeException( |
||
| 198 | sprintf( |
||
| 199 | 'Migration class %s requires a ServiceLocator, but there is no instance available.', |
||
| 200 | get_class($migrationObject) |
||
| 201 | ) |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | |||
| 205 | $migrationObject->setServiceLocator($this->serviceLocator); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($migrationObject instanceof AdapterAwareInterface) { |
||
| 209 | if (is_null($this->adapter)) { |
||
| 210 | throw new \RuntimeException( |
||
| 211 | sprintf( |
||
| 212 | 'Migration class %s requires an Adapter, but there is no instance available.', |
||
| 213 | get_class($migrationObject) |
||
| 214 | ) |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | $migrationObject->setDbAdapter($this->adapter); |
||
| 219 | } |
||
| 220 | |||
| 221 | $this->outputWriter->writeLine( |
||
| 222 | sprintf( |
||
| 223 | "%sExecute migration class %s %s", |
||
| 224 | $fake ? '[FAKE] ' : '', |
||
| 225 | $migration['class'], |
||
| 226 | $down ? 'down' : 'up' |
||
| 227 | ) |
||
| 228 | ); |
||
| 229 | |||
| 230 | if (!$fake) { |
||
| 231 | $sqlList = $down ? $migrationObject->getDownSql() : $migrationObject->getUpSql(); |
||
| 232 | foreach ($sqlList as $sql) { |
||
| 233 | $this->outputWriter->writeLine("Execute query:\n\n" . $sql); |
||
| 234 | $this->connection->execute($sql); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | if ($down) { |
||
| 239 | $this->migrationVersionTable->delete($migration['version']); |
||
| 240 | } else { |
||
| 241 | $this->migrationVersionTable->save($migration['version']); |
||
| 242 | } |
||
| 243 | $this->connection->commit(); |
||
| 244 | } catch (InvalidQueryException $e) { |
||
| 245 | $this->connection->rollback(); |
||
| 246 | $previousMessage = $e->getPrevious() ? $e->getPrevious()->getMessage() : null; |
||
| 247 | $msg = sprintf( |
||
| 248 | '%s: "%s"; File: %s; Line #%d', |
||
| 249 | $e->getMessage(), |
||
| 250 | $previousMessage, |
||
| 251 | $e->getFile(), |
||
| 252 | $e->getLine() |
||
| 253 | ); |
||
| 254 | throw new \Exception($msg, $e->getCode(), $e); |
||
| 255 | } catch (\Exception $e) { |
||
| 256 | $this->connection->rollback(); |
||
| 257 | $msg = sprintf('%s; File: %s; Line #%d', $e->getMessage(), $e->getFile(), $e->getLine()); |
||
| 258 | throw new \Exception($msg, $e->getCode(), $e); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: