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 |
||
| 19 | class InMemoryCacheAdapterTest extends TestCase |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var \eZ\Publish\Core\Persistence\Cache\Adapter\InMemoryCacheAdapter |
||
| 23 | */ |
||
| 24 | protected $cache; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface|\PHPUnit\Framework\MockObject\MockObject |
||
| 28 | */ |
||
| 29 | protected $cacheMock; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \Closure |
||
| 33 | */ |
||
| 34 | private $cacheItemsClosure; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Setup the HandlerTest. |
||
| 38 | */ |
||
| 39 | final protected function setUp() |
||
| 40 | { |
||
| 41 | parent::setUp(); |
||
| 42 | |||
| 43 | $this->cacheMock = $this->createMock(TagAwareAdapterInterface::class); |
||
| 44 | |||
| 45 | $this->cache = new InMemoryCacheAdapter( |
||
| 46 | $this->cacheMock, |
||
| 47 | 20, |
||
| 48 | 3 |
||
| 49 | ); |
||
| 50 | |||
| 51 | $this->cacheItemsClosure = \Closure::bind( |
||
| 52 | function ($key, $value, $isHit, $defaultLifetime = 0) { |
||
| 53 | $item = new CacheItem(); |
||
| 54 | $item->key = $key; |
||
|
|
|||
| 55 | $item->value = $value; |
||
| 56 | $item->isHit = $isHit; |
||
| 57 | $item->defaultLifetime = $defaultLifetime; |
||
| 58 | |||
| 59 | return $item; |
||
| 60 | }, |
||
| 61 | null, |
||
| 62 | CacheItem::class |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Tear down test (properties). |
||
| 68 | */ |
||
| 69 | final protected function tearDown() |
||
| 76 | |||
| 77 | public function testGetItemOnlyCalledOnce() |
||
| 93 | |||
| 94 | View Code Duplication | public function testGetItemsOnlyCalledOnce() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Symfony uses generators with getItems() so we need to make sure we handle that. |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function testGetItemsWithGenerator() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param $key |
||
| 139 | * @param null $value If null the cache item will be assumed to be a cache miss here. |
||
| 140 | * @param int $defaultLifetime |
||
| 141 | * |
||
| 142 | * @return CacheItem |
||
| 143 | */ |
||
| 144 | final protected function getCacheItem($key, $value = null, $defaultLifetime = 0) |
||
| 150 | |||
| 151 | final protected function arrayAsGenerator(array $array) |
||
| 157 | } |
||
| 158 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.