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 by this locker |
||
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) |
|
87 | { |
||
88 | 9 | $config = null; |
|
89 | 9 | View Code Duplication | if (\array_key_exists('redis_lock', $GLOBALS['TYPO3_CONF_VARS']['SYS'])) { |
|
|||
90 | 8 | $config = $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock']; |
|
91 | } |
||
92 | |||
93 | 9 | if (!\is_array($config)) { |
|
94 | 2 | throw new LockCreateException('no configuration for redis lock strategy found'); |
|
95 | } |
||
96 | |||
97 | 7 | if (!\array_key_exists('host', $config)) { |
|
98 | 1 | throw new LockCreateException('no host for redis lock strategy found'); |
|
99 | } |
||
100 | |||
101 | 6 | $port = 6379; |
|
102 | 6 | if (\array_key_exists('port', $config)) { |
|
103 | $port = (int) $config['port']; |
||
104 | } |
||
105 | |||
106 | 6 | if (!\array_key_exists('database', $config)) { |
|
107 | 1 | throw new LockCreateException('no database for redis lock strategy found'); |
|
108 | } |
||
109 | |||
110 | 5 | if (\array_key_exists('ttl', $config)) { |
|
111 | $this->ttl = (int) $config['ttl']; |
||
112 | } |
||
113 | |||
114 | 5 | if (\array_key_exists('blTo', $config)) { |
|
115 | $this->blTo = (int) $config['blTo']; |
||
116 | } |
||
117 | |||
118 | 5 | $this->redis = new \Redis(); |
|
119 | 5 | $this->redis->connect($config['host'], $port); |
|
120 | 5 | if (\array_key_exists('auth', $config)) { |
|
121 | $this->redis->auth($config['auth']); |
||
122 | } |
||
123 | 5 | $this->redis->select((int) $config['database']); |
|
124 | |||
125 | 5 | $this->subject = $subject; |
|
126 | 5 | $this->name = sprintf('lock:name:%s', $subject); |
|
127 | 5 | $this->mutex = sprintf('lock:mutex:%s', $subject); |
|
128 | 5 | $this->value = uniqid(); |
|
129 | 5 | } |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 9 | public static function getCapabilities() |
|
138 | |||
139 | /** |
||
140 | * @inheritdoc |
||
141 | */ |
||
142 | 9 | public static function getPriority() |
|
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | 4 | public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE) |
|
187 | |||
188 | /** |
||
189 | * @inheritdoc |
||
190 | */ |
||
191 | 2 | public function isAcquired() |
|
195 | |||
196 | /** |
||
197 | * @inheritdoc |
||
198 | */ |
||
199 | 1 | public function destroy() |
|
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | 2 | public function release() |
|
221 | |||
222 | /** |
||
223 | * Try to get the lock |
||
224 | * N.B. this a is non-blocking operation |
||
225 | * |
||
226 | * @return boolean TRUE on success, FALSE otherwise |
||
227 | */ |
||
228 | 4 | View Code Duplication | private function lock() |
235 | |||
236 | /** |
||
237 | * Wait for the lock being released |
||
238 | * N.B. this a is blocking operation |
||
239 | * |
||
240 | * @param int $blTo The blocking timeout in seconds |
||
241 | * @return string The popped value, FALSE on timeout |
||
242 | */ |
||
243 | private function wait($blTo) |
||
249 | |||
250 | /** |
||
251 | * Try to unlock the mutex and if succeeds, signal the waiting locks |
||
252 | * N.B. by using EVAL we enforce transactional behaviour |
||
253 | * |
||
254 | * @return boolean TRUE on success, FALSE otherwise |
||
255 | */ |
||
256 | 1 | View Code Duplication | private function unlockAndSignal() |
267 | |||
268 | } |
||
269 |
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.