-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update phpstan-codeigniter and fix errors on Modules #8036
Update phpstan-codeigniter and fix errors on Modules #8036
Conversation
01ac45a
to
f3a285c
Compare
I don't understand the failure on |
@paulbalandan It is a bug. I sent a PR #8037
|
(Copying my comment from the other PR) My only qualm with this new rule is that it hides dependencies from Deptrac. I think Config is fine as a globally-available dependency so I'm not too worried about it, but I did like the easily-built dependency graph. |
@paulbalandan By the way, why is |
This comment was marked as outdated.
This comment was marked as outdated.
f3a285c
to
07ebcad
Compare
My inspiration for this is when I remember you made a PR on shield changing from |
e2ed1c0
to
b9d05ec
Compare
Yes, this is good for libraries (modules). Because But if there is |
Ok. So |
bcfbe29
to
550a624
Compare
|
system/Config/BaseConfig.php
Outdated
@@ -82,7 +82,7 @@ public static function __set_state(array $array) | |||
*/ | |||
public function __construct() | |||
{ | |||
static::$moduleConfig = config(Modules::class); | |||
static::$moduleConfig = new Modules(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseConfig
does not share the Config\Modules
.
So there is no way to change the Config\Modules
values during testing.
CodeIgniter4/tests/system/Config/BaseConfigTest.php
Lines 268 to 281 in 4c09ca3
public function testNotEnabled(): void | |
{ | |
$modulesConfig = config('Modules'); | |
$modulesConfig->enabled = false; | |
$config = new RegistrarConfig(); | |
$config::$registrars = []; | |
$expected = $config::$registrars; | |
$method = $this->getPrivateMethodInvoker($config, 'registerProperties'); | |
$method(); | |
$this->assertSame($expected, $config::$registrars); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix will be something like this, but it is a breaking change.
diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php
index b4b9a4a213..2d0a0e9fe6 100644
--- a/system/Config/BaseConfig.php
+++ b/system/Config/BaseConfig.php
@@ -80,9 +80,9 @@ class BaseConfig
*
* The "shortPrefix" is the lowercase-only config class name.
*/
- public function __construct()
+ public function __construct(?Modules $config = null)
{
- static::$moduleConfig = new Modules();
+ static::$moduleConfig = $config ?? new Modules();
if (! static::$override) {
return;
diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php
index 2da390392e..4c66673189 100644
--- a/tests/system/Config/BaseConfigTest.php
+++ b/tests/system/Config/BaseConfigTest.php
@@ -12,6 +12,7 @@
namespace CodeIgniter\Config;
use CodeIgniter\Test\CIUnitTestCase;
+use Config\Modules;
use Encryption;
use RegistrarConfig;
use RuntimeException;
@@ -267,10 +268,10 @@ final class BaseConfigTest extends CIUnitTestCase
public function testNotEnabled(): void
{
- $modulesConfig = config('Modules');
+ $modulesConfig = new Modules();
$modulesConfig->enabled = false;
- $config = new RegistrarConfig();
+ $config = new RegistrarConfig($modulesConfig);
$config::$registrars = [];
$expected = $config::$registrars;
@@ -282,10 +283,10 @@ final class BaseConfigTest extends CIUnitTestCase
public function testDidDiscovery(): void
{
- $modulesConfig = config('Modules');
+ $modulesConfig = new Modules();
$modulesConfig->enabled = true;
- $config = new RegistrarConfig();
+ $config = new RegistrarConfig($modulesConfig);
$config::$registrars = [];
$this->setPrivateProperty($config, 'didDiscovery', false);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't modify that constructor. What about a static @testTag
setter? Then the constructor could be:
static::$moduleConfig ??= new Modules();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I'll change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be okay to change static::$moduleConfig
as nullable? phpstan complains on ??=
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has only @var
, so it is okay.
CodeIgniter4/system/Config/BaseConfig.php
Lines 56 to 60 in 6f81e6e
* The modules configuration. | |
* | |
* @var Modules | |
*/ | |
protected static $moduleConfig; |
👋 Hi, @paulbalandan! |
550a624
to
3773743
Compare
|
||
$method = $this->getPrivateMethodInvoker($config, 'registerProperties'); | ||
$method(); | ||
($this->getPrivateMethodInvoker($config, 'registerProperties'))(); | ||
|
||
$this->assertTrue($this->getPrivateProperty($config, 'didDiscovery')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following is easier to understand.
--- a/tests/system/Config/BaseConfigTest.php
+++ b/tests/system/Config/BaseConfigTest.php
@@ -290,9 +290,9 @@ final class BaseConfigTest extends CIUnitTestCase
$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'));
}
d328eac
to
61bbbca
Compare
Description
Explain what you have changed, and why.
Checklist: