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 |
||
21 | class CacheServiceDecorator implements TransactionAwareAdapterInterface |
||
22 | { |
||
23 | const SPI_CACHE_KEY_PREFIX = 'ez_spi'; |
||
24 | |||
25 | /** |
||
26 | * @var \Stash\Interfaces\PoolInterface |
||
27 | */ |
||
28 | protected $cachePool; |
||
29 | |||
30 | /** @var int */ |
||
31 | protected $transactionDepth = 0; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $deferredClearKeys = []; |
||
35 | |||
36 | /** |
||
37 | * Constructs the cache service decorator. |
||
38 | * |
||
39 | * @param \Stash\Interfaces\PoolInterface $cachePool |
||
40 | */ |
||
41 | public function __construct(PoolInterface $cachePool) |
||
45 | |||
46 | /** |
||
47 | * Prepend key with prefix and support array format Stash supported before. |
||
48 | * |
||
49 | * {@see \Psr\Cache\CacheItemPoolInterface} |
||
50 | * |
||
51 | * @internal param array|string $key , $key, $key... |
||
52 | * |
||
53 | * @return \Stash\Interfaces\ItemInterface |
||
54 | */ |
||
55 | public function getItem() |
||
78 | |||
79 | /** |
||
80 | * Prepend keys with prefix. |
||
81 | * |
||
82 | * {@see \Psr\Cache\CacheItemPoolInterface} |
||
83 | * |
||
84 | * @param array $keys |
||
85 | * @return \Stash\Interfaces\ItemInterface[] |
||
86 | */ |
||
87 | public function getItems(array $keys = []) |
||
103 | |||
104 | /** |
||
105 | * @param \Stash\Interfaces\ItemInterface[] $items |
||
106 | * @return \Stash\Interfaces\ItemInterface[] |
||
107 | */ |
||
108 | private function handleTransactionItems(array $items) |
||
134 | |||
135 | /** |
||
136 | * Remove slashes from start and end of keys, and for content replace it with _ to avoid issues for Stash. |
||
137 | * |
||
138 | * @param string $key |
||
139 | * @return string |
||
140 | */ |
||
141 | private function washKey($key) |
||
145 | |||
146 | /** |
||
147 | * Clears the cache for the key, or if none is specified clears the entire cache. The key can be either |
||
148 | * a series of string arguments, or an array. |
||
149 | * |
||
150 | * @internal |
||
151 | * @param array|null|string $key , $key, $key... |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function clear(...$key) |
||
167 | |||
168 | private function clearOrDefer(array $keys) |
||
179 | |||
180 | private function executeClear(array $keys) |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc}. |
||
194 | */ |
||
195 | public function beginTransaction() |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc}. |
||
206 | */ |
||
207 | public function commitTransaction() |
||
225 | |||
226 | /** |
||
227 | * {@inheritdoc}. |
||
228 | */ |
||
229 | public function rollbackTransaction() |
||
236 | } |
||
237 |
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.