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 | 9 | public function __construct($subject) |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 9 | public static function getCapabilities() |
|
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | 9 | public static function getPriority() |
|
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 4 | public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE) |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 2 | public function isAcquired() |
|
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | 1 | public function destroy() |
|
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | 1 | public function release() |
|
234 | |||
235 | /** |
||
236 | * Initialize the synchronization object, i.e. a simple list with some random element |
||
237 | * |
||
238 | * @return boolean TRUE on sucess, FALSE otherwise |
||
239 | */ |
||
240 | 5 | View Code Duplication | private function init() |
251 | |||
252 | /** |
||
253 | * Try to get the lock |
||
254 | * N.B. this a is non-blocking operation |
||
255 | * |
||
256 | * @return boolean TRUE on success, FALSE otherwise |
||
257 | */ |
||
258 | 4 | View Code Duplication | private function lock() |
265 | |||
266 | /** |
||
267 | * Wait for the lock being released |
||
268 | * N.B. this a is blocking operation |
||
269 | * |
||
270 | * @param int $blTo The blocking timeout |
||
271 | * @return string The popped value, FALSE otherwise |
||
272 | */ |
||
273 | 3 | private function wait($blTo) |
|
279 | |||
280 | /** |
||
281 | * Try to unlock the mutex and if succeeds, signal the waiting locks |
||
282 | * |
||
283 | * @return boolean TRUE on success, FALSE otherwise |
||
284 | */ |
||
285 | 1 | View Code Duplication | private function unlockAndSignal() |
296 | |||
297 | } |
||
298 |
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.