| Conditions | 10 |
| Paths | 80 |
| Total Lines | 50 |
| Code Lines | 24 |
| 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 |
||
| 76 | public function run() |
||
| 77 | { |
||
| 78 | /** |
||
| 79 | * @var Driver[] $drivers |
||
| 80 | * @var CommandInterface[] $executedCommands |
||
| 81 | */ |
||
| 82 | $drivers = []; |
||
| 83 | $executedCommands = []; |
||
| 84 | |||
| 85 | try { |
||
| 86 | foreach ($this->getCommands() as $command) { |
||
| 87 | if ($command instanceof TransactionInterface) { |
||
| 88 | //All transaction commands are flatten (see getCommands() method) |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($command instanceof SQLCommandInterface) { |
||
| 93 | $driver = $command->getDriver(); |
||
| 94 | |||
| 95 | if (in_array($driver, $drivers)) { |
||
| 96 | //Command requires DBAL driver to open transaction |
||
| 97 | $drivers[] = $driver; |
||
| 98 | $driver->beginTransaction(); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | //Execute command |
||
| 103 | $command->execute(); |
||
| 104 | $executedCommands[] = $command; |
||
| 105 | } |
||
| 106 | } catch (\Throwable $e) { |
||
| 107 | foreach (array_reverse($drivers) as $driver) { |
||
| 108 | $driver->rollbackTransaction(); |
||
| 109 | } |
||
| 110 | |||
| 111 | foreach (array_reverse($executedCommands) as $command) { |
||
| 112 | $command->rollBack(); |
||
| 113 | } |
||
| 114 | |||
| 115 | throw $e; |
||
| 116 | } |
||
| 117 | |||
| 118 | foreach ($drivers as $driver) { |
||
| 119 | $driver->commitTransaction(); |
||
| 120 | } |
||
| 121 | |||
| 122 | foreach ($executedCommands as $command) { |
||
| 123 | $command->complete(); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |