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-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/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\ diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 87166737e746..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,6 +74,15 @@ 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. @@ -82,7 +91,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']); } diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index 2da390392ed6..958387cc6b6d 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,9 @@ protected function setUp(): void if (! class_exists('Encryption', false)) { require $this->fixturesFolder . '/Encryption.php'; } + + BaseConfig::$registrars = []; + BaseConfig::setModules(new Modules()); // reset to clean copy of Modules } public function testBasicValues(): void @@ -265,32 +271,28 @@ 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(); + RegistrarConfig::setModules($modules); + $config = new RegistrarConfig(); - $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 = []; - $this->setPrivateProperty($config, 'didDiscovery', false); + $this->setPrivateProperty(RegistrarConfig::class, 'didDiscovery', false); - $method = $this->getPrivateMethodInvoker($config, 'registerProperties'); - $method(); + $config = new RegistrarConfig(); $this->assertTrue($this->getPrivateProperty($config, 'didDiscovery')); }