Complex classes like ManagesTransactions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ManagesTransactions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait ManagesTransactions |
||
| 11 | { |
||
| 12 | protected $transactions = 0; |
||
| 13 | |||
| 14 | protected $transactionCommands = []; |
||
| 15 | |||
| 16 | protected $arangoTransaction; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Execute a Closure within a transaction. |
||
| 20 | * |
||
| 21 | * @param \Closure $callback |
||
| 22 | * @param array $options |
||
| 23 | * @param int $attempts |
||
| 24 | * @return mixed |
||
| 25 | * |
||
| 26 | * @throws \Exception|\Throwable |
||
| 27 | */ |
||
| 28 | public function transaction(Closure $callback, $options = [], $attempts = 1) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Start a new database transaction. |
||
| 39 | * |
||
| 40 | * @return void |
||
| 41 | * |
||
| 42 | * @throws \Exception |
||
| 43 | */ |
||
| 44 | public function beginTransaction() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Add a command to the transaction. Parameters must include: |
||
| 55 | * collections['write'][]: collections that are written to |
||
| 56 | * collections['read'][]: collections that are read from |
||
| 57 | * command: the db command to execute. |
||
| 58 | * |
||
| 59 | * @param \Illuminate\Support\Fluent $command |
||
| 60 | */ |
||
| 61 | public function addTransactionCommand(IlluminateFluent $command) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add a query command to the transaction. |
||
| 68 | * |
||
| 69 | * @param $query |
||
| 70 | * @param $bindings |
||
| 71 | * @param array|null $collections |
||
| 72 | * @return IlluminateFluent |
||
| 73 | */ |
||
| 74 | public function addQueryToTransaction($query, $bindings = [], $collections = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Transaction like a list of read collections to prevent possible read deadlocks. |
||
| 101 | * Transactions require a list of write collections to prepare write locks. |
||
| 102 | * |
||
| 103 | * @param $query |
||
| 104 | * @param $bindings |
||
| 105 | * @param $collections |
||
| 106 | * @return mixed |
||
| 107 | */ |
||
| 108 | public function extractTransactionCollections($query, $bindings, $collections) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Extract collections that are read from in a query. Not required but can prevent deadlocks. |
||
| 119 | * |
||
| 120 | * @param $query |
||
| 121 | * @param $bindings |
||
| 122 | * @param $collections |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public function extractReadCollections($query, $bindings, $collections) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Extract collections that are written to in a query. |
||
| 160 | * |
||
| 161 | * @param $query |
||
| 162 | * @param $bindings |
||
| 163 | * @param $collections |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | public function extractWriteCollections($query, $bindings, $collections) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the collection names that are bound in a query. |
||
| 186 | * |
||
| 187 | * @param $collections |
||
| 188 | * @param $bindings |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | public function getCollectionByBinding($collections, $bindings) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Commit the current transaction. |
||
| 204 | * |
||
| 205 | * @param array $options |
||
| 206 | * @param int $attempts |
||
| 207 | * @return mixed |
||
| 208 | * @throws Exception |
||
| 209 | */ |
||
| 210 | public function commit($options = [], $attempts = 1) |
||
| 229 | |||
| 230 | public function executeTransaction($options, $attempts = 1) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Handle an exception encountered when running a transacted statement. |
||
| 253 | * |
||
| 254 | * @param Exception $e |
||
| 255 | * @param integer $currentAttempt |
||
| 256 | * @param integer $attempts |
||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | protected function handleTransactionException($e, $currentAttempt, $attempts) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * compile an array of unique collections that are used to read from and/or write to. |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function compileTransactionCollections() |
||
| 328 | |||
| 329 | public function compileTransactionAction() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Handle an exception from a rollback. |
||
| 342 | * |
||
| 343 | * @param \Exception $e |
||
| 344 | * |
||
| 345 | * @throws \Exception |
||
| 346 | */ |
||
| 347 | protected function handleRollBackException($e) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the number of active transactions. |
||
| 358 | * |
||
| 359 | * @return int |
||
| 360 | */ |
||
| 361 | public function transactionLevel() |
||
| 365 | |||
| 366 | public function getTransactionCommands() |
||
| 370 | |||
| 371 | //Override unused trait transaction functions with dummy methods |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Dummy. |
||
| 375 | * |
||
| 376 | * @param $e |
||
| 377 | */ |
||
| 378 | public function handleBeginTransactionException($e) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Dummy override: Rollback the active database transaction. |
||
| 385 | * |
||
| 386 | * @param int|null $toLevel |
||
| 387 | * @return void |
||
| 388 | * |
||
| 389 | * @throws \Exception |
||
| 390 | */ |
||
| 391 | public function rollBack($toLevel = null) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Dummy override: ArangoDB rolls back the entire transaction on a failure. |
||
| 398 | * |
||
| 399 | * @param int $toLevel |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | protected function performRollBack($toLevel) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Create a save point within the database. |
||
| 409 | * Not supported by ArangoDB(?). |
||
| 410 | * |
||
| 411 | * @return void |
||
| 412 | */ |
||
| 413 | protected function createSavepoint() |
||
| 417 | } |
||
| 418 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.