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 declare(strict_types=1); |
||
32 | trait RedisTaggedCacheTrait |
||
33 | { |
||
34 | /** |
||
35 | * @var Redis |
||
36 | */ |
||
37 | private $redisInstance; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $internalKeysPrefix = '_:k:'; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $internalTagsPrefix = '_:t:'; |
||
48 | /** |
||
49 | * @return Redis |
||
50 | */ |
||
51 | 6 | protected function getRedisInstance(): Redis |
|
57 | |||
58 | /** |
||
59 | * @param Redis $redisInstance |
||
60 | */ |
||
61 | 6 | protected function setRedisInstance(Redis $redisInstance): void |
|
65 | |||
66 | /** @noinspection PhpDocRedundantThrowsInspection |
||
67 | * @param string $key |
||
68 | * @param string $value |
||
69 | * @param array $tags |
||
70 | * @param int $ttl |
||
71 | * |
||
72 | * @return void |
||
73 | * |
||
74 | * @throws RedisTaggedCacheException |
||
75 | */ |
||
76 | 2 | public function addTaggedValue(string $key, string $value, array $tags, $ttl = 0): void |
|
93 | |||
94 | /** @noinspection PhpDocRedundantThrowsInspection |
||
95 | * @param string $key |
||
96 | * |
||
97 | * @return void |
||
98 | * |
||
99 | * @throws RedisTaggedCacheException |
||
100 | */ |
||
101 | 2 | View Code Duplication | public function removeTaggedValue(string $key): void |
115 | |||
116 | /** @noinspection PhpDocRedundantThrowsInspection |
||
117 | * @param string $tag |
||
118 | * |
||
119 | * @return void |
||
120 | * |
||
121 | * @throws RedisTaggedCacheException |
||
122 | */ |
||
123 | 2 | View Code Duplication | public function invalidateTag(string $tag): void |
137 | |||
138 | /** |
||
139 | * @param int $scriptIndex |
||
140 | * @param array $arguments |
||
141 | * @param int $keysInArgs |
||
142 | * |
||
143 | * @return bool |
||
144 | * |
||
145 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
146 | */ |
||
147 | 6 | protected function evalScript(int $scriptIndex, array $arguments, int $keysInArgs): bool |
|
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | 6 | protected function getInternalKeysPrefix(): string |
|
173 | |||
174 | /** |
||
175 | * @param string $internalKeysPrefix |
||
176 | */ |
||
177 | 6 | protected function setInternalKeysPrefix(string $internalKeysPrefix): void |
|
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | 6 | protected function getInternalTagsPrefix(): string |
|
189 | |||
190 | /** |
||
191 | * @param string $internalTagsPrefix |
||
192 | */ |
||
193 | 6 | protected function setInternalTagsPrefix(string $internalTagsPrefix): void |
|
197 | } |
||
198 |
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.