Skip to content

Commit 87af2ac

Browse files
committed
Fixed class hierarchy issues reported by Scrutinizer
1 parent e84ae27 commit 87af2ac

35 files changed

Lines changed: 146 additions & 135 deletions

lib/Phpfastcache/Cluster/AggregatablePoolInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
namespace Phpfastcache\Cluster;
1818

19-
use Psr\Cache\CacheItemPoolInterface;
19+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
2020

21-
interface AggregatablePoolInterface extends CacheItemPoolInterface
21+
interface AggregatablePoolInterface extends ExtendedCacheItemPoolInterface
2222
{
2323
}

lib/Phpfastcache/Cluster/Drivers/MasterSlaveReplication/Driver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
2121
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
2222
use Phpfastcache\Event\Event;
23-
use Phpfastcache\EventManager;
23+
use Phpfastcache\Event\EventManagerInterface;
2424
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
2525
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
2626
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
@@ -36,7 +36,7 @@ class Driver extends ClusterPoolAbstract
3636
/**
3737
* MasterSlaveReplicationCluster constructor.
3838
* @param string $clusterName
39-
* @param EventManager $em
39+
* @param EventManagerInterface $em
4040
* @param ExtendedCacheItemPoolInterface ...$driverPools
4141
* @throws PhpfastcacheDriverCheckException
4242
* @throws PhpfastcacheDriverConnectException
@@ -45,7 +45,7 @@ class Driver extends ClusterPoolAbstract
4545
* @throws PhpfastcacheDriverException
4646
* @throws PhpfastcacheIOException
4747
*/
48-
public function __construct(string $clusterName, EventManager $em, ExtendedCacheItemPoolInterface ...$driverPools)
48+
public function __construct(string $clusterName, EventManagerInterface $em, ExtendedCacheItemPoolInterface ...$driverPools)
4949
{
5050
if (\count($driverPools) !== 2) {
5151
throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.');

lib/Phpfastcache/Config/ConfigurationOption.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,15 @@ public function setUseStaticItemCaching(bool $useStaticItemCaching): static
290290
return $this;
291291
}
292292

293-
294293
/**
295294
* @return object
296-
* @throws PhpfastcacheInvalidArgumentException
297-
* @throws PhpfastcacheLogicException
298295
*/
299296
public function getSuperGlobalAccessor(): object
300297
{
301298
if (!isset($this->superGlobalAccessor)) {
302-
$this->setSuperGlobalAccessor($this->getDefaultSuperGlobalAccessor());
299+
$this->superGlobalAccessor = $this->getDefaultSuperGlobalAccessor();
303300
}
301+
304302
return $this->superGlobalAccessor;
305303
}
306304

lib/Phpfastcache/Config/IOConfigurationOptionTrait.php renamed to lib/Phpfastcache/Config/IOConfigurationOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
2020
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
2121

22-
trait IOConfigurationOptionTrait
22+
class IOConfigurationOption extends ConfigurationOption
2323
{
2424
protected bool $secureFileManipulation = false;
2525

lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -309,24 +309,5 @@ public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface
309309
return $driverPool::getClassNamespace() === self::getClassNamespace();
310310
}
311311

312-
/**
313-
* @throws PhpfastcacheLogicException
314-
* @throws PhpfastcacheInvalidArgumentException
315-
*/
316-
public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void
317-
{
318-
$itemTarget->setEventManager($this->getEventManager())
319-
->set($this->getRawValue())
320-
->setHit($this->isHit())
321-
->setTags($this->getTags())
322-
->expiresAt(clone $this->getExpirationDate())
323-
->setDriver($itemPoolTarget ?? $this->driver);
324-
325-
if ($this->driver->getConfig()->isItemDetailedDate()) {
326-
$itemTarget->setCreationDate(clone $this->getCreationDate())
327-
->setModificationDate(clone $this->getModificationDate());
328-
}
329-
}
330-
331312
abstract protected function getDriverClass(): string;
332313
}

lib/Phpfastcache/Core/Item/TaggableCacheItemTrait.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
namespace Phpfastcache\Core\Item;
1818

19+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
1920
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
21+
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
2022

2123
trait TaggableCacheItemTrait
2224
{
@@ -150,4 +152,23 @@ public function getRemovedTags(): array
150152
{
151153
return \array_diff($this->removedTags, $this->tags);
152154
}
155+
156+
/**
157+
* @throws PhpfastcacheLogicException
158+
* @throws PhpfastcacheInvalidArgumentException
159+
*/
160+
public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void
161+
{
162+
$itemTarget->setEventManager($this->getEventManager())
163+
->set($this->getRawValue())
164+
->setHit($this->isHit())
165+
->setTags($this->getTags())
166+
->expiresAt(clone $this->getExpirationDate())
167+
->setDriver($itemPoolTarget ?? $this->driver);
168+
169+
if ($this->driver->getConfig()->isItemDetailedDate()) {
170+
$itemTarget->setCreationDate(clone $this->getCreationDate())
171+
->setModificationDate(clone $this->getModificationDate());
172+
}
173+
}
153174
}

lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
namespace Phpfastcache\Core\Pool;
1818

1919
use DateTime;
20+
use Phpfastcache\Config\ConfigurationOptionInterface;
2021
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
2122
use Phpfastcache\Entities\DriverIO;
2223
use Phpfastcache\Entities\ItemBatch;
2324
use Phpfastcache\Event\Event;
25+
use Phpfastcache\Event\EventManagerInterface;
2426
use Phpfastcache\Event\EventReferenceParameter;
2527
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
2628
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
@@ -30,9 +32,15 @@
3032
use Psr\Cache\CacheItemInterface;
3133
use RuntimeException;
3234

35+
/**
36+
* @method array driverUnwrapTags(array $wrapper)
37+
* @method void cleanItemTags(ExtendedCacheItemInterface $item)
38+
*/
3339
trait CacheItemPoolTrait
3440
{
35-
use DriverBaseTrait;
41+
use DriverBaseTrait {
42+
DriverBaseTrait::__construct as __driverBaseConstruct;
43+
}
3644

3745
/**
3846
* @var string
@@ -51,6 +59,12 @@ trait CacheItemPoolTrait
5159

5260
protected DriverIO $IO;
5361

62+
public function __construct(ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em)
63+
{
64+
$this->IO = new DriverIO();
65+
$this->__driverBaseConstruct($config, $instanceId, $em);
66+
}
67+
5468
/**
5569
* @throws PhpfastcacheLogicException
5670
* @throws PhpfastcacheInvalidArgumentException
@@ -310,7 +324,7 @@ public function deleteItem(string $key): bool
310324
/**
311325
* Perform a tag cleanup to avoid memory leaks
312326
*/
313-
if (!\str_starts_with($key, self::DRIVER_TAGS_KEY_PREFIX)) {
327+
if (!\str_starts_with($key, TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) {
314328
$this->cleanItemTags($item);
315329
}
316330

@@ -433,4 +447,49 @@ public function getIO(): DriverIO
433447
{
434448
return $this->IO;
435449
}
450+
451+
/**
452+
* @internal This method de-register an item from $this->itemInstances
453+
*/
454+
protected function deregisterItem(string $item): static
455+
{
456+
unset($this->itemInstances[$item]);
457+
458+
if (\gc_enabled()) {
459+
\gc_collect_cycles();
460+
}
461+
462+
return $this;
463+
}
464+
465+
/**
466+
* @throws PhpfastcacheLogicException
467+
*/
468+
public function attachItem(CacheItemInterface $item): static
469+
{
470+
if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) {
471+
throw new PhpfastcacheLogicException(
472+
'The item already exists and cannot be overwritten because the Spl object hash mismatches !
473+
You probably tried to re-attach a detached item which has been already retrieved from cache.'
474+
);
475+
}
476+
477+
if (!$this->getConfig()->isUseStaticItemCaching()) {
478+
throw new PhpfastcacheLogicException(
479+
'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.'
480+
);
481+
}
482+
483+
$this->itemInstances[$item->getKey()] = $item;
484+
485+
return $this;
486+
}
487+
488+
public function isAttached(CacheItemInterface $item): bool
489+
{
490+
if (isset($this->itemInstances[$item->getKey()])) {
491+
return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]);
492+
}
493+
return false;
494+
}
436495
}

lib/Phpfastcache/Core/Pool/DriverBaseTrait.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ public function __construct(ConfigurationOptionInterface $config, string $instan
6666
$this->setEventManager($em);
6767
$this->setConfig($config);
6868
$this->instanceId = $instanceId;
69-
$this->IO = new DriverIO();
7069

7170
if (!$this->driverCheck()) {
72-
throw new PhpfastcacheDriverCheckException(\sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
71+
throw new PhpfastcacheDriverCheckException(\sprintf(ExtendedCacheItemPoolInterface::DRIVER_CHECK_FAILURE, $this->getDriverName()));
7372
}
7473

7574
try {
@@ -78,7 +77,7 @@ public function __construct(ConfigurationOptionInterface $config, string $instan
7877
} catch (Throwable $e) {
7978
throw new PhpfastcacheDriverConnectException(
8079
sprintf(
81-
self::DRIVER_CONNECT_FAILURE,
80+
ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE,
8281
$e::class,
8382
$this->getDriverName(),
8483
$e->getMessage(),
@@ -145,8 +144,8 @@ public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyD
145144
$wrap = [
146145
ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key
147146
ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => $item->getRawValue(),
147+
ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(),
148148
TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(),
149-
self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(),
150149
];
151150

152151
if ($this->getConfig()->isItemDetailedDate()) {
@@ -207,11 +206,11 @@ public function driverUnwrapData(array $wrapper): mixed
207206
*/
208207
public function driverUnwrapEdate(array $wrapper): \DateTimeInterface
209208
{
210-
if ($wrapper[self::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
211-
return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX];
209+
if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
210+
return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX];
212211
}
213212

214-
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX]);
213+
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]);
215214
}
216215

217216
/**
@@ -220,11 +219,11 @@ public function driverUnwrapEdate(array $wrapper): \DateTimeInterface
220219
*/
221220
public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface
222221
{
223-
if ($wrapper[self::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
224-
return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX];
222+
if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
223+
return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX];
225224
}
226225

227-
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX]);
226+
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]);
228227
}
229228

230229
/**
@@ -233,11 +232,11 @@ public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface
233232
*/
234233
public function driverUnwrapMdate(array $wrapper): ?\DateTimeInterface
235234
{
236-
if ($wrapper[self::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
237-
return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX];
235+
if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
236+
return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX];
238237
}
239238

240-
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX]);
239+
return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]);
241240
}
242241

243242
/**

lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -69,51 +69,6 @@ public function detachItem(CacheItemInterface $item): static
6969
return $this;
7070
}
7171

72-
/**
73-
* @internal This method de-register an item from $this->itemInstances
74-
*/
75-
protected function deregisterItem(string $item): static
76-
{
77-
unset($this->itemInstances[$item]);
78-
79-
if (\gc_enabled()) {
80-
\gc_collect_cycles();
81-
}
82-
83-
return $this;
84-
}
85-
86-
/**
87-
* @throws PhpfastcacheLogicException
88-
*/
89-
public function attachItem(CacheItemInterface $item): static
90-
{
91-
if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) {
92-
throw new PhpfastcacheLogicException(
93-
'The item already exists and cannot be overwritten because the Spl object hash mismatches !
94-
You probably tried to re-attach a detached item which has been already retrieved from cache.'
95-
);
96-
}
97-
98-
if (!$this->getConfig()->isUseStaticItemCaching()) {
99-
throw new PhpfastcacheLogicException(
100-
'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.'
101-
);
102-
}
103-
104-
$this->itemInstances[$item->getKey()] = $item;
105-
106-
return $this;
107-
}
108-
109-
public function isAttached(CacheItemInterface $item): bool
110-
{
111-
if (isset($this->itemInstances[$item->getKey()])) {
112-
return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]);
113-
}
114-
return false;
115-
}
116-
11772
/**
11873
* @param ExtendedCacheItemInterface ...$items
11974
* @return bool

lib/Phpfastcache/Core/Pool/TaggableCacheItemPoolTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItem
201201
$items = $this->fetchItemsByTagFromBackend($tagName);
202202
if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) {
203203
foreach ($items as $key => $item) {
204-
if (\array_diff($item->getTags(), $tagName)) {
204+
if (\array_diff($item->getTags(), [$tagName])) {
205205
unset($items[$key]);
206206
}
207207
}

0 commit comments

Comments
 (0)