Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 6 | class ConnConfigTransactionWatcher implements ITransactionControlObserver |
||
| 7 | { |
||
| 8 | const SCOPE_TRANSACTION = 'trans'; |
||
| 9 | const SCOPE_SESSION = 'session'; |
||
| 10 | |||
| 11 | const RESET_ALL = ''; |
||
| 12 | |||
| 13 | private static $emptyStruct = [ |
||
| 14 | 'name' => null, |
||
| 15 | self::SCOPE_TRANSACTION => [], |
||
| 16 | self::SCOPE_SESSION => [], |
||
| 17 | ]; |
||
| 18 | |||
| 19 | private $connConfig; |
||
| 20 | |||
| 21 | /** @var bool whether currently in a transaction */ |
||
| 22 | private $inTrans = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array[]|null savepoints held within the current transaction; |
||
| 26 | * list: struct: |
||
| 27 | * 'name' => savepoint name, |
||
| 28 | * 'trans' => map: lower-cased names => original-cased names of properties the transaction-wide value |
||
| 29 | * a change of which has been saved by this savepoint, |
||
| 30 | * 'session' => similar for session-wide values; |
||
| 31 | * the list is sorted in the savepoint save order; |
||
| 32 | * the last struct always holds the currently unsaved values, its 'name' is null; |
||
| 33 | * <tt>null</tt> if not within transaction |
||
| 34 | */ |
||
| 35 | private $bySavepoint; |
||
| 36 | |||
| 37 | /** @var int index of the last struct in the $bySavepoint list */ |
||
| 38 | private $tailIdx; |
||
| 39 | |||
| 40 | |||
| 41 | public function __construct(IObservableConnConfig $connConfig) |
||
| 45 | |||
| 46 | |||
| 47 | public function handleSetForTransaction($propertyName) |
||
| 53 | |||
| 54 | public function handleSetForSession($propertyName) |
||
| 60 | |||
| 61 | public function handleResetAll() |
||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | //region ITransactionControlObserver |
||
| 71 | |||
| 72 | public function handleTransactionStart() |
||
| 78 | |||
| 79 | public function handleTransactionCommit() |
||
| 94 | |||
| 95 | public function handleTransactionRollback() |
||
| 117 | |||
| 118 | public function handleSavepointSaved($name) |
||
| 124 | |||
| 125 | public function handleSavepointReleased($name) |
||
| 140 | |||
| 141 | public function handleRollbackToSavepoint($name) |
||
| 165 | |||
| 166 | private function findSavepoint($name) |
||
| 175 | |||
| 176 | public function handleTransactionPrepared($name) |
||
| 180 | |||
| 181 | public function handlePreparedTransactionCommit($name) |
||
| 185 | |||
| 186 | public function handlePreparedTransactionRollback($name) |
||
| 190 | |||
| 191 | //endregion |
||
| 192 | } |
||
| 193 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.