Conditions | 16 |
Paths | 404 |
Total Lines | 70 |
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 |
||
89 | public function run(bool $forceTransaction = false, bool $clean = true) |
||
90 | { |
||
91 | /** |
||
92 | * @var Driver[] $drivers |
||
93 | * @var CommandInterface[] $commands |
||
94 | */ |
||
95 | $drivers = $commands = []; |
||
96 | |||
97 | foreach ($this->getCommands() as $command) { |
||
98 | if ($command instanceof SQLCommandInterface) { |
||
99 | $driver = $command->getDriver(); |
||
100 | if (!empty($driver) && !in_array($driver, $drivers)) { |
||
101 | $drivers[] = $driver; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $commands[] = $command; |
||
106 | } |
||
107 | |||
108 | if (empty($commands)) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | //Commands we executed and drivers with started transactions |
||
113 | $executedCommands = $wrappedDrivers = []; |
||
114 | |||
115 | try { |
||
116 | if ($forceTransaction || count($commands) > 1) { |
||
117 | //Starting transactions |
||
118 | foreach ($drivers as $driver) { |
||
119 | $driver->beginTransaction(); |
||
120 | $wrappedDrivers[] = $driver; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | //Run commands |
||
125 | foreach ($commands as $command) { |
||
126 | $command->execute(); |
||
127 | $executedCommands[] = $command; |
||
128 | } |
||
129 | } catch (\Throwable $e) { |
||
130 | foreach (array_reverse($wrappedDrivers) as $driver) { |
||
131 | /** @var Driver $driver */ |
||
132 | $driver->rollbackTransaction(); |
||
133 | } |
||
134 | |||
135 | foreach (array_reverse($executedCommands) as $command) { |
||
136 | /** @var CommandInterface $command */ |
||
137 | $command->rollBack(); |
||
138 | } |
||
139 | |||
140 | $this->commands = []; |
||
141 | throw $e; |
||
142 | } |
||
143 | |||
144 | foreach (array_reverse($wrappedDrivers) as $driver) { |
||
145 | /** @var Driver $driver */ |
||
146 | $driver->commitTransaction(); |
||
147 | } |
||
148 | |||
149 | foreach ($executedCommands as $command) { |
||
150 | //This is the point when record will get related PK and FKs filled |
||
151 | $command->complete(); |
||
152 | } |
||
153 | |||
154 | //Clean transaction |
||
155 | if ($clean) { |
||
156 | $this->commands = []; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 |