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 |
||
| 22 | class TransactionalInMemoryCacheAdapter implements TransactionAwareAdapterInterface |
||
| 23 | { |
||
| 24 | /** @var \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface */ |
||
| 25 | protected $sharedPool; |
||
| 26 | |||
| 27 | /** @var \eZ\Publish\Core\Persistence\Cache\inMemory\InMemoryCache[] */ |
||
| 28 | private $inMemoryPools; |
||
| 29 | |||
| 30 | /** @var int */ |
||
| 31 | protected $transactionDepth; |
||
| 32 | |||
| 33 | /** @var array To be unique and simplify lookup hash key is cache tag, value is only true value */ |
||
| 34 | protected $deferredTagsInvalidation; |
||
| 35 | |||
| 36 | /** @var array To be unique and simplify lookup hash key is cache key, value is only true value */ |
||
| 37 | protected $deferredItemsDeletion; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface $sharedPool |
||
| 41 | * @param \eZ\Publish\Core\Persistence\Cache\inMemory\InMemoryCache[] $inMemoryPools |
||
| 42 | * @param int $transactionDepth |
||
| 43 | * @param array $deferredTagsInvalidation |
||
| 44 | * @param array $deferredItemsDeletion |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function getItem($key) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function getItems(array $keys = []) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function hasItem($key) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function deleteItem($key) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function deleteItems(array $keys) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function invalidateTags(array $tags) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function clear() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | public function save(CacheItemInterface $item) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | public function beginTransaction(): void |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | */ |
||
| 198 | public function commitTransaction(): void |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function rollbackTransaction(): void |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | * |
||
| 234 | * Symfony cache feature for deferring saves, not used by eZ & not related to transaction handling here. |
||
| 235 | */ |
||
| 236 | public function saveDeferred(CacheItemInterface $item) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | * |
||
| 244 | * Symfony cache feature for committing deferred saves, not used by eZ & not related to transaction handling here. |
||
| 245 | */ |
||
| 246 | public function commit() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * For use by getItem(s) to mark items as a miss if it's going to be cleared on transaction commit. |
||
| 253 | * |
||
| 254 | * @param \Symfony\Component\Cache\CacheItem[] $items |
||
| 255 | * |
||
| 256 | * @return \Symfony\Component\Cache\CacheItem[] |
||
| 257 | */ |
||
| 258 | private function markItemsAsDeferredMissIfNeeded(iterable $items) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param \Symfony\Component\Cache\CacheItem $item |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | private function itemIsDeferredMiss(CacheItem $item): bool |
||
| 301 | } |
||
| 302 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: