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 |
||
| 46 | abstract class LockManager { |
||
| 47 | /** @var LoggerInterface */ |
||
| 48 | protected $logger; |
||
| 49 | |||
| 50 | /** @var array Mapping of lock types to the type actually used */ |
||
| 51 | protected $lockTypeMap = [ |
||
| 52 | self::LOCK_SH => self::LOCK_SH, |
||
| 53 | self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH |
||
| 54 | self::LOCK_EX => self::LOCK_EX |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** @var array Map of (resource path => lock type => count) */ |
||
| 58 | protected $locksHeld = []; |
||
| 59 | |||
| 60 | protected $domain; // string; domain (usually wiki ID) |
||
| 61 | protected $lockTTL; // integer; maximum time locks can be held |
||
| 62 | |||
| 63 | /** @var string Random 32-char hex number */ |
||
| 64 | protected $session; |
||
| 65 | |||
| 66 | /** Lock types; stronger locks have higher values */ |
||
| 67 | const LOCK_SH = 1; // shared lock (for reads) |
||
| 68 | const LOCK_UW = 2; // shared lock (for reads used to write elsewhere) |
||
| 69 | const LOCK_EX = 3; // exclusive lock (for writes) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Construct a new instance from configuration |
||
| 73 | * |
||
| 74 | * @param array $config Parameters include: |
||
| 75 | * - domain : Domain (usually wiki ID) that all resources are relative to [optional] |
||
| 76 | * - lockTTL : Age (in seconds) at which resource locks should expire. |
||
| 77 | * This only applies if locks are not tied to a connection/process. |
||
| 78 | */ |
||
| 79 | public function __construct( array $config ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Lock the resources at the given abstract paths |
||
| 101 | * |
||
| 102 | * @param array $paths List of resource names |
||
| 103 | * @param int $type LockManager::LOCK_* constant |
||
| 104 | * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21) |
||
| 105 | * @return StatusValue |
||
| 106 | */ |
||
| 107 | final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Lock the resources at the given abstract paths |
||
| 113 | * |
||
| 114 | * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths |
||
| 115 | * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21) |
||
| 116 | * @return StatusValue |
||
| 117 | * @since 1.22 |
||
| 118 | */ |
||
| 119 | final public function lockByType( array $pathsByType, $timeout = 0 ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Unlock the resources at the given abstract paths |
||
| 138 | * |
||
| 139 | * @param array $paths List of paths |
||
| 140 | * @param int $type LockManager::LOCK_* constant |
||
| 141 | * @return StatusValue |
||
| 142 | */ |
||
| 143 | final public function unlock( array $paths, $type = self::LOCK_EX ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Unlock the resources at the given abstract paths |
||
| 149 | * |
||
| 150 | * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths |
||
| 151 | * @return StatusValue |
||
| 152 | * @since 1.22 |
||
| 153 | */ |
||
| 154 | final public function unlockByType( array $pathsByType ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the base 36 SHA-1 of a string, padded to 31 digits. |
||
| 163 | * Before hashing, the path will be prefixed with the domain ID. |
||
| 164 | * This should be used interally for lock key or file names. |
||
| 165 | * |
||
| 166 | * @param string $path |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | final protected function sha1Base36Absolute( $path ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the base 16 SHA-1 of a string, padded to 31 digits. |
||
| 175 | * Before hashing, the path will be prefixed with the domain ID. |
||
| 176 | * This should be used interally for lock key or file names. |
||
| 177 | * |
||
| 178 | * @param string $path |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | final protected function sha1Base16Absolute( $path ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Normalize the $paths array by converting LOCK_UW locks into the |
||
| 187 | * appropriate type and removing any duplicated paths for each lock type. |
||
| 188 | * |
||
| 189 | * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths |
||
| 190 | * @return array |
||
| 191 | * @since 1.22 |
||
| 192 | */ |
||
| 193 | final protected function normalizePathsByType( array $pathsByType ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @see LockManager::lockByType() |
||
| 204 | * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths |
||
| 205 | * @return StatusValue |
||
| 206 | * @since 1.22 |
||
| 207 | */ |
||
| 208 | protected function doLockByType( array $pathsByType ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Lock resources with the given keys and lock type |
||
| 229 | * |
||
| 230 | * @param array $paths List of paths |
||
| 231 | * @param int $type LockManager::LOCK_* constant |
||
| 232 | * @return StatusValue |
||
| 233 | */ |
||
| 234 | abstract protected function doLock( array $paths, $type ); |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @see LockManager::unlockByType() |
||
| 238 | * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths |
||
| 239 | * @return StatusValue |
||
| 240 | * @since 1.22 |
||
| 241 | */ |
||
| 242 | View Code Duplication | protected function doUnlockByType( array $pathsByType ) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Unlock resources with the given keys and lock type |
||
| 253 | * |
||
| 254 | * @param array $paths List of paths |
||
| 255 | * @param int $type LockManager::LOCK_* constant |
||
| 256 | * @return StatusValue |
||
| 257 | */ |
||
| 258 | abstract protected function doUnlock( array $paths, $type ); |
||
| 259 | } |
||
| 260 |