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) |
|
87 | { |
||
88 | 9 | $config = null; |
|
89 | |||
90 | 9 | if (\array_key_exists('redis_lock', $GLOBALS['TYPO3_CONF_VARS']['SYS'])) { |
|
91 | 8 | $config = $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock']; |
|
92 | } |
||
93 | |||
94 | 9 | if (!\is_array($config)) { |
|
95 | 2 | throw new LockCreateException('no configuration for redis lock strategy found'); |
|
96 | } |
||
97 | |||
98 | 7 | if (!\array_key_exists('host', $config)) { |
|
99 | 1 | throw new LockCreateException('no host for redis lock strategy found'); |
|
100 | } |
||
101 | 6 | $port = 6379; |
|
102 | |||
103 | 6 | if (\array_key_exists('port', $config)) { |
|
104 | 5 | $port = (int) $config['port']; |
|
105 | } |
||
106 | |||
107 | 6 | if (!\array_key_exists('database', $config)) { |
|
108 | 1 | throw new LockCreateException('no database for redis lock strategy found'); |
|
109 | } |
||
110 | |||
111 | 5 | if (\array_key_exists('ttl', $config)) { |
|
112 | $this->ttl = (int) $config['ttl']; |
||
113 | } |
||
114 | |||
115 | 5 | $this->redis = new \Redis(); |
|
|
|||
116 | 5 | $this->redis->connect($config['host'], $port); |
|
117 | |||
118 | 5 | if (\array_key_exists('auth', $config)) { |
|
119 | $this->redis->auth($config['auth']); |
||
120 | } |
||
121 | |||
122 | 5 | $this->redis->select((int) $config['database']); |
|
123 | |||
124 | 5 | $this->subject = $subject; |
|
125 | 5 | $this->name = sprintf('lock:name:%s', $subject); |
|
126 | 5 | $this->mutex = sprintf('lock:mutex:%s', $subject); |
|
127 | 5 | $this->value = uniqid(); |
|
128 | |||
129 | 5 | $this->init(); |
|
130 | 5 | } |
|
131 | |||
132 | /** |
||
133 | * @inheritdoc |
||
134 | */ |
||
135 | 9 | public static function getCapabilities() |
|
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | 9 | public static function getPriority() |
|
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | 2 | public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE) |
|
152 | { |
||
153 | 2 | if ($this->isAcquired) { |
|
154 | return true; |
||
155 | } |
||
156 | |||
157 | 2 | if ($mode & self::LOCK_CAPABILITY_EXCLUSIVE) { |
|
158 | |||
159 | 2 | if ($mode & self::LOCK_CAPABILITY_NOBLOCK) { |
|
160 | |||
161 | // this does not block |
||
162 | $this->isAcquired = $this->lock(); |
||
163 | |||
164 | if (!$this->isAcquired) { |
||
165 | throw new LockAcquireWouldBlockException('could not acquire lock'); |
||
166 | } |
||
167 | } else { |
||
168 | |||
169 | // this blocks till the lock gets released |
||
170 | 2 | $this->wait(); |
|
171 | |||
172 | $this->isAcquired = $this->lock(); |
||
173 | |||
174 | if (!$this->isAcquired) { |
||
175 | throw new LockAcquireException('could not acquire lock'); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | } else { |
||
180 | throw new LockAcquireException('insufficient capabilities'); |
||
181 | } |
||
182 | |||
183 | return true; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @inheritdoc |
||
188 | */ |
||
189 | public function isAcquired() |
||
190 | { |
||
191 | return $this->isAcquired; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | 1 | 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 | 5 | View Code Duplication | private function init() |
238 | |||
239 | /** |
||
240 | * Try to get the lock |
||
241 | * N.B. this a is non-blocking operation |
||
242 | * |
||
243 | * @return boolean TRUE on success, FALSE otherwise |
||
244 | */ |
||
245 | private function lock() |
||
246 | { |
||
247 | $this->value = uniqid(); |
||
248 | |||
249 | // option NX: set value iff key is not present |
||
250 | return $this->redis->set($this->name, $this->value, ['NX', 'PX' => $this->ttl]); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Wait for the lock being released |
||
255 | * N.B. this a is blocking operation |
||
256 | * |
||
257 | * @return string The value, FALSE otherwise |
||
258 | */ |
||
259 | 2 | private function wait() |
|
263 | |||
264 | /** |
||
265 | * Try to unlock the mutex and if succeeds, signal the waiting locks |
||
266 | * |
||
267 | * @return boolean TRUE on success, FALSE otherwise |
||
268 | */ |
||
269 | View Code Duplication | private function unlockAndSignal() |
|
280 | |||
281 | } |
||
282 |
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.