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 |
||
| 38 | abstract class DBLockManager extends QuorumLockManager { |
||
| 39 | /** @var array[] Map of DB names to server config */ |
||
| 40 | protected $dbServers; // (DB name => server config array) |
||
| 41 | /** @var BagOStuff */ |
||
| 42 | protected $statusCache; |
||
| 43 | |||
| 44 | protected $lockExpiry; // integer number of seconds |
||
| 45 | protected $safeDelay; // integer number of seconds |
||
| 46 | |||
| 47 | protected $session = 0; // random integer |
||
| 48 | /** @var IDatabase[] Map Database connections (DB name => Database) */ |
||
| 49 | protected $conns = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Construct a new instance from configuration. |
||
| 53 | * |
||
| 54 | * @param array $config Parameters include: |
||
| 55 | * - dbServers : Associative array of DB names to server configuration. |
||
| 56 | * Configuration is an associative array that includes: |
||
| 57 | * - host : DB server name |
||
| 58 | * - dbname : DB name |
||
| 59 | * - type : DB type (mysql,postgres,...) |
||
| 60 | * - user : DB user |
||
| 61 | * - password : DB user password |
||
| 62 | * - tablePrefix : DB table prefix |
||
| 63 | * - flags : DB flags (see DatabaseBase) |
||
| 64 | * - dbsByBucket : Array of 1-16 consecutive integer keys, starting from 0, |
||
| 65 | * each having an odd-numbered list of DB names (peers) as values. |
||
| 66 | * Any DB named 'localDBMaster' will automatically use the DB master |
||
| 67 | * settings for this wiki (without the need for a dbServers entry). |
||
| 68 | * Only use 'localDBMaster' if the domain is a valid wiki ID. |
||
| 69 | * - lockExpiry : Lock timeout (seconds) for dropped connections. [optional] |
||
| 70 | * This tells the DB server how long to wait before assuming |
||
| 71 | * connection failure and releasing all the locks for a session. |
||
| 72 | */ |
||
| 73 | public function __construct( array $config ) { |
||
| 104 | |||
| 105 | // @todo change this code to work in one batch |
||
| 106 | View Code Duplication | protected function getLocksOnServer( $lockSrv, array $pathsByType ) { |
|
| 114 | |||
| 115 | abstract protected function doGetLocksOnServer( $lockSrv, array $paths, $type ); |
||
| 116 | |||
| 117 | protected function freeLocksOnServer( $lockSrv, array $pathsByType ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @see QuorumLockManager::isServerUp() |
||
| 123 | * @param string $lockSrv |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | protected function isServerUp( $lockSrv ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get (or reuse) a connection to a lock DB |
||
| 143 | * |
||
| 144 | * @param string $lockDb |
||
| 145 | * @return IDatabase |
||
| 146 | * @throws DBError |
||
| 147 | * @throws UnexpectedValueException |
||
| 148 | */ |
||
| 149 | protected function getConnection( $lockDb ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return LoadBalancer |
||
| 186 | */ |
||
| 187 | protected function getLocalLB() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Do additional initialization for new lock DB connection |
||
| 193 | * |
||
| 194 | * @param string $lockDb |
||
| 195 | * @param IDatabase $db |
||
| 196 | * @throws DBError |
||
| 197 | */ |
||
| 198 | protected function initConnection( $lockDb, IDatabase $db ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Checks if the DB has not recently had connection/query errors. |
||
| 203 | * This just avoids wasting time on doomed connection attempts. |
||
| 204 | * |
||
| 205 | * @param string $lockDb |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | protected function cacheCheckFailures( $lockDb ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Log a lock request failure to the cache |
||
| 216 | * |
||
| 217 | * @param string $lockDb |
||
| 218 | * @return bool Success |
||
| 219 | */ |
||
| 220 | protected function cacheRecordFailure( $lockDb ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get a cache key for recent query misses for a DB |
||
| 228 | * |
||
| 229 | * @param string $lockDb |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function getMissKey( $lockDb ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Make sure remaining locks get cleared for sanity |
||
| 239 | */ |
||
| 240 | function __destruct() { |
||
| 246 | } |
||
| 247 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.