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 |
||
42 | */ |
||
43 | class RedisLockStrategyTest extends FunctionalTestCase |
||
44 | { |
||
45 | /** |
||
46 | * @var LockFactory |
||
47 | */ |
||
48 | private $lockFactory; |
||
49 | private $redisHost; |
||
50 | private $redisDatabase; |
||
51 | |||
52 | /** |
||
53 | * @test |
||
54 | * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
||
55 | * @expectedExceptionMessage no configuration for redis lock strategy found |
||
56 | */ |
||
57 | public function shouldThrowExceptionBecauseConfigIsMissing() |
||
58 | { |
||
59 | $this->lockFactory->createLocker('test'); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @test |
||
64 | * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
||
65 | * @expectedExceptionMessage no configuration for redis lock strategy found |
||
66 | */ |
||
67 | public function shouldThrowExceptionBecauseConfigIsNotAnArray() |
||
68 | { |
||
69 | $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = 'test'; |
||
70 | $this->lockFactory->createLocker('test'); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @test |
||
75 | * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
||
76 | * @expectedExceptionMessage no host for redis lock strategy found |
||
77 | */ |
||
78 | public function shouldThrowExceptionBecauseConfigHasNoHost() |
||
79 | { |
||
80 | $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = []; |
||
81 | |||
82 | $this->lockFactory->createLocker('test'); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @test |
||
87 | * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException |
||
88 | * @expectedExceptionMessage no database for redis lock strategy found |
||
89 | */ |
||
90 | public function shouldThrowExceptionBecauseConfigHasNoDatabase() |
||
91 | { |
||
92 | $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [ |
||
93 | 'host' => $this->redisHost, |
||
94 | ]; |
||
95 | |||
96 | $this->lockFactory->createLocker('test'); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @test |
||
101 | */ |
||
102 | public function shouldConnectAndAcquireAExistingLock() |
||
103 | { |
||
104 | $subject = uniqid(); |
||
105 | |||
106 | $locker = $this->getLocker($subject); |
||
107 | |||
108 | self::assertTrue($locker->acquire()); |
||
109 | } |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @test |
||
114 | */ |
||
115 | public function shouldConnectAndAcquireALock() |
||
116 | { |
||
117 | $subject = uniqid(); |
||
118 | |||
119 | $locker = $this->getLocker($subject); |
||
120 | |||
121 | $redis = $this->getRedisClient(); |
||
122 | |||
123 | self::assertTrue($redis->exists($subject)); |
||
124 | |||
125 | self::assertTrue($locker->acquire()); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @test |
||
130 | */ |
||
131 | public function shouldConnectAndCheckIfLockIsAcquired() |
||
132 | { |
||
133 | $subject = uniqid(); |
||
134 | |||
135 | $locker = $this->getLocker($subject); |
||
136 | |||
137 | $locker->acquire(); |
||
138 | |||
139 | self::assertTrue($locker->isAcquired()); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @test |
||
144 | */ |
||
145 | public function shouldConnectAndDestroyALock() |
||
146 | { |
||
147 | $subject = uniqid(); |
||
148 | |||
149 | $locker = $this->getLocker($subject); |
||
150 | |||
151 | $redis = $this->getRedisClient(); |
||
152 | |||
153 | $redis->set($subject, 'testvalue'); |
||
154 | |||
155 | $locker->destroy(); |
||
156 | |||
157 | self::assertFalse($redis->exists($subject)); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @test |
||
162 | */ |
||
163 | public function shouldConnectAndDestroyANotExistingLock() |
||
164 | { |
||
165 | $subject = uniqid(); |
||
166 | |||
167 | $locker = $this->getLocker($subject); |
||
168 | |||
169 | $redis = $this->getRedisClient(); |
||
170 | |||
171 | $locker->destroy(); |
||
172 | |||
173 | self::assertFalse($redis->exists($subject)); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @test |
||
178 | */ |
||
179 | public function shouldAcquireNonBlockingAndReleaseMoreLocks() |
||
180 | { |
||
181 | $subject = uniqid(); |
||
|
|||
182 | $capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK; |
||
183 | |||
184 | $locker1 = $this->getLocker($subject); |
||
185 | $locker2 = $this->getLocker($subject); |
||
186 | $locker3 = $this->getLocker($subject); |
||
187 | |||
188 | self::assertTrue($locker1->acquire($capabilities)); |
||
189 | self::expectException('\TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException') && $locker2->acquire($capabilities); |
||
190 | self::expectException('\TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException') && $locker3->acquire($capabilities); |
||
191 | |||
192 | self::assertTrue($locker1->isAcquired()); |
||
193 | self::assertFalse($locker2->isAcquired()); |
||
194 | self::assertFalse($locker3->isAcquired()); |
||
195 | |||
196 | self::assertTrue($locker1->release()); |
||
197 | self::assertTrue($locker2->acquire($capabilities)); |
||
198 | self::assertTrue($locker3->release()); |
||
199 | |||
200 | self::assertTrue($locker1->acquire($capabilities); |
||
201 | self::assertTrue($locker2->release()); |
||
202 | self::assertTrue($locker3->acquire($capabilities)); |
||
203 | |||
204 | self::assertTrue($locker1->isAcquired()); |
||
205 | self::assertFalse($locker2->isAcquired()); |
||
206 | self::assertTrue($locker3->isAcquired()); |
||
207 | } |
||
208 | |||
209 | protected function setUp() |
||
210 | { |
||
211 | $this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy'; |
||
212 | $this->redisHost = getenv('typo3RedisHost'); |
||
213 | $this->redisDatabase = getenv('typo3RedisDatabase'); |
||
214 | |||
215 | parent::setUp(); |
||
216 | |||
217 | $this->lockFactory = GeneralUtility::makeInstance(LockFactory::class); |
||
218 | } |
||
219 | |||
220 | protected function tearDown() |
||
221 | { |
||
222 | parent::tearDown(); |
||
223 | |||
224 | $this->getRedisClient()->flushDB(); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return \Redis |
||
229 | */ |
||
256 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.