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[]|IDatabase[] Map of (DB names => server config or IDatabase) */ |
||
| 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 | /** @var IDatabase[] Map Database connections (DB name => Database) */ |
||
| 47 | protected $conns = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Construct a new instance from configuration. |
||
| 51 | * |
||
| 52 | * @param array $config Parameters include: |
||
| 53 | * - dbServers : Associative array of DB names to server configuration. |
||
| 54 | * Configuration is an associative array that includes: |
||
| 55 | * - host : DB server name |
||
| 56 | * - dbname : DB name |
||
| 57 | * - type : DB type (mysql,postgres,...) |
||
| 58 | * - user : DB user |
||
| 59 | * - password : DB user password |
||
| 60 | * - tablePrefix : DB table prefix |
||
| 61 | * - flags : DB flags; bitfield of IDatabase::DBO_* constants |
||
| 62 | * - dbsByBucket : Array of 1-16 consecutive integer keys, starting from 0, |
||
| 63 | * each having an odd-numbered list of DB names (peers) as values. |
||
| 64 | * - lockExpiry : Lock timeout (seconds) for dropped connections. [optional] |
||
| 65 | * This tells the DB server how long to wait before assuming |
||
| 66 | * connection failure and releasing all the locks for a session. |
||
| 67 | * - srvCache : A BagOStuff instance using APC or the like. |
||
| 68 | */ |
||
| 69 | public function __construct( array $config ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @TODO change this code to work in one batch |
||
| 96 | * @param string $lockSrv |
||
| 97 | * @param array $pathsByType |
||
| 98 | * @return StatusValue |
||
| 99 | */ |
||
| 100 | View Code Duplication | protected function getLocksOnServer( $lockSrv, array $pathsByType ) { |
|
| 108 | |||
| 109 | abstract protected function doGetLocksOnServer( $lockSrv, array $paths, $type ); |
||
| 110 | |||
| 111 | protected function freeLocksOnServer( $lockSrv, array $pathsByType ) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @see QuorumLockManager::isServerUp() |
||
| 117 | * @param string $lockSrv |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | protected function isServerUp( $lockSrv ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get (or reuse) a connection to a lock DB |
||
| 137 | * |
||
| 138 | * @param string $lockDb |
||
| 139 | * @return IDatabase |
||
| 140 | * @throws DBError |
||
| 141 | * @throws UnexpectedValueException |
||
| 142 | */ |
||
| 143 | protected function getConnection( $lockDb ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Do additional initialization for new lock DB connection |
||
| 175 | * |
||
| 176 | * @param string $lockDb |
||
| 177 | * @param IDatabase $db |
||
| 178 | * @throws DBError |
||
| 179 | */ |
||
| 180 | protected function initConnection( $lockDb, IDatabase $db ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Checks if the DB has not recently had connection/query errors. |
||
| 185 | * This just avoids wasting time on doomed connection attempts. |
||
| 186 | * |
||
| 187 | * @param string $lockDb |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | protected function cacheCheckFailures( $lockDb ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Log a lock request failure to the cache |
||
| 198 | * |
||
| 199 | * @param string $lockDb |
||
| 200 | * @return bool Success |
||
| 201 | */ |
||
| 202 | protected function cacheRecordFailure( $lockDb ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get a cache key for recent query misses for a DB |
||
| 210 | * |
||
| 211 | * @param string $lockDb |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | protected function getMissKey( $lockDb ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Make sure remaining locks get cleared for sanity |
||
| 220 | */ |
||
| 221 | function __destruct() { |
||
| 227 | } |
||
| 228 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.