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 |
||
| 41 | class RedisLockStrategy implements LockingStrategyInterface |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var \Redis A key-value data store |
||
| 45 | */ |
||
| 46 | private $redis; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string The locking subject, i.e. a string to discriminate the lock |
||
| 50 | */ |
||
| 51 | private $subject; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string The key used for the lock itself |
||
| 55 | */ |
||
| 56 | private $name; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string The key used for the mutex, i.e. a list |
||
| 60 | */ |
||
| 61 | private $mutex; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string The value used for the lock |
||
| 65 | */ |
||
| 66 | private $value; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var boolean TRUE if lock is acquired |
||
| 70 | */ |
||
| 71 | private $isAcquired = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var int Seconds the lock remains persistent |
||
| 75 | */ |
||
| 76 | private $ttl = 3600; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int Seconds to wait for a lock |
||
| 80 | */ |
||
| 81 | private $blTo = 60; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @inheritdoc |
||
| 85 | */ |
||
| 86 | 10 | public function __construct($subject) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @inheritdoc |
||
| 134 | */ |
||
| 135 | 10 | public static function getCapabilities() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritdoc |
||
| 142 | */ |
||
| 143 | 10 | public static function getPriority() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | 2 | public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | */ |
||
| 189 | 1 | public function isAcquired() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @inheritdoc |
||
| 196 | */ |
||
| 197 | 2 | public function destroy() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @inheritdoc |
||
| 205 | */ |
||
| 206 | public function release() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Initialize the synchronization object, i.e. a simple list with some random element |
||
| 224 | * |
||
| 225 | * @return boolean TRUE on sucess, FALSE otherwise |
||
| 226 | */ |
||
| 227 | 6 | View Code Duplication | private function init() |
| 238 | |||
| 239 | /** |
||
| 240 | * Try to get the lock |
||
| 241 | * N.B. this operation is non-blocking |
||
| 242 | * |
||
| 243 | * @return boolean TRUE on success, FALSE otherwise |
||
| 244 | */ |
||
| 245 | 1 | private function lock() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Wait for the lock beeing released |
||
| 255 | * |
||
| 256 | * @return string The value, FALSE otherwise |
||
| 257 | */ |
||
| 258 | 2 | private function wait() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Try to unlock the mutex and if succeeds, signal the waiting locks |
||
| 265 | * |
||
| 266 | * @return boolean TRUE on success, FALSE otherwise |
||
| 267 | */ |
||
| 268 | View Code Duplication | private function unlockAndSignal() |
|
| 279 | |||
| 280 | } |
||
| 281 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.