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) |
|
156 | { |
||
157 | 4 | if ($this->isAcquired) { |
|
158 | return true; |
||
159 | } |
||
160 | |||
161 | 4 | if ($mode & self::LOCK_CAPABILITY_EXCLUSIVE) { |
|
162 | |||
163 | 4 | if ($mode & self::LOCK_CAPABILITY_NOBLOCK) { |
|
164 | |||
165 | // this does not block |
||
166 | 1 | $this->isAcquired = $this->lock(); |
|
167 | |||
168 | 1 | if (!$this->isAcquired) { |
|
169 | 1 | throw new LockAcquireWouldBlockException('could not acquire lock'); |
|
170 | } |
||
171 | } else { |
||
172 | |||
173 | // try to acquire the lock till $blTo is reached |
||
174 | 3 | $waited = 0; |
|
175 | do { |
||
176 | 3 | $start = time(); |
|
177 | |||
178 | // this blocks till the lock gets released |
||
179 | 3 | $this->wait($this->blTo - $waited); |
|
180 | |||
181 | 3 | $waited += time() - $start; |
|
182 | |||
183 | 3 | $this->isAcquired = $this->lock(); |
|
184 | |||
185 | 4 | } while (!$this->isAcquired && $waited < $this->blTo); |
|
186 | } |
||
187 | |||
188 | } else { |
||
189 | throw new LockAcquireException('insufficient capabilities'); |
||
190 | } |
||
191 | |||
192 | 4 | return true; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * @inheritdoc |
||
197 | */ |
||
198 | 1 | public function isAcquired() |
|
202 | |||
203 | /** |
||
204 | * @inheritdoc |
||
205 | */ |
||
206 | 1 | public function destroy() |
|
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | public function release() |
||
230 | |||
231 | /** |
||
232 | * Initialize the synchronization object, i.e. a simple list with some random element |
||
233 | * |
||
234 | * @return boolean TRUE on sucess, FALSE otherwise |
||
235 | */ |
||
236 | 5 | View Code Duplication | private function init() |
247 | |||
248 | /** |
||
249 | * Try to get the lock |
||
250 | * N.B. this a is non-blocking operation |
||
251 | * |
||
252 | * @return boolean TRUE on success, FALSE otherwise |
||
253 | */ |
||
254 | 4 | private function lock() |
|
261 | |||
262 | /** |
||
263 | * Wait for the lock being released |
||
264 | * N.B. this a is blocking operation |
||
265 | * |
||
266 | * @return string The popped value, FALSE otherwise |
||
267 | */ |
||
268 | 3 | private function wait() |
|
274 | |||
275 | /** |
||
276 | * Try to unlock the mutex and if succeeds, signal the waiting locks |
||
277 | * |
||
278 | * @return boolean TRUE on success, FALSE otherwise |
||
279 | */ |
||
280 | View Code Duplication | private function unlockAndSignal() |
|
291 | |||
292 | } |
||
293 |
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.