From 2969640d4a4bf2cf4d345e98faee6d42979e25b9 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sat, 21 Oct 2023 15:54:00 +0800 Subject: [PATCH 1/5] Update phpstan-codeigniter to v1.4.1 --- composer.json | 2 +- phpstan.neon.dist | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 0151c1f982a8..9e0288c26f6b 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "require-dev": { "codeigniter/coding-standard": "^1.5", - "codeigniter/phpstan-codeigniter": "^v1.1", + "codeigniter/phpstan-codeigniter": "^1.4", "ergebnis/composer-normalize": "^2.28", "fakerphp/faker": "^1.9", "kint-php/kint": "^5.0.4", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 1fcd28307cf5..4041710fb703 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -38,6 +38,3 @@ parameters: booleansInConditions: true disallowedConstructs: true matchingInheritedMethodNames: true - codeigniter: - additionalConfigNamespaces: - - CodeIgniter\Config\ From 94130de237e84aee778ca88604da6c0ac527356c Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sat, 21 Oct 2023 15:59:29 +0800 Subject: [PATCH 2/5] Fix Modules does not extend BaseConfig --- phpstan-baseline.php | 10 ---------- system/Config/BaseConfig.php | 2 +- system/Config/BaseService.php | 8 ++------ .../Cache/FactoriesCacheFileVarExportHandlerTest.php | 2 -- 4 files changed, 3 insertions(+), 19 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index d1c574463f58..0d372afdb97c 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -871,16 +871,6 @@ 'count' => 4, 'path' => __DIR__ . '/system/Config/BaseConfig.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Argument \\#1 \\$name \\(\'Config\\\\\\\\Modules\'\\) passed to function config does not extend CodeIgniter\\\\\\\\Config\\\\\\\\BaseConfig\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Config/BaseConfig.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Argument \\#1 \\$name \\(\'Config\\\\\\\\Modules\'\\) passed to function config does not extend CodeIgniter\\\\\\\\Config\\\\\\\\BaseConfig\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/system/Config/BaseService.php', -]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', 'count' => 3, diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 87166737e746..b4b9a4a21349 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -82,7 +82,7 @@ public static function __set_state(array $array) */ public function __construct() { - static::$moduleConfig = config(Modules::class); + static::$moduleConfig = new Modules(); if (! static::$override) { return; diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index b66b8c9f535e..22daf7b69e4b 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -330,9 +330,7 @@ public static function injectMock(string $name, $mock) protected static function discoverServices(string $name, array $arguments) { if (! static::$discovered) { - $config = config(Modules::class); - - if ($config->shouldDiscover('services')) { + if ((new Modules())->shouldDiscover('services')) { $locator = static::locator(); $files = $locator->search('Config/Services'); @@ -372,9 +370,7 @@ protected static function discoverServices(string $name, array $arguments) protected static function buildServicesCache(): void { if (! static::$discovered) { - $config = config(Modules::class); - - if ($config->shouldDiscover('services')) { + if ((new Modules())->shouldDiscover('services')) { $locator = static::locator(); $files = $locator->search('Config/Services'); diff --git a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php index a3aff35b000e..8a2dba00703b 100644 --- a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php +++ b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php @@ -15,7 +15,6 @@ use CodeIgniter\Config\Factories; use CodeIgniter\Test\CIUnitTestCase; use Config\App; -use Config\Modules; /** * @internal @@ -58,7 +57,6 @@ public function testSave() $this->assertArrayHasKey('aliases', $cachedData); $this->assertArrayHasKey('instances', $cachedData); - $this->assertArrayHasKey(Modules::class, $cachedData['aliases']); $this->assertArrayHasKey('App', $cachedData['aliases']); } From 89f1cc68ed44a7208d478ab3779b7118abf9a0c0 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 22 Oct 2023 19:02:06 +0800 Subject: [PATCH 3/5] Inject Modules into constructor --- system/Config/BaseConfig.php | 4 +-- tests/system/Config/BaseConfigTest.php | 36 +++++++++++++------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index b4b9a4a21349..0269194bdc4c 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -80,9 +80,9 @@ public static function __set_state(array $array) * * The "shortPrefix" is the lowercase-only config class name. */ - public function __construct() + public function __construct(?Modules $modules = null) { - static::$moduleConfig = new Modules(); + static::$moduleConfig = $modules ?? new Modules(); if (! static::$override) { return; diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index 2da390392ed6..ca615602a276 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -11,8 +11,11 @@ namespace CodeIgniter\Config; +use CodeIgniter\Autoloader\FileLocator; use CodeIgniter\Test\CIUnitTestCase; +use Config\Modules; use Encryption; +use PHPUnit\Framework\MockObject\MockObject; use RegistrarConfig; use RuntimeException; use SimpleConfig; @@ -45,6 +48,8 @@ protected function setUp(): void if (! class_exists('Encryption', false)) { require $this->fixturesFolder . '/Encryption.php'; } + + BaseConfig::$registrars = []; } public function testBasicValues(): void @@ -265,32 +270,27 @@ public function testBadRegistrar(): void $this->assertSame('bar', $config->foo); } - public function testNotEnabled(): void + public function testDiscoveryNotEnabledWillNotPopulateRegistrarsArray(): void { - $modulesConfig = config('Modules'); - $modulesConfig->enabled = false; - - $config = new RegistrarConfig(); - $config::$registrars = []; - $expected = $config::$registrars; + /** @var MockObject&Modules $modules */ + $modules = $this->createMock(Modules::class); + $modules->method('shouldDiscover')->with('registrars')->willReturn(false); - $method = $this->getPrivateMethodInvoker($config, 'registerProperties'); - $method(); + $config = new RegistrarConfig($modules); - $this->assertSame($expected, $config::$registrars); + $this->assertSame([], $config::$registrars); } - public function testDidDiscovery(): void + public function testRedoingDiscoveryWillStillSetDidDiscoveryPropertyToTrue(): void { - $modulesConfig = config('Modules'); - $modulesConfig->enabled = true; + /** @var FileLocator&MockObject $locator */ + $locator = $this->createMock(FileLocator::class); + $locator->method('search')->with('Config/Registrar.php')->willReturn([]); + Services::injectMock('locator', $locator); - $config = new RegistrarConfig(); - $config::$registrars = []; + $config = new RegistrarConfig(); $this->setPrivateProperty($config, 'didDiscovery', false); - - $method = $this->getPrivateMethodInvoker($config, 'registerProperties'); - $method(); + ($this->getPrivateMethodInvoker($config, 'registerProperties'))(); $this->assertTrue($this->getPrivateProperty($config, 'didDiscovery')); } From 314210848d990d26420fab5d53bba88c626a4d24 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Tue, 24 Oct 2023 11:34:32 +0800 Subject: [PATCH 4/5] Use static setter --- system/Config/BaseConfig.php | 15 ++++++++++++--- tests/system/Config/BaseConfigTest.php | 4 +++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 0269194bdc4c..7daa6e1544c0 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -55,7 +55,7 @@ class BaseConfig /** * The modules configuration. * - * @var Modules + * @var Modules|null */ protected static $moduleConfig; @@ -74,15 +74,24 @@ public static function __set_state(array $array) return $obj; } + /** + * @internal For testing purposes only. + * @testTag + */ + public static function setModules(Modules $modules): void + { + static::$moduleConfig = $modules; + } + /** * Will attempt to get environment variables with names * that match the properties of the child class. * * The "shortPrefix" is the lowercase-only config class name. */ - public function __construct(?Modules $modules = null) + public function __construct() { - static::$moduleConfig = $modules ?? new Modules(); + static::$moduleConfig ??= new Modules(); if (! static::$override) { return; diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index ca615602a276..76861ccb12ad 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -50,6 +50,7 @@ protected function setUp(): void } BaseConfig::$registrars = []; + BaseConfig::setModules(new Modules()); // reset to clean copy of Modules } public function testBasicValues(): void @@ -276,7 +277,8 @@ public function testDiscoveryNotEnabledWillNotPopulateRegistrarsArray(): void $modules = $this->createMock(Modules::class); $modules->method('shouldDiscover')->with('registrars')->willReturn(false); - $config = new RegistrarConfig($modules); + RegistrarConfig::setModules($modules); + $config = new RegistrarConfig(); $this->assertSame([], $config::$registrars); } From 61bbbca81163dfc61af410972395de9cd905e637 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 25 Oct 2023 09:02:56 +0800 Subject: [PATCH 5/5] Simplify test --- tests/system/Config/BaseConfigTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index 76861ccb12ad..958387cc6b6d 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -290,9 +290,9 @@ public function testRedoingDiscoveryWillStillSetDidDiscoveryPropertyToTrue(): vo $locator->method('search')->with('Config/Registrar.php')->willReturn([]); Services::injectMock('locator', $locator); + $this->setPrivateProperty(RegistrarConfig::class, 'didDiscovery', false); + $config = new RegistrarConfig(); - $this->setPrivateProperty($config, 'didDiscovery', false); - ($this->getPrivateMethodInvoker($config, 'registerProperties'))(); $this->assertTrue($this->getPrivateProperty($config, 'didDiscovery')); }