Skip to content

Commit 7f86ce4

Browse files
committed
Final code review as per Scrutinizer recommendations
1 parent 7299285 commit 7f86ce4

18 files changed

+309
-169
lines changed

docs/migration/MigratingFromV6ToV7.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,7 @@ Therefore it way requires a Couchbase Server updated combined with an SDK update
163163
As of the V7, all the constant starting by _PFC\_*_ were moved into the `Phpfastcache\Autoload` namespace\
164164
to not pollute php's root namespace.
165165

166+
### Autoload mechanism
167+
Although we highly recommend you to make use of composer benefits, it's however still possible\
168+
to use our own standalone autoloader. It is now located in `lib/autoload`
169+

lib/Phpfastcache/Autoload/Autoload.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
*
1717
*/
1818

19-
class Autoload
20-
{
21-
}
22-
2319
const PFC_PHP_EXT = 'php';
2420
const PFC_BIN_DIR = __DIR__ . '/../../../bin/';
2521
const PFC_LIB_DIR = __DIR__ . '/../../../lib/';

lib/Phpfastcache/CacheManager.php

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Phpfastcache\Config\ConfigurationOption;
1919
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
2020
use Phpfastcache\Exceptions\{
21-
PhpfastcacheDriverCheckException, PhpfastcacheDriverException, PhpfastcacheDriverNotFoundException, PhpfastcacheInstanceNotFoundException, PhpfastcacheInvalidArgumentException, PhpfastcacheInvalidConfigurationException, PhpfastcacheLogicException, PhpfastcacheUnsupportedOperationException
21+
PhpfastcacheDriverCheckException, PhpfastcacheDriverException, PhpfastcacheDriverNotFoundException, PhpfastcacheExceptionInterface, PhpfastcacheInstanceNotFoundException, PhpfastcacheInvalidArgumentException, PhpfastcacheInvalidConfigurationException, PhpfastcacheLogicException, PhpfastcacheRootException, PhpfastcacheUnsupportedOperationException
2222
};
2323
use Phpfastcache\Util\ClassNamespaceResolverTrait;
2424

@@ -112,61 +112,29 @@ class CacheManager
112112
*/
113113
public static function getInstance(string $driver = self::AUTOMATIC_DRIVER_CLASS, $config = null, string $instanceId = null): ExtendedCacheItemPoolInterface
114114
{
115-
if (\is_array($config)) {
116-
$config = new ConfigurationOption($config);
117-
\trigger_error(
118-
'The CacheManager will drops the support of primitive configuration arrays, use a "\Phpfastcache\Config\ConfigurationOption" object instead',
119-
E_USER_DEPRECATED
120-
);
121-
} elseif ($config === null) {
122-
$config = self::getDefaultConfig();
123-
} else {
124-
if (!($config instanceof ConfigurationOption)) {
125-
throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', \gettype($config)));
126-
}
127-
}
128-
115+
$config = self::validateConfig($config);
129116
$driver = self::standardizeDriverName($driver);
130117

131118
if (!$driver || $driver === self::AUTOMATIC_DRIVER_CLASS) {
132119
$driver = self::getAutoClass($config);
133120
}
134121

135-
$instance = $instanceId ?: \md5($driver . \serialize($config->toArray()));
122+
$instanceId = $instanceId ?: \md5($driver . \serialize($config->toArray()));
136123

137-
if (!isset(self::$instances[$instance])) {
124+
if (!isset(self::$instances[$instanceId])) {
138125
self::$badPracticeOmeter[$driver] = 1;
139-
$driverClass = self::getDriverClass($driver);
140-
141-
if (!is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)) {
142-
throw new PhpfastcacheDriverException(\sprintf(
143-
'Class "%s" does not implement "%s"',
144-
$driverClass,
145-
ExtendedCacheItemPoolInterface::class
146-
));
147-
}
126+
$driverClass = self::validateDriverClass(self::getDriverClass($driver));
127+
148128
try {
149129
if (\class_exists($driverClass)) {
150130
$configClass = $driverClass::getConfigClass();
151-
self::$instances[$instance] = new $driverClass(new $configClass($config->toArray()), $instance);
152-
self::$instances[$instance]->setEventManager(EventManager::getInstance());
131+
self::$instances[$instanceId] = new $driverClass(new $configClass($config->toArray()), $instanceId);
132+
self::$instances[$instanceId]->setEventManager(EventManager::getInstance());
153133
} else {
154134
throw new PhpfastcacheDriverNotFoundException(\sprintf('The driver "%s" does not exists', $driver));
155135
}
156136
} catch (PhpfastcacheDriverCheckException $e) {
157-
if ($config->getFallback()) {
158-
try {
159-
$fallback = $config->getFallback();
160-
$config->setFallback('');
161-
\trigger_error(\sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver,
162-
$fallback), E_USER_WARNING);
163-
return self::getInstance($fallback, $config->getFallbackConfig());
164-
} catch (PhpfastcacheInvalidArgumentException $e) {
165-
throw new PhpfastcacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e);
166-
}
167-
} else {
168-
throw new PhpfastcacheDriverCheckException($e->getMessage(), $e->getCode(), $e);
169-
}
137+
return self::getFallbackInstance($driver, $config, $e);
170138
}
171139
} else {
172140
if (self::$badPracticeOmeter[$driver] >= 2) {
@@ -177,7 +145,34 @@ public static function getInstance(string $driver = self::AUTOMATIC_DRIVER_CLASS
177145

178146
self::$badPracticeOmeter[$driver]++;
179147

180-
return self::$instances[$instance];
148+
return self::$instances[$instanceId];
149+
}
150+
151+
/**
152+
* @param string $driver
153+
* @param ConfigurationOption $config
154+
* @param PhpfastcacheDriverCheckException $e
155+
* @return ExtendedCacheItemPoolInterface
156+
* @throws PhpfastcacheDriverCheckException
157+
* @throws PhpfastcacheDriverException
158+
* @throws PhpfastcacheDriverNotFoundException
159+
* @throws PhpfastcacheInvalidConfigurationException
160+
*/
161+
protected static function getFallbackInstance(string $driver, ConfigurationOption $config, PhpfastcacheDriverCheckException $e)
162+
{
163+
if ($config->getFallback()) {
164+
try {
165+
$fallback = $config->getFallback();
166+
$config->setFallback('');
167+
\trigger_error(\sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver,
168+
$fallback), E_USER_WARNING);
169+
return self::getInstance($fallback, $config->getFallbackConfig());
170+
} catch (PhpfastcacheInvalidArgumentException $e) {
171+
throw new PhpfastcacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e);
172+
}
173+
} else {
174+
throw new PhpfastcacheDriverCheckException($e->getMessage(), $e->getCode(), $e);
175+
}
181176
}
182177

183178
/**
@@ -190,10 +185,6 @@ public static function getInstance(string $driver = self::AUTOMATIC_DRIVER_CLASS
190185
*/
191186
public static function getInstanceById(string $instanceId): ExtendedCacheItemPoolInterface
192187
{
193-
if ($instanceId !== null && !\is_string($instanceId)) {
194-
throw new PhpfastcacheInvalidArgumentException('The Instance ID must be a string');
195-
}
196-
197188
if (isset(self::$instances[$instanceId])) {
198189
return self::$instances[$instanceId];
199190
}
@@ -242,8 +233,9 @@ public static function getAutoClass(ConfigurationOption $config): string
242233

243234
if ($autoDriver === null) {
244235
foreach (self::getDriverList() as $driver) {
245-
/** @var ExtendedCacheItemPoolInterface $driver */
246-
if ((self::CORE_DRIVER_NAMESPACE . $driver . '\Driver')::isUsableInAutoContext()) {
236+
/** @var ExtendedCacheItemPoolInterface $driverClass */
237+
$driverClass = self::CORE_DRIVER_NAMESPACE . $driver . '\Driver';
238+
if ($driverClass::isUsableInAutoContext()) {
247239
try {
248240
self::getInstance($driver, $config);
249241
$autoDriver = $driver;
@@ -267,7 +259,7 @@ public static function getAutoClass(ConfigurationOption $config): string
267259
/**
268260
* @param string $name
269261
* @param array $arguments
270-
* @return \Psr\Cache\ExtendedCacheItemPoolInterface
262+
* @return ExtendedCacheItemPoolInterface
271263
*/
272264
public static function __callStatic(string $name, array $arguments): ExtendedCacheItemPoolInterface
273265
{
@@ -559,4 +551,46 @@ public static function removeCoreDriverOverride(string $driverName)
559551

560552
unset(self::$driverOverrides[$driverName]);
561553
}
554+
555+
/**
556+
* @param array|ConfigurationOption
557+
* @return ConfigurationOption
558+
* @throws PhpfastcacheInvalidArgumentException
559+
* @throws PhpfastcacheInvalidConfigurationException
560+
*/
561+
protected static function validateConfig($config): ConfigurationOption
562+
{
563+
if (\is_array($config)) {
564+
$config = new ConfigurationOption($config);
565+
\trigger_error(
566+
'The CacheManager will drops the support of primitive configuration arrays, use a "\Phpfastcache\Config\ConfigurationOption" object instead',
567+
E_USER_DEPRECATED
568+
);
569+
} elseif ($config === null) {
570+
$config = self::getDefaultConfig();
571+
} else {
572+
if (!($config instanceof ConfigurationOption)) {
573+
throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', \gettype($config)));
574+
}
575+
}
576+
577+
return $config;
578+
}
579+
580+
/**
581+
* @param string $driverClass
582+
* @return string
583+
* @throws PhpfastcacheDriverException
584+
*/
585+
protected static function validateDriverClass(string $driverClass): string
586+
{
587+
if (!is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)) {
588+
throw new PhpfastcacheDriverException(\sprintf(
589+
'Class "%s" does not implement "%s"',
590+
$driverClass,
591+
ExtendedCacheItemPoolInterface::class
592+
));
593+
}
594+
return $driverClass;
595+
}
562596
}

lib/Phpfastcache/Config/Config.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<?php
22
/**
3-
* Created by PhpStorm.
4-
* User: Geolim4
5-
* Date: 10/02/2018
6-
* Time: 18:47
3+
*
4+
* This file is part of phpFastCache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt file.
9+
*
10+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
*
713
*/
8-
14+
declare(strict_types=1);
915
namespace Phpfastcache\Config;
1016

1117
/**

lib/Phpfastcache/Config/ConfigurationOption.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<?php
22
/**
3-
* Created by PhpStorm.
4-
* User: Geolim4
5-
* Date: 10/02/2018
6-
* Time: 18:45
3+
*
4+
* This file is part of phpFastCache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt file.
9+
*
10+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
*
713
*/
14+
declare(strict_types=1);
815

916
namespace Phpfastcache\Config;
1017

1118
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
1219
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
1320
use Phpfastcache\Util\ArrayObject;
1421

15-
class ConfigurationOption extends ArrayObject
22+
class ConfigurationOption extends ArrayObject implements ConfigurationOptionInterface
1623
{
1724
/**
1825
* @var bool
@@ -261,7 +268,7 @@ public function getDefaultKeyHashFunction()
261268
*/
262269
public function setDefaultKeyHashFunction($defaultKeyHashFunction)
263270
{
264-
if (!\function_exists($defaultKeyHashFunction) || !\is_callable($defaultKeyHashFunction)) {
271+
if (!\is_callable($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction)) {
265272
throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string');
266273
}
267274
$this->defaultKeyHashFunction = $defaultKeyHashFunction;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of phpFastCache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt file.
9+
*
10+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
*
13+
*/
14+
declare(strict_types=1);
15+
namespace Phpfastcache\Config;
16+
17+
18+
interface ConfigurationOptionInterface
19+
{
20+
/**
21+
* @param $args
22+
* ArrayObject constructor.
23+
*/
24+
public function __construct(...$args);
25+
26+
/**
27+
* @param string $optionName
28+
* @return mixed|null
29+
* @deprecated Use ->getOptionName() instead
30+
*/
31+
public function getOption(string $optionName);
32+
33+
/**
34+
* @param string $optionName
35+
* @return mixed|null
36+
*/
37+
public function isValidOption(string $optionName);
38+
}

lib/Phpfastcache/Config/IOConfigurationOptionTrait.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<?php
22
/**
3-
* Created by PhpStorm.
4-
* User: Geolim4
5-
* Date: 10/02/2018
6-
* Time: 18:45
3+
*
4+
* This file is part of phpFastCache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt file.
9+
*
10+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
*
713
*/
8-
14+
declare(strict_types=1);
915
namespace Phpfastcache\Config;
1016

1117
trait IOConfigurationOptionTrait
@@ -16,7 +22,7 @@ trait IOConfigurationOptionTrait
1622
protected $secureFileManipulation = false;
1723

1824
/**
19-
* @var string
25+
* @var bool
2026
*/
2127
protected $htaccess = true;
2228

@@ -37,7 +43,7 @@ public function getSecurityKey(): string
3743
* @param string $securityKey
3844
* @return Config
3945
*/
40-
public function setSecurityKey(string $securityKey): self
46+
public function setSecurityKey(string $securityKey): ConfigurationOptionInterface
4147
{
4248
$this->securityKey = $securityKey;
4349

@@ -56,9 +62,10 @@ public function getHtaccess(): bool
5662
* @param bool $htaccess
5763
* @return Config
5864
*/
59-
public function setHtaccess(bool $htaccess): self
65+
public function setHtaccess(bool $htaccess): ConfigurationOptionInterface
6066
{
6167
$this->htaccess = $htaccess;
68+
6269
return $this;
6370
}
6471

@@ -74,7 +81,7 @@ public function isSecureFileManipulation(): bool
7481
* @param bool $secureFileManipulation
7582
* @return self
7683
*/
77-
public function setSecureFileManipulation(bool $secureFileManipulation): self
84+
public function setSecureFileManipulation(bool $secureFileManipulation): ConfigurationOptionInterface
7885
{
7986
$this->secureFileManipulation = $secureFileManipulation;
8087
return $this;

lib/Phpfastcache/Core/Item/ItemBaseTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ trait ItemBaseTrait
4141
protected $data;
4242

4343
/**
44-
* @var \DateTime
44+
* @var \DateTimeInterface
4545
*/
4646
protected $expirationDate;
4747

4848
/**
49-
* @var \DateTime
49+
* @var \DateTimeInterface
5050
*/
5151
protected $creationDate;
5252

5353
/**
54-
* @var \DateTime
54+
* @var \DateTimeInterface
5555
*/
5656
protected $modificationDate;
5757

0 commit comments

Comments
 (0)