diff --git a/app/Config/Cache.php b/app/Config/Cache.php index e6efa3acf6f5..1169c95ff7ee 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -78,7 +78,7 @@ class Cache extends BaseConfig * Your file storage preferences can be specified below, if you are using * the File driver. * - * @var array + * @var array{storePath?: string, mode?: int} */ public array $file = [ 'storePath' => WRITEPATH . 'cache/', @@ -95,7 +95,7 @@ class Cache extends BaseConfig * * @see https://codeigniter.com/user_guide/libraries/caching.html#memcached * - * @var array + * @var array{host?: string, port?: int, weight?: int, raw?: bool} */ public array $memcached = [ 'host' => '127.0.0.1', @@ -112,7 +112,7 @@ class Cache extends BaseConfig * Your Redis server can be specified below, if you are using * the Redis or Predis drivers. * - * @var array + * @var array{host?: string, password?: string|null, port?: int, timeout?: int, database?: int} */ public array $redis = [ 'host' => '127.0.0.1', diff --git a/system/Cache/CacheFactory.php b/system/Cache/CacheFactory.php index fb60d73c5cd7..e84254142125 100644 --- a/system/Cache/CacheFactory.php +++ b/system/Cache/CacheFactory.php @@ -75,7 +75,7 @@ public static function getHandler(Cache $config, ?string $handler = null, ?strin } } - // If $adapter->initialization throws a CriticalError exception, we will attempt to + // If $adapter->initialize() throws a CriticalError exception, we will attempt to // use the $backup handler, if that also fails, we resort to the dummy handler. try { $adapter->initialize(); diff --git a/system/Cache/CacheInterface.php b/system/Cache/CacheInterface.php index d76d5aa15ba3..0f94cd56f930 100644 --- a/system/Cache/CacheInterface.php +++ b/system/Cache/CacheInterface.php @@ -13,9 +13,6 @@ namespace CodeIgniter\Cache; -/** - * Cache interface - */ interface CacheInterface { /** @@ -30,16 +27,16 @@ public function initialize(); * * @param string $key Cache item name * - * @return array|bool|float|int|object|string|null + * @return mixed */ public function get(string $key); /** * Saves an item to the cache store. * - * @param string $key Cache item name - * @param array|bool|float|int|object|string|null $value The data to save - * @param int $ttl Time To Live, in seconds (default 60) + * @param string $key Cache item name + * @param mixed $value The data to save + * @param int $ttl Time To Live, in seconds (default 60) * * @return bool Success or failure */ @@ -87,7 +84,7 @@ public function clean(); * The information returned and the structure of the data * varies depending on the handler. * - * @return array|false|object|null + * @return array|false|object|null */ public function getCacheInfo(); @@ -96,10 +93,9 @@ public function getCacheInfo(); * * @param string $key Cache item name. * - * @return array|false|null - * Returns null if the item does not exist, otherwise array - * with at least the 'expire' key for absolute epoch expiry (or null). - * Some handlers may return false when an item does not exist, which is deprecated. + * @return array|false|null Returns null if the item does not exist, otherwise array + * with at least the 'expire' key for absolute epoch expiry (or null). + * Some handlers may return false when an item does not exist, which is deprecated. */ public function getMetaData(string $key); diff --git a/system/Cache/Exceptions/CacheException.php b/system/Cache/Exceptions/CacheException.php index 650dad64d649..c4871a00e8b4 100644 --- a/system/Cache/Exceptions/CacheException.php +++ b/system/Cache/Exceptions/CacheException.php @@ -16,9 +16,6 @@ use CodeIgniter\Exceptions\DebugTraceableTrait; use CodeIgniter\Exceptions\RuntimeException; -/** - * CacheException - */ class CacheException extends RuntimeException { use DebugTraceableTrait; @@ -26,7 +23,7 @@ class CacheException extends RuntimeException /** * Thrown when handler has no permission to write cache. * - * @return CacheException + * @return static */ public static function forUnableToWrite(string $path) { @@ -36,7 +33,7 @@ public static function forUnableToWrite(string $path) /** * Thrown when an unrecognized handler is used. * - * @return CacheException + * @return static */ public static function forInvalidHandlers() { @@ -46,7 +43,7 @@ public static function forInvalidHandlers() /** * Thrown when no backup handler is setup in config. * - * @return CacheException + * @return static */ public static function forNoBackup() { @@ -56,7 +53,7 @@ public static function forNoBackup() /** * Thrown when specified handler was not found. * - * @return CacheException + * @return static */ public static function forHandlerNotFound() { diff --git a/system/Cache/FactoriesCache.php b/system/Cache/FactoriesCache.php index e4b7488f43df..6e49996c5d45 100644 --- a/system/Cache/FactoriesCache.php +++ b/system/Cache/FactoriesCache.php @@ -18,15 +18,9 @@ final class FactoriesCache { - /** - * @var CacheInterface|FileVarExportHandler - */ - private $cache; - - /** - * @param CacheInterface|FileVarExportHandler|null $cache - */ - public function __construct($cache = null) + private readonly CacheInterface|FileVarExportHandler $cache; + + public function __construct(CacheInterface|FileVarExportHandler|null $cache = null) { $this->cache = $cache ?? new FileVarExportHandler(); } @@ -51,7 +45,9 @@ public function load(string $component): bool { $key = $this->getCacheKey($component); - if (! $data = $this->cache->get($key)) { + $data = $this->cache->get($key); + + if (! is_array($data) || $data === []) { return false; } diff --git a/system/Cache/FactoriesCache/FileVarExportHandler.php b/system/Cache/FactoriesCache/FileVarExportHandler.php index b7c1a043c86e..092cd67ebd80 100644 --- a/system/Cache/FactoriesCache/FileVarExportHandler.php +++ b/system/Cache/FactoriesCache/FileVarExportHandler.php @@ -17,10 +17,7 @@ final class FileVarExportHandler { private string $path = WRITEPATH . 'cache'; - /** - * @param array|bool|float|int|object|string|null $val - */ - public function save(string $key, $val): void + public function save(string $key, mixed $val): void { $val = var_export($val, true); @@ -36,10 +33,7 @@ public function delete(string $key): void @unlink($this->path . "/{$key}"); } - /** - * @return array|bool|float|int|object|string|null - */ - public function get(string $key) + public function get(string $key): mixed { return @include $this->path . "/{$key}"; } diff --git a/system/Cache/Handlers/BaseHandler.php b/system/Cache/Handlers/BaseHandler.php index ebe0ca2f43ba..db482952022a 100644 --- a/system/Cache/Handlers/BaseHandler.php +++ b/system/Cache/Handlers/BaseHandler.php @@ -53,7 +53,7 @@ abstract class BaseHandler implements CacheInterface * Keys that exceed MAX_KEY_LENGTH are hashed. * From https://github.com/symfony/cache/blob/7b024c6726af21fd4984ac8d1eae2b9f3d90de88/CacheItem.php#L158 * - * @param string $key The key to validate + * @param mixed $key The key to validate * @param string $prefix Optional prefix to include in length calculations * * @throws InvalidArgumentException When $key is not valid @@ -67,7 +67,8 @@ public static function validateKey($key, $prefix = ''): string throw new InvalidArgumentException('Cache key cannot be empty.'); } - $reserved = config(Cache::class)->reservedCharacters ?? self::RESERVED_CHARACTERS; + $reserved = config(Cache::class)->reservedCharacters; + if ($reserved !== '' && strpbrk($key, $reserved) !== false) { throw new InvalidArgumentException('Cache key contains reserved characters ' . $reserved); } @@ -83,7 +84,7 @@ public static function validateKey($key, $prefix = ''): string * @param int $ttl Time to live * @param Closure(): mixed $callback Callback return value * - * @return array|bool|float|int|object|string|null + * @return mixed */ public function remember(string $key, int $ttl, Closure $callback) { @@ -103,7 +104,7 @@ public function remember(string $key, int $ttl, Closure $callback) * * @param string $pattern Cache items glob-style pattern * - * @return int|never + * @return int * * @throws Exception */ diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index c496143f62d9..010fc86d9cdd 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -54,14 +54,19 @@ class FileHandler extends BaseHandler */ public function __construct(Cache $config) { - $this->path = ! empty($config->file['storePath']) ? $config->file['storePath'] : WRITEPATH . 'cache'; - $this->path = rtrim($this->path, '/') . '/'; + $options = [ + ...['storePath' => WRITEPATH . 'cache', 'mode' => 0640], + ...$config->file, + ]; + + $this->path = $options['storePath'] !== '' ? $options['storePath'] : WRITEPATH . 'cache'; + $this->path = rtrim($this->path, '\\/') . '/'; if (! is_really_writable($this->path)) { throw CacheException::forUnableToWrite($this->path); } - $this->mode = $config->file['mode'] ?? 0640; + $this->mode = $options['mode']; $this->prefix = $config->prefix; helper('filesystem'); @@ -342,33 +347,46 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs * @param bool $topLevelOnly Look only at the top level directory specified? * @param bool $_recursion Internal variable to determine recursion status - do not use in calls * - * @return array|false + * @return array|false */ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false) { - static $_filedata = []; - $relativePath = $sourceDir; + static $filedata = []; + + $relativePath = $sourceDir; + $filePointer = @opendir($sourceDir); - if ($fp = @opendir($sourceDir)) { + if (! is_bool($filePointer)) { // reset the array and make sure $sourceDir has a trailing slash on the initial call if ($_recursion === false) { - $_filedata = []; - $sourceDir = rtrim(realpath($sourceDir) ?: $sourceDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + $filedata = []; + + $resolvedSrc = realpath($sourceDir); + $resolvedSrc = $resolvedSrc === false ? $sourceDir : $resolvedSrc; + + $sourceDir = rtrim($resolvedSrc, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } // Used to be foreach (scandir($sourceDir, 1) as $file), but scandir() is simply not as fast - while (false !== ($file = readdir($fp))) { + while (false !== $file = readdir($filePointer)) { if (is_dir($sourceDir . $file) && $file[0] !== '.' && $topLevelOnly === false) { $this->getDirFileInfo($sourceDir . $file . DIRECTORY_SEPARATOR, $topLevelOnly, true); } elseif (! is_dir($sourceDir . $file) && $file[0] !== '.') { - $_filedata[$file] = $this->getFileInfo($sourceDir . $file); - $_filedata[$file]['relative_path'] = $relativePath; + $filedata[$file] = $this->getFileInfo($sourceDir . $file); + + $filedata[$file]['relative_path'] = $relativePath; } } - closedir($fp); + closedir($filePointer); - return $_filedata; + return $filedata; } return false; @@ -382,10 +400,19 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, * * @deprecated 4.6.1 Use `get_file_info()` instead. * - * @param string $file Path to file - * @param array|string $returnedValues Array or comma separated string of information returned + * @param string $file Path to file + * @param list|string $returnedValues Array or comma separated string of information returned * - * @return array|false + * @return array{ + * name?: string, + * server_path?: string, + * size?: int, + * date?: int, + * readable?: bool, + * writable?: bool, + * executable?: bool, + * fileperms?: int + * }|false */ protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']) { diff --git a/system/Cache/Handlers/MemcachedHandler.php b/system/Cache/Handlers/MemcachedHandler.php index c6bb6ddcadce..91ac38a9427d 100644 --- a/system/Cache/Handlers/MemcachedHandler.php +++ b/system/Cache/Handlers/MemcachedHandler.php @@ -38,7 +38,7 @@ class MemcachedHandler extends BaseHandler /** * Memcached Configuration * - * @var array + * @var array{host: string, port: int, weight: int, raw: bool} */ protected $config = [ 'host' => '127.0.0.1', @@ -76,43 +76,32 @@ public function initialize() { try { if (class_exists(Memcached::class)) { - // Create new instance of Memcached $this->memcached = new Memcached(); + if ($this->config['raw']) { $this->memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); } - // Add server $this->memcached->addServer( $this->config['host'], $this->config['port'], $this->config['weight'], ); - // attempt to get status of servers $stats = $this->memcached->getStats(); // $stats should be an associate array with a key in the format of host:port. // If it doesn't have the key, we know the server is not working as expected. - if (! isset($stats[$this->config['host'] . ':' . $this->config['port']])) { + if (! is_array($stats) || ! isset($stats[$this->config['host'] . ':' . $this->config['port']])) { throw new CriticalError('Cache: Memcached connection failed.'); } } elseif (class_exists(Memcache::class)) { - // Create new instance of Memcache $this->memcached = new Memcache(); - // Check if we can connect to the server - $canConnect = $this->memcached->connect( - $this->config['host'], - $this->config['port'], - ); - - // If we can't connect, throw a CriticalError exception - if ($canConnect === false) { + if (! $this->memcached->connect($this->config['host'], $this->config['port'])) { throw new CriticalError('Cache: Memcache connection failed.'); } - // Add server, third parameter is persistence and defaults to TRUE. $this->memcached->addServer( $this->config['host'], $this->config['port'], diff --git a/system/Cache/Handlers/PredisHandler.php b/system/Cache/Handlers/PredisHandler.php index bdf5afe0f22f..250ea74a88e0 100644 --- a/system/Cache/Handlers/PredisHandler.php +++ b/system/Cache/Handlers/PredisHandler.php @@ -31,7 +31,13 @@ class PredisHandler extends BaseHandler /** * Default config * - * @var array + * @var array{ + * scheme: string, + * host: string, + * password: string|null, + * port: int, + * timeout: int + * } */ protected $config = [ 'scheme' => 'tcp', diff --git a/system/Cache/Handlers/RedisHandler.php b/system/Cache/Handlers/RedisHandler.php index 80c21e8559e8..3a13ee07f8e4 100644 --- a/system/Cache/Handlers/RedisHandler.php +++ b/system/Cache/Handlers/RedisHandler.php @@ -29,7 +29,13 @@ class RedisHandler extends BaseHandler /** * Default config * - * @var array + * @var array{ + * host: string, + * password: string|null, + * port: int, + * timeout: int, + * database: int, + * } */ protected $config = [ 'host' => '127.0.0.1', diff --git a/system/Cache/ResponseCache.php b/system/Cache/ResponseCache.php index e984b8e3606f..2c92dc5058be 100644 --- a/system/Cache/ResponseCache.php +++ b/system/Cache/ResponseCache.php @@ -40,12 +40,10 @@ final class ResponseCache * * @var bool|list */ - private $cacheQueryString = false; + private array|bool $cacheQueryString = false; /** - * Cache time to live. - * - * @var int seconds + * Cache time to live (TTL) in seconds. */ private int $ttl = 0; @@ -54,10 +52,7 @@ public function __construct(CacheConfig $config, private readonly CacheInterface $this->cacheQueryString = $config->cacheQueryString; } - /** - * @return $this - */ - public function setTtl(int $ttl) + public function setTtl(int $ttl): self { $this->ttl = $ttl; @@ -67,11 +62,9 @@ public function setTtl(int $ttl) /** * Generates the cache key to use from the current request. * - * @param CLIRequest|IncomingRequest $request - * * @internal for testing purposes only */ - public function generateCacheKey($request): string + public function generateCacheKey(CLIRequest|IncomingRequest $request): string { if ($request instanceof CLIRequest) { return md5($request->getPath()); @@ -79,7 +72,7 @@ public function generateCacheKey($request): string $uri = clone $request->getUri(); - $query = $this->cacheQueryString + $query = (bool) $this->cacheQueryString ? $uri->getQuery(is_array($this->cacheQueryString) ? ['only' => $this->cacheQueryString] : []) : ''; @@ -88,10 +81,8 @@ public function generateCacheKey($request): string /** * Caches the response. - * - * @param CLIRequest|IncomingRequest $request */ - public function make($request, ResponseInterface $response): bool + public function make(CLIRequest|IncomingRequest $request, ResponseInterface $response): bool { if ($this->ttl === 0) { return true; @@ -118,12 +109,12 @@ public function make($request, ResponseInterface $response): bool /** * Gets the cached response for the request. - * - * @param CLIRequest|IncomingRequest $request */ - public function get($request, ResponseInterface $response): ?ResponseInterface + public function get(CLIRequest|IncomingRequest $request, ResponseInterface $response): ?ResponseInterface { - if ($cachedResponse = $this->cache->get($this->generateCacheKey($request))) { + $cachedResponse = $this->cache->get($this->generateCacheKey($request)); + + if (is_string($cachedResponse) && $cachedResponse !== '') { $cachedResponse = unserialize($cachedResponse); if ( diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index aa7efbea0468..e7bd35441959 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -255,6 +255,14 @@ function get_filenames( * @param string $sourceDir Path to source * @param bool $topLevelOnly Look only at the top level directory specified? * @param bool $recursion Internal variable to determine recursion status - do not use in calls + * + * @return array */ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $recursion = false): array { @@ -298,10 +306,19 @@ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $r * Options are: name, server_path, size, date, readable, writable, executable, fileperms * Returns false if the file cannot be found. * - * @param string $file Path to file - * @param array|string $returnedValues Array or comma separated string of information returned + * @param string $file Path to file + * @param list|string $returnedValues Array or comma separated string of information returned * - * @return array|null + * @return array{ + * name?: string, + * server_path?: string, + * size?: int, + * date?: int, + * readable?: bool, + * writable?: bool, + * executable?: bool, + * fileperms?: int + * }|null */ function get_file_info(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']) { diff --git a/system/View/Cell.php b/system/View/Cell.php index 79e573f5103e..8325accc7223 100644 --- a/system/View/Cell.php +++ b/system/View/Cell.php @@ -87,7 +87,9 @@ public function render(string $library, $params = null, int $ttl = 0, ?string $c // Is the output cached? $cacheName ??= str_replace(['\\', '/'], '', $class) . $method . md5(serialize($params)); - if ($output = $this->cache->get($cacheName)) { + $output = $this->cache->get($cacheName); + + if (is_string($output) && $output !== '') { return $output; } diff --git a/tests/system/Cache/AbstractFactoriesCacheHandlerTestCase.php b/tests/system/Cache/AbstractFactoriesCacheHandlerTestCase.php new file mode 100644 index 000000000000..7a75239c5b03 --- /dev/null +++ b/tests/system/Cache/AbstractFactoriesCacheHandlerTestCase.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Cache; + +use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; +use CodeIgniter\Config\Factories; +use CodeIgniter\Test\CIUnitTestCase; +use Config\App; + +/** + * @internal + */ +abstract class AbstractFactoriesCacheHandlerTestCase extends CIUnitTestCase +{ + protected FactoriesCache $cache; + protected CacheInterface|FileVarExportHandler $handler; + + abstract protected function createFactoriesCache(): void; + + public function testInstantiate(): void + { + $this->createFactoriesCache(); + $this->assertInstanceOf(FactoriesCache::class, $this->cache); + } + + public function testSave(): void + { + Factories::reset(); + Factories::config('App'); + + $this->createFactoriesCache(); + + $this->cache->save('config'); + + $cachedData = $this->handler->get('FactoriesCache_config'); + + $this->assertIsArray($cachedData); + $this->assertArrayHasKey('aliases', $cachedData); + $this->assertArrayHasKey('instances', $cachedData); + $this->assertArrayHasKey('App', $cachedData['aliases']); + } + + public function testLoad(): void + { + Factories::reset(); + + /** @var App $appConfig */ + $appConfig = Factories::config('App'); + + $appConfig->baseURL = 'http://test.example.jp/this-is-test/'; + + $this->createFactoriesCache(); + $this->cache->save('config'); + + Factories::reset(); + $this->cache->load('config'); + + /** @var App $appConfig */ + $appConfig = Factories::config('App'); + $this->assertSame('http://test.example.jp/this-is-test/', $appConfig->baseURL); + } + + public function testDelete(): void + { + $this->createFactoriesCache(); + + $this->cache->delete('config'); + + $this->assertFalse($this->cache->load('config')); + } +} diff --git a/tests/system/Cache/CacheFactoryTest.php b/tests/system/Cache/CacheFactoryTest.php index 8a76462b2cea..c51dce6830c3 100644 --- a/tests/system/Cache/CacheFactoryTest.php +++ b/tests/system/Cache/CacheFactoryTest.php @@ -26,15 +26,12 @@ final class CacheFactoryTest extends CIUnitTestCase { private static string $directory = 'CacheFactory'; - private CacheFactory $cacheFactory; private Cache $config; protected function setUp(): void { parent::setUp(); - $this->cacheFactory = new CacheFactory(); - // Initialize path $this->config = new Cache(); $this->config->file['storePath'] .= self::$directory; @@ -48,11 +45,6 @@ protected function tearDown(): void } } - public function testNew(): void - { - $this->assertInstanceOf(CacheFactory::class, $this->cacheFactory); - } - public function testGetHandlerExceptionCacheInvalidHandlers(): void { $this->expectException(CacheException::class); @@ -60,7 +52,7 @@ public function testGetHandlerExceptionCacheInvalidHandlers(): void $this->config->validHandlers = []; - $this->cacheFactory->getHandler($this->config); + CacheFactory::getHandler($this->config); } public function testGetHandlerExceptionCacheHandlerNotFound(): void @@ -70,7 +62,7 @@ public function testGetHandlerExceptionCacheHandlerNotFound(): void unset($this->config->validHandlers[$this->config->handler]); - $this->cacheFactory->getHandler($this->config); + CacheFactory::getHandler($this->config); } public function testGetDummyHandler(): void @@ -81,7 +73,7 @@ public function testGetDummyHandler(): void $this->config->handler = 'dummy'; - $this->assertInstanceOf(DummyHandler::class, $this->cacheFactory->getHandler($this->config)); + $this->assertInstanceOf(DummyHandler::class, CacheFactory::getHandler($this->config)); // Initialize path $this->config = new Cache(); @@ -99,7 +91,7 @@ public function testHandlesBadHandler(): void if (is_windows()) { $this->markTestSkipped('Cannot test this properly on Windows.'); } else { - $this->assertInstanceOf(DummyHandler::class, $this->cacheFactory->getHandler($this->config, 'wincache', 'wincache')); + $this->assertInstanceOf(DummyHandler::class, CacheFactory::getHandler($this->config, 'wincache', 'wincache')); } // Initialize path diff --git a/tests/system/Cache/CacheMockTest.php b/tests/system/Cache/CacheMockTest.php index c867bebc4249..9fe4538c2760 100644 --- a/tests/system/Cache/CacheMockTest.php +++ b/tests/system/Cache/CacheMockTest.php @@ -35,6 +35,7 @@ public function testMockReturnsMockCacheClass(): void public function testMockCaching(): void { + /** @var MockCache $mock */ $mock = mock(CacheFactory::class); // Ensure it stores the value normally diff --git a/tests/system/Cache/FactoriesCacheFileHandlerTest.php b/tests/system/Cache/FactoriesCacheFileHandlerTest.php index 01e3276733e3..660c476cc799 100644 --- a/tests/system/Cache/FactoriesCacheFileHandlerTest.php +++ b/tests/system/Cache/FactoriesCacheFileHandlerTest.php @@ -13,24 +13,18 @@ namespace CodeIgniter\Cache; -use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; -use Config\Cache as CacheConfig; +use Config\Cache; use PHPUnit\Framework\Attributes\Group; /** * @internal */ #[Group('Others')] -final class FactoriesCacheFileHandlerTest extends FactoriesCacheFileVarExportHandlerTest +final class FactoriesCacheFileHandlerTest extends AbstractFactoriesCacheHandlerTestCase { - /** - * @var CacheInterface|FileVarExportHandler - */ - protected $handler; - protected function createFactoriesCache(): void { - $this->handler = CacheFactory::getHandler(new CacheConfig(), 'file'); + $this->handler = CacheFactory::getHandler(new Cache(), 'file'); $this->cache = new FactoriesCache($this->handler); } } diff --git a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php index 8bf5c79d9209..c71c756f5cdd 100644 --- a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php +++ b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php @@ -14,78 +14,17 @@ namespace CodeIgniter\Cache; use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; -use CodeIgniter\Config\Factories; -use CodeIgniter\Test\CIUnitTestCase; -use Config\App; use PHPUnit\Framework\Attributes\Group; /** * @internal - * @no-final */ #[Group('Others')] -class FactoriesCacheFileVarExportHandlerTest extends CIUnitTestCase +final class FactoriesCacheFileVarExportHandlerTest extends AbstractFactoriesCacheHandlerTestCase { - protected FactoriesCache $cache; - - /** - * @var CacheInterface|FileVarExportHandler - */ - protected $handler; - protected function createFactoriesCache(): void { $this->handler = new FileVarExportHandler(); $this->cache = new FactoriesCache($this->handler); } - - public function testInstantiate(): void - { - $this->createFactoriesCache(); - - $this->assertInstanceOf(FactoriesCache::class, $this->cache); - } - - public function testSave(): void - { - Factories::reset(); - Factories::config('App'); - - $this->createFactoriesCache(); - - $this->cache->save('config'); - - $cachedData = $this->handler->get('FactoriesCache_config'); - - $this->assertArrayHasKey('aliases', $cachedData); - $this->assertArrayHasKey('instances', $cachedData); - $this->assertArrayHasKey('App', $cachedData['aliases']); - } - - public function testLoad(): void - { - Factories::reset(); - /** @var App $appConfig */ - $appConfig = Factories::config('App'); - $appConfig->baseURL = 'http://test.example.jp/this-is-test/'; - - $this->createFactoriesCache(); - $this->cache->save('config'); - - Factories::reset(); - - $this->cache->load('config'); - - $appConfig = Factories::config('App'); - $this->assertSame('http://test.example.jp/this-is-test/', $appConfig->baseURL); - } - - public function testDelete(): void - { - $this->createFactoriesCache(); - - $this->cache->delete('config'); - - $this->assertFalse($this->cache->load('config')); - } } diff --git a/tests/system/Cache/Handlers/BaseHandlerTest.php b/tests/system/Cache/Handlers/BaseHandlerTest.php index 06d3c4a98f8e..4cba697e5e00 100644 --- a/tests/system/Cache/Handlers/BaseHandlerTest.php +++ b/tests/system/Cache/Handlers/BaseHandlerTest.php @@ -25,11 +25,8 @@ #[Group('Others')] final class BaseHandlerTest extends CIUnitTestCase { - /** - * @param mixed $input - */ #[DataProvider('provideValidateKeyInvalidType')] - public function testValidateKeyInvalidType($input): void + public function testValidateKeyInvalidType(mixed $input): void { $this->expectException('InvalidArgumentException'); $this->expectExceptionMessage('Cache key must be a string'); @@ -37,14 +34,17 @@ public function testValidateKeyInvalidType($input): void BaseHandler::validateKey($input); } + /** + * @return iterable + */ public static function provideValidateKeyInvalidType(): iterable { - return [ - [true], - [false], - [null], - [42], - [new stdClass()], + yield from [ + 'true' => [true], + 'false' => [false], + 'null' => [null], + 'int' => [42], + 'object' => [new stdClass()], ]; } diff --git a/tests/system/Cache/Handlers/DummyHandlerTest.php b/tests/system/Cache/Handlers/DummyHandlerTest.php index c9c8a0fa0937..ab3b3ccd643d 100644 --- a/tests/system/Cache/Handlers/DummyHandlerTest.php +++ b/tests/system/Cache/Handlers/DummyHandlerTest.php @@ -13,7 +13,9 @@ namespace CodeIgniter\Cache\Handlers; +use CodeIgniter\Cache\CacheFactory; use CodeIgniter\Test\CIUnitTestCase; +use Config\Cache; use PHPUnit\Framework\Attributes\Group; /** @@ -26,8 +28,7 @@ final class DummyHandlerTest extends CIUnitTestCase protected function setUp(): void { - $this->handler = new DummyHandler(); - $this->handler->initialize(); + $this->handler = CacheFactory::getHandler(new Cache(), 'dummy'); } public function testNew(): void diff --git a/tests/system/Cache/Handlers/FileHandlerTest.php b/tests/system/Cache/Handlers/FileHandlerTest.php index ed163071aede..0819a5a9df06 100644 --- a/tests/system/Cache/Handlers/FileHandlerTest.php +++ b/tests/system/Cache/Handlers/FileHandlerTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Cache\Handlers; +use CodeIgniter\Cache\CacheFactory; use CodeIgniter\Cache\Exceptions\CacheException; use CodeIgniter\CLI\CLI; use CodeIgniter\I18n\Time; @@ -58,8 +59,7 @@ protected function setUp(): void mkdir($this->config->file['storePath'], 0777, true); } - $this->handler = new FileHandler($this->config); - $this->handler->initialize(); + $this->handler = CacheFactory::getHandler($this->config, 'file'); } protected function tearDown(): void @@ -98,19 +98,16 @@ public function testNewWithNonWritablePath(): void $this->expectException(CacheException::class); chmod($this->config->file['storePath'], 0444); - new FileHandler($this->config); + CacheFactory::getHandler($this->config, 'file'); } public function testSetDefaultPath(): void { // Initialize path - $config = new Cache(); - $config->file['storePath'] = null; + $config = new Cache(); + unset($config->file['storePath']); - $this->handler = new FileHandler($config); - $this->handler->initialize(); - - $this->assertInstanceOf(FileHandler::class, $this->handler); + $this->assertInstanceOf(FileHandler::class, CacheFactory::getHandler($config, 'file')); } /** @@ -245,9 +242,8 @@ public function testIncrement(): void public function testIncrementWithDefaultPrefix(): void { $this->config->prefix = 'test_'; - $this->handler = new FileHandler($this->config); - $this->handler->initialize(); + $this->handler = CacheFactory::getHandler($this->config, 'file'); $this->handler->save(self::$key1, 1); $this->handler->save(self::$key2, 'value'); @@ -320,9 +316,7 @@ public function testSaveMode(int $int, string $string): void $config = new Cache(); $config->file['mode'] = $int; - $this->handler = new FileHandler($config); - $this->handler->initialize(); - + $this->handler = CacheFactory::getHandler($config, 'file'); $this->handler->save(self::$key1, 'value'); $file = $config->file['storePath'] . DIRECTORY_SEPARATOR . self::$key1; @@ -346,10 +340,17 @@ public static function provideSaveMode(): iterable public function testFileHandler(): void { - $fileHandler = new BaseTestFileHandler(); + $cache = new Cache(); - $actual = $fileHandler->getFileInfoTest(); + $cache->validHandlers['file'] = BaseTestFileHandler::class; + + /** @var BaseTestFileHandler $fileHandler */ + $fileHandler = CacheFactory::getHandler($cache, 'file'); + $this->assertInstanceOf(BaseTestFileHandler::class, $fileHandler); + $actual = $fileHandler->getFileInfoTest(); + $this->assertIsArray($actual); + $this->assertArrayHasKey('name', $actual); $this->assertArrayHasKey('server_path', $actual); $this->assertArrayHasKey('size', $actual); $this->assertArrayHasKey('date', $actual); @@ -389,6 +390,9 @@ public function testGetItemWithCorruptedData(): void } } +/** + * @internal + */ final class BaseTestFileHandler extends FileHandler { private static string $directory = 'FileHandler'; @@ -405,7 +409,16 @@ public function __construct() } /** - * @return array|null + * @return array{ + * name: string, + * server_path: string, + * size: int, + * date: int, + * readable: bool, + * writable: bool, + * executable: bool, + * fileperms: int, + * }|null */ public function getFileInfoTest(): ?array { diff --git a/tests/system/Cache/Handlers/MemcachedHandlerTest.php b/tests/system/Cache/Handlers/MemcachedHandlerTest.php index c6cfdb8543ee..f4114d4742e7 100644 --- a/tests/system/Cache/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Cache/Handlers/MemcachedHandlerTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Cache\Handlers; +use CodeIgniter\Cache\CacheFactory; use CodeIgniter\CLI\CLI; use CodeIgniter\Exceptions\BadMethodCallException; use CodeIgniter\I18n\Time; @@ -45,11 +46,7 @@ protected function setUp(): void $this->markTestSkipped('Memcached extension not loaded.'); } - $config = new Cache(); - - $this->handler = new MemcachedHandler($config); - - $this->handler->initialize(); + $this->handler = CacheFactory::getHandler(new Cache(), 'memcached'); } protected function tearDown(): void @@ -139,9 +136,8 @@ public function testIncrement(): void $config = new Cache(); $config->memcached['raw'] = true; - $memcachedHandler = new MemcachedHandler($config); - $memcachedHandler->initialize(); + $memcachedHandler = CacheFactory::getHandler($config, 'memcached'); $memcachedHandler->save(self::$key1, 1); $memcachedHandler->save(self::$key2, 'value'); @@ -158,9 +154,8 @@ public function testDecrement(): void $config = new Cache(); $config->memcached['raw'] = true; - $memcachedHandler = new MemcachedHandler($config); - $memcachedHandler->initialize(); + $memcachedHandler = CacheFactory::getHandler($config, 'memcached'); $memcachedHandler->save(self::$key1, 10); $memcachedHandler->save(self::$key2, 'value'); diff --git a/tests/system/Cache/Handlers/PredisHandlerTest.php b/tests/system/Cache/Handlers/PredisHandlerTest.php index 4b009582cd55..eec292f280ad 100644 --- a/tests/system/Cache/Handlers/PredisHandlerTest.php +++ b/tests/system/Cache/Handlers/PredisHandlerTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Cache\Handlers; +use CodeIgniter\Cache\CacheFactory; use CodeIgniter\CLI\CLI; use CodeIgniter\I18n\Time; use Config\Cache; @@ -42,11 +43,8 @@ protected function setUp(): void { parent::setUp(); - $this->config = new Cache(); - - $this->handler = new PredisHandler($this->config); - - $this->handler->initialize(); + $this->config = new Cache(); + $this->handler = CacheFactory::getHandler($this->config, 'predis'); } protected function tearDown(): void @@ -63,9 +61,7 @@ public function testNew(): void public function testDestruct(): void { - $this->handler = new PredisHandler($this->config); - $this->handler->initialize(); - + $this->handler = CacheFactory::getHandler($this->config, 'predis'); $this->assertInstanceOf(PredisHandler::class, $this->handler); } diff --git a/tests/system/Cache/Handlers/RedisHandlerTest.php b/tests/system/Cache/Handlers/RedisHandlerTest.php index 812985e65e8d..467abc675372 100644 --- a/tests/system/Cache/Handlers/RedisHandlerTest.php +++ b/tests/system/Cache/Handlers/RedisHandlerTest.php @@ -48,11 +48,8 @@ protected function setUp(): void $this->markTestSkipped('redis extension not loaded.'); } - $this->config = new Cache(); - - $this->handler = new RedisHandler($this->config); - - $this->handler->initialize(); + $this->config = new Cache(); + $this->handler = CacheFactory::getHandler($this->config, 'redis'); } protected function tearDown(): void @@ -69,9 +66,7 @@ public function testNew(): void public function testDestruct(): void { - $this->handler = new RedisHandler($this->config); - $this->handler->initialize(); - + $this->handler = CacheFactory::getHandler($this->config, 'redis'); $this->assertInstanceOf(RedisHandler::class, $this->handler); } diff --git a/tests/system/Cache/ResponseCacheTest.php b/tests/system/Cache/ResponseCacheTest.php index 11c82db71247..b3f0ec832905 100644 --- a/tests/system/Cache/ResponseCacheTest.php +++ b/tests/system/Cache/ResponseCacheTest.php @@ -21,8 +21,9 @@ use CodeIgniter\HTTP\SiteURI; use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Test\CIUnitTestCase; -use Config\App as AppConfig; -use Config\Cache as CacheConfig; +use CodeIgniter\Test\Mock\MockCache; +use Config\App; +use Config\Cache; use ErrorException; use PHPUnit\Framework\Attributes\BackupGlobals; use PHPUnit\Framework\Attributes\Group; @@ -34,138 +35,112 @@ #[Group('Others')] final class ResponseCacheTest extends CIUnitTestCase { - private AppConfig $appConfig; - - protected function setUp(): void + /** + * @param array $query + */ + private function createIncomingRequest(string $uri = '', array $query = [], App $app = new App()): IncomingRequest { - parent::setUp(); - - $this->appConfig = new AppConfig(); - } + $superglobals = service('superglobals'); + $superglobals->setServer('REQUEST_URI', sprintf('/%s%s', $uri, $query !== [] ? '?' . http_build_query($query) : '')); + $superglobals->setServer('SCRIPT_NAME', '/index.php'); - private function createIncomingRequest( - string $uri = '', - array $query = [], - ?AppConfig $appConfig = null, - ): IncomingRequest { - $_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = []; + $siteUri = new SiteURI($app, $uri); - $_SERVER['REQUEST_URI'] = '/' . $uri . ($query !== [] ? '?' . http_build_query($query) : ''); - $_SERVER['SCRIPT_NAME'] = '/index.php'; - - $appConfig ??= $this->appConfig; - - $siteUri = new SiteURI($appConfig, $uri); if ($query !== []) { - $_GET = $_REQUEST = $query; $siteUri->setQueryArray($query); } - return new IncomingRequest( - $appConfig, - $siteUri, - null, - new UserAgent(), - ); + return new IncomingRequest($app, $siteUri, null, new UserAgent()); } /** * @param list $params */ - private function createCLIRequest(array $params = [], ?AppConfig $appConfig = null): CLIRequest + private function createCLIRequest(array $params = [], App $app = new App()): CLIRequest { - $_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = []; + $_SERVER['argv'] = ['public/index.php', ...$params]; - $_SERVER['argv'] = ['public/index.php', ...$params]; - $_SERVER['SCRIPT_NAME'] = 'public/index.php'; + $superglobals = service('superglobals'); + $superglobals->setServer('SCRIPT_NAME', 'public/index.php'); - $appConfig ??= $this->appConfig; - - return new CLIRequest($appConfig); + return new CLIRequest($app); } - private function createResponseCache(?CacheConfig $cacheConfig = null): ResponseCache + private function createResponseCache(Cache $cache = new Cache()): ResponseCache { - $cache = mock(CacheFactory::class); - - $cacheConfig ??= new CacheConfig(); + /** @var MockCache $mockCache */ + $mockCache = mock(CacheFactory::class); - return (new ResponseCache($cacheConfig, $cache))->setTtl(300); + return (new ResponseCache($cache, $mockCache))->setTtl(300); } public function testCachePageIncomingRequest(): void { $pageCache = $this->createResponseCache(); - $request = $this->createIncomingRequest('foo/bar'); - - $response = new Response($this->appConfig); + $response = new Response(new App()); $response->setHeader('ETag', 'abcd1234'); $response->setBody('The response body.'); - $return = $pageCache->make($request, $response); - - $this->assertTrue($return); + $this->assertTrue($pageCache->make( + $this->createIncomingRequest('foo/bar'), + $response, + )); // Check cache with a request with the same URI path. - $request = $this->createIncomingRequest('foo/bar'); - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response(new App())); $this->assertInstanceOf(ResponseInterface::class, $cachedResponse); $this->assertSame('The response body.', $cachedResponse->getBody()); $this->assertSame('abcd1234', $cachedResponse->getHeaderLine('ETag')); // Check cache with a request with the same URI path and different query string. - $request = $this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']); - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get( + $this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']), + new Response(new App()), + ); $this->assertInstanceOf(ResponseInterface::class, $cachedResponse); $this->assertSame('The response body.', $cachedResponse->getBody()); $this->assertSame('abcd1234', $cachedResponse->getHeaderLine('ETag')); // Check cache with another request with the different URI path. - $request = $this->createIncomingRequest('another'); - - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get($this->createIncomingRequest('another'), new Response(new App())); $this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse); } public function testCachePageIncomingRequestWithCacheQueryString(): void { - $cacheConfig = new CacheConfig(); - $cacheConfig->cacheQueryString = true; - $pageCache = $this->createResponseCache($cacheConfig); + $cache = new Cache(); + + $cache->cacheQueryString = true; + + $pageCache = $this->createResponseCache($cache); $request = $this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']); - $response = new Response($this->appConfig); + $response = new Response(new App()); $response->setHeader('ETag', 'abcd1234'); $response->setBody('The response body.'); - $return = $pageCache->make($request, $response); - - $this->assertTrue($return); + $this->assertTrue($pageCache->make($request, $response)); // Check cache with a request with the same URI path and same query string. - $this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']); - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get( + $this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']), + new Response(new App()), + ); $this->assertInstanceOf(ResponseInterface::class, $cachedResponse); $this->assertSame('The response body.', $cachedResponse->getBody()); $this->assertSame('abcd1234', $cachedResponse->getHeaderLine('ETag')); // Check cache with a request with the same URI path and different query string. - $request = $this->createIncomingRequest('foo/bar', ['xfoo' => 'bar', 'bar' => 'baz']); - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get( + $this->createIncomingRequest('foo/bar', ['xfoo' => 'bar', 'bar' => 'baz']), + new Response(new App()), + ); $this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse); // Check cache with another request with the different URI path. - $request = $this->createIncomingRequest('another'); - - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get($this->createIncomingRequest('another'), new Response(new App())); $this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse); } @@ -173,19 +148,16 @@ public function testCachePageIncomingRequestWithHttpMethods(): void { $pageCache = $this->createResponseCache(); - $request = $this->createIncomingRequest('foo/bar'); - - $response = new Response($this->appConfig); + $response = new Response(new App()); $response->setBody('The response body.'); - $return = $pageCache->make($request, $response); - - $this->assertTrue($return); + $this->assertTrue($pageCache->make($this->createIncomingRequest('foo/bar'), $response)); // Check cache with a request with the same URI path and different HTTP method - $request = $this->createIncomingRequest('foo/bar')->withMethod('POST'); - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get( + $this->createIncomingRequest('foo/bar')->withMethod('POST'), + new Response(new App()), + ); $this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse); } @@ -193,27 +165,18 @@ public function testCachePageCLIRequest(): void { $pageCache = $this->createResponseCache(); - $request = $this->createCLIRequest(['foo', 'bar']); - - $response = new Response($this->appConfig); + $response = new Response(new App()); $response->setBody('The response body.'); - $return = $pageCache->make($request, $response); - - $this->assertTrue($return); + $this->assertTrue($pageCache->make($this->createCLIRequest(['foo', 'bar']), $response)); // Check cache with a request with the same params. - $request = $this->createCLIRequest(['foo', 'bar']); - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get($this->createCLIRequest(['foo', 'bar']), new Response(new App())); $this->assertInstanceOf(ResponseInterface::class, $cachedResponse); $this->assertSame('The response body.', $cachedResponse->getBody()); // Check cache with another request with the different params. - $request = $this->createCLIRequest(['baz']); - - $cachedResponse = $pageCache->get($request, new Response($this->appConfig)); - + $cachedResponse = $pageCache->get($this->createCLIRequest(['baz']), new Response(new App())); $this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse); } @@ -222,13 +185,13 @@ public function testUnserializeError(): void $this->expectException(ErrorException::class); $this->expectExceptionMessage('unserialize(): Error at offset 0 of 12 bytes'); - $cache = mock(CacheFactory::class); - $cacheConfig = new CacheConfig(); - $pageCache = new ResponseCache($cacheConfig, $cache); + /** @var MockCache $mockCache */ + $mockCache = mock(CacheFactory::class); + $pageCache = new ResponseCache(new Cache(), $mockCache); $request = $this->createIncomingRequest('foo/bar'); - $response = new Response($this->appConfig); + $response = new Response(new App()); $response->setHeader('ETag', 'abcd1234'); $response->setBody('The response body.'); @@ -237,10 +200,10 @@ public function testUnserializeError(): void $cacheKey = $pageCache->generateCacheKey($request); // Save invalid data. - $cache->save($cacheKey, 'Invalid data'); + $mockCache->save($cacheKey, 'Invalid data'); // Check cache with a request with the same URI path. - $pageCache->get($request, new Response($this->appConfig)); + $pageCache->get($request, new Response(new App())); } public function testInvalidCacheError(): void @@ -248,13 +211,13 @@ public function testInvalidCacheError(): void $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Error unserializing page cache'); - $cache = mock(CacheFactory::class); - $cacheConfig = new CacheConfig(); - $pageCache = new ResponseCache($cacheConfig, $cache); + /** @var MockCache $mockCache */ + $mockCache = mock(CacheFactory::class); + $pageCache = new ResponseCache(new Cache(), $mockCache); $request = $this->createIncomingRequest('foo/bar'); - $response = new Response($this->appConfig); + $response = new Response(new App()); $response->setHeader('ETag', 'abcd1234'); $response->setBody('The response body.'); @@ -263,9 +226,9 @@ public function testInvalidCacheError(): void $cacheKey = $pageCache->generateCacheKey($request); // Save invalid data. - $cache->save($cacheKey, serialize(['a' => '1'])); + $mockCache->save($cacheKey, serialize(['a' => '1'])); // Check cache with a request with the same URI path. - $pageCache->get($request, new Response($this->appConfig)); + $pageCache->get($request, new Response(new App())); } } diff --git a/utils/phpstan-baseline/codeigniter.cacheHandlerInstance.neon b/utils/phpstan-baseline/codeigniter.cacheHandlerInstance.neon deleted file mode 100644 index f9225bcfb5dd..000000000000 --- a/utils/phpstan-baseline/codeigniter.cacheHandlerInstance.neon +++ /dev/null @@ -1,33 +0,0 @@ -# total 14 errors - -parameters: - ignoreErrors: - - - message: '#^Calling new DummyHandler\(\) directly is incomplete to get the cache instance\.$#' - count: 1 - path: ../../tests/system/Cache/Handlers/DummyHandlerTest.php - - - - message: '#^Calling new BaseTestFileHandler\(\) directly is incomplete to get the cache instance\.$#' - count: 1 - path: ../../tests/system/Cache/Handlers/FileHandlerTest.php - - - - message: '#^Calling new FileHandler\(\) directly is incomplete to get the cache instance\.$#' - count: 5 - path: ../../tests/system/Cache/Handlers/FileHandlerTest.php - - - - message: '#^Calling new MemcachedHandler\(\) directly is incomplete to get the cache instance\.$#' - count: 3 - path: ../../tests/system/Cache/Handlers/MemcachedHandlerTest.php - - - - message: '#^Calling new PredisHandler\(\) directly is incomplete to get the cache instance\.$#' - count: 2 - path: ../../tests/system/Cache/Handlers/PredisHandlerTest.php - - - - message: '#^Calling new RedisHandler\(\) directly is incomplete to get the cache instance\.$#' - count: 2 - path: ../../tests/system/Cache/Handlers/RedisHandlerTest.php diff --git a/utils/phpstan-baseline/codeigniter.getReassignArray.neon b/utils/phpstan-baseline/codeigniter.getReassignArray.neon index e02f1107c29c..b661a9e9d7de 100644 --- a/utils/phpstan-baseline/codeigniter.getReassignArray.neon +++ b/utils/phpstan-baseline/codeigniter.getReassignArray.neon @@ -1,12 +1,7 @@ -# total 22 errors +# total 19 errors parameters: ignoreErrors: - - - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' - count: 3 - path: ../../tests/system/Cache/ResponseCacheTest.php - - message: '#^Re\-assigning arrays to \$_GET directly is discouraged\.$#' count: 3 diff --git a/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon b/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon index 4e5600bcc34c..4a3113467afe 100644 --- a/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon +++ b/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon @@ -1,4 +1,4 @@ -# total 466 errors +# total 463 errors parameters: ignoreErrors: @@ -22,21 +22,6 @@ parameters: count: 1 path: ../../tests/system/CLI/ConsoleTest.php - - - message: '#^Assigning ''/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Cache/ResponseCacheTest.php - - - - message: '#^Assigning ''public/index\.php'' directly on offset ''SCRIPT_NAME'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Cache/ResponseCacheTest.php - - - - message: '#^Assigning non\-falsy\-string directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/Cache/ResponseCacheTest.php - - message: '#^Assigning ''/'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' count: 2 diff --git a/utils/phpstan-baseline/empty.notAllowed.neon b/utils/phpstan-baseline/empty.notAllowed.neon index 91a3123c5d6c..9bdf7e5f5720 100644 --- a/utils/phpstan-baseline/empty.notAllowed.neon +++ b/utils/phpstan-baseline/empty.notAllowed.neon @@ -1,12 +1,7 @@ -# total 240 errors +# total 239 errors parameters: ignoreErrors: - - - message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#' count: 1 diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 03b3f826d494..f48b806c95ed 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,8 +1,7 @@ -# total 2932 errors +# total 2866 errors includes: - argument.type.neon - assign.propertyType.neon - - codeigniter.cacheHandlerInstance.neon - codeigniter.getReassignArray.neon - codeigniter.modelArgumentType.neon - codeigniter.superglobalAccess.neon diff --git a/utils/phpstan-baseline/missingType.iterableValue.neon b/utils/phpstan-baseline/missingType.iterableValue.neon index 6d974d9bb30b..4a4d55f542f2 100644 --- a/utils/phpstan-baseline/missingType.iterableValue.neon +++ b/utils/phpstan-baseline/missingType.iterableValue.neon @@ -1,4 +1,4 @@ -# total 1479 errors +# total 1436 errors parameters: ignoreErrors: @@ -242,196 +242,6 @@ parameters: count: 1 path: ../../system/CLI/Console.php - - - message: '#^Method CodeIgniter\\Cache\\CacheInterface\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/CacheInterface.php - - - - message: '#^Method CodeIgniter\\Cache\\CacheInterface\:\:getCacheInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/CacheInterface.php - - - - message: '#^Method CodeIgniter\\Cache\\CacheInterface\:\:getMetaData\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/CacheInterface.php - - - - message: '#^Method CodeIgniter\\Cache\\CacheInterface\:\:save\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/CacheInterface.php - - - - message: '#^Method CodeIgniter\\Cache\\FactoriesCache\\FileVarExportHandler\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/FactoriesCache/FileVarExportHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\FactoriesCache\\FileVarExportHandler\:\:save\(\) has parameter \$val with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/FactoriesCache/FileVarExportHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\BaseHandler\:\:remember\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/BaseHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\DummyHandler\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/DummyHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\DummyHandler\:\:getCacheInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/DummyHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\DummyHandler\:\:getMetaData\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/DummyHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\DummyHandler\:\:remember\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/DummyHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\DummyHandler\:\:save\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/DummyHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\FileHandler\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\FileHandler\:\:getCacheInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\FileHandler\:\:getDirFileInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\FileHandler\:\:getFileInfo\(\) has parameter \$returnedValues with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\FileHandler\:\:getFileInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\FileHandler\:\:getMetaData\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\FileHandler\:\:save\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\MemcachedHandler\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/MemcachedHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\MemcachedHandler\:\:getCacheInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/MemcachedHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\MemcachedHandler\:\:getMetaData\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/MemcachedHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\MemcachedHandler\:\:save\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/MemcachedHandler.php - - - - message: '#^Property CodeIgniter\\Cache\\Handlers\\MemcachedHandler\:\:\$config type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/MemcachedHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\PredisHandler\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/PredisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\PredisHandler\:\:getCacheInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/PredisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\PredisHandler\:\:getMetaData\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/PredisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\PredisHandler\:\:save\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/PredisHandler.php - - - - message: '#^Property CodeIgniter\\Cache\\Handlers\\PredisHandler\:\:\$config type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/PredisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\RedisHandler\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/RedisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\RedisHandler\:\:getCacheInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/RedisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\RedisHandler\:\:getMetaData\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/RedisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\RedisHandler\:\:save\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/RedisHandler.php - - - - message: '#^Property CodeIgniter\\Cache\\Handlers\\RedisHandler\:\:\$config type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/RedisHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\WincacheHandler\:\:get\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/WincacheHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\WincacheHandler\:\:getCacheInfo\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/WincacheHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\WincacheHandler\:\:getMetaData\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/WincacheHandler.php - - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\WincacheHandler\:\:save\(\) has parameter \$value with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Cache/Handlers/WincacheHandler.php - - message: '#^Method CodeIgniter\\CodeIgniter\:\:getPerformanceStats\(\) return type has no value type specified in iterable type array\.$#' count: 1 @@ -3947,21 +3757,6 @@ parameters: count: 1 path: ../../system/Helpers/filesystem_helper.php - - - message: '#^Function get_dir_file_info\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Helpers/filesystem_helper.php - - - - message: '#^Function get_file_info\(\) has parameter \$returnedValues with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Helpers/filesystem_helper.php - - - - message: '#^Function get_file_info\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Helpers/filesystem_helper.php - - message: '#^Function get_filenames\(\) return type has no value type specified in iterable type array\.$#' count: 1 @@ -5742,16 +5537,6 @@ parameters: count: 1 path: ../../tests/system/CLI/CLITest.php - - - message: '#^Method CodeIgniter\\Cache\\Handlers\\BaseHandlerTest\:\:provideValidateKeyInvalidType\(\) return type has no value type specified in iterable type iterable\.$#' - count: 1 - path: ../../tests/system/Cache/Handlers/BaseHandlerTest.php - - - - message: '#^Method CodeIgniter\\Cache\\ResponseCacheTest\:\:createIncomingRequest\(\) has parameter \$query with no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Cache/ResponseCacheTest.php - - message: '#^Method CodeIgniter\\CodeIgniterTest\:\:providePageCacheWithCacheQueryString\(\) return type has no value type specified in iterable type iterable\.$#' count: 1 diff --git a/utils/phpstan-baseline/property.notFound.neon b/utils/phpstan-baseline/property.notFound.neon index 9c0dfe2005a3..f7f0db25ce63 100644 --- a/utils/phpstan-baseline/property.notFound.neon +++ b/utils/phpstan-baseline/property.notFound.neon @@ -1,4 +1,4 @@ -# total 59 errors +# total 58 errors parameters: ignoreErrors: @@ -27,11 +27,6 @@ parameters: count: 1 path: ../../system/Session/Handlers/RedisHandler.php - - - message: '#^Access to an undefined property CodeIgniter\\Config\\BaseConfig\:\:\$baseURL\.$#' - count: 1 - path: ../../tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php - - message: '#^Access to an undefined property Tests\\Support\\Commands\\AppInfo\:\:\$foobar\.$#' count: 2 diff --git a/utils/phpstan-baseline/ternary.shortNotAllowed.neon b/utils/phpstan-baseline/ternary.shortNotAllowed.neon index 18051955d6f7..d0ed19c4567e 100644 --- a/utils/phpstan-baseline/ternary.shortNotAllowed.neon +++ b/utils/phpstan-baseline/ternary.shortNotAllowed.neon @@ -1,4 +1,4 @@ -# total 35 errors +# total 34 errors parameters: ignoreErrors: @@ -7,11 +7,6 @@ parameters: count: 2 path: ../../system/CLI/CLI.php - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Cache/Handlers/FileHandler.php - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' count: 1