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 |
||
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 | View Code Duplication | 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 | View Code Duplication | public function shouldConnectAndDestroyALock() |
|
159 | |||
160 | /** |
||
161 | * @test |
||
162 | */ |
||
163 | View Code Duplication | public function shouldConnectAndDestroyANotExistingLock() |
|
175 | |||
176 | /** |
||
177 | * @test |
||
178 | */ |
||
179 | public function shouldAcquireNonBlockingAndReleaseMoreLocks() |
||
180 | { |
||
181 | $subject = uniqid(); |
||
182 | $capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK; |
||
242 | |||
243 | protected function setUp() |
||
253 | |||
254 | protected function tearDown() |
||
260 | |||
261 | /** |
||
262 | * @return \Redis |
||
263 | */ |
||
264 | private function getRedisClient() |
||
272 | |||
273 | /** |
||
274 | * @param string $id Locker id |
||
275 | * |
||
276 | * @return \TYPO3\CMS\Core\Locking\LockingStrategyInterface |
||
277 | */ |
||
278 | private function getLocker($id) |
||
288 | |||
289 | } |
||
290 |
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.