Skip to content
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

Merged
merged 5 commits into from
Oct 25, 2023

Conversation

paulbalandan
Copy link
Member

Description
Explain what you have changed, and why.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@paulbalandan paulbalandan force-pushed the update-phpstan-codeigniter branch 2 times, most recently from 01ac45a to f3a285c Compare October 12, 2023 03:34
@paulbalandan
Copy link
Member Author

I don't understand the failure on testLoad. using dd shows that it is http://test.example.jp/this-is-test/

@kenjis
Copy link
Member

kenjis commented Oct 12, 2023

@paulbalandan It is a bug. I sent a PR #8037

 3) CodeIgniter\Cache\FactoriesCacheFileVarExportHandlerTest::testLoad
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'http://test.example.jp/this-is-test/'
+'http://example.com/'

/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php:80

https://github.com/codeigniter4/CodeIgniter4/actions/runs/6490929878/job/17627453343?pr=8036

@kenjis kenjis mentioned this pull request Oct 12, 2023
5 tasks
@MGatner
Copy link
Member

MGatner commented Oct 12, 2023

(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.

system/CodeIgniter.php Outdated Show resolved Hide resolved
@kenjis
Copy link
Member

kenjis commented Oct 12, 2023

Is there any benefit in changing to short names in this main repository?

Another drawback is that completion in the IDE will not work.
If we use ::class, IDE can autocomplete.
Screenshot 2023-10-12 20 16 50

@kenjis
Copy link
Member

kenjis commented Oct 12, 2023

@paulbalandan By the way, why is ::class call discouraged?
The only case this becomes a problem is if devs want to change the class in a module to be loaded at will.

@github-actions github-actions bot added the stale Pull requests with conflicts label Oct 16, 2023
@github-actions

This comment was marked as outdated.

@paulbalandan
Copy link
Member Author

My inspiration for this is when I remember you made a PR on shield changing from config(AuthJWT::class) to config('AuthJWT') to allow overriding. So I thought why not made this a PHPStan rule.

@paulbalandan paulbalandan removed the stale Pull requests with conflicts label Oct 20, 2023
@paulbalandan paulbalandan force-pushed the update-phpstan-codeigniter branch 2 times, most recently from e2ed1c0 to b9d05ec Compare October 20, 2023 02:55
@kenjis
Copy link
Member

kenjis commented Oct 20, 2023

Yes, this is good for libraries (modules). Because config(\CodeIgniter\Shield\Config\Auth::class) do not load \Config\Auth (now by default).

But if there is \Config\Foo and devs use it, config(\Config\Foo::class) is no problem.

@paulbalandan
Copy link
Member Author

Ok. So Config\ should be the "champion". In cases where it is module-specific, that needs to be flagged. Hmm, I think that's doable.

@paulbalandan paulbalandan force-pushed the update-phpstan-codeigniter branch 3 times, most recently from bcfbe29 to 550a624 Compare October 21, 2023 08:04
@paulbalandan paulbalandan changed the title Update phpstan-codeigniter and fix new errors Update phpstan-codeigniter and fix errors on Modules Oct 21, 2023
@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 21, 2023
@kenjis
Copy link
Member

kenjis commented Oct 21, 2023

There were 2 errors:

1) CodeIgniter\Config\BaseConfigTest::testNotEnabled
RuntimeException: Registrars must return an array of properties and their values.

/home/runner/work/CodeIgniter4/CodeIgniter4/system/Config/BaseConfig.php:233
/home/runner/work/CodeIgniter4/CodeIgniter4/system/Config/BaseConfig.php:91
/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Config/BaseConfigTest.php:273

2) CodeIgniter\Config\BaseConfigTest::testDidDiscovery
RuntimeException: Registrars must return an array of properties and their values.

/home/runner/work/CodeIgniter4/CodeIgniter4/system/Config/BaseConfig.php:233
/home/runner/work/CodeIgniter4/CodeIgniter4/system/Config/BaseConfig.php:91
/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Config/BaseConfigTest.php:288

ERRORS!
Tests: 548, Assertions: 930, Errors: 2.

https://github.com/codeigniter4/CodeIgniter4/actions/runs/6596168617/job/17924550098?pr=8036

@@ -82,7 +82,7 @@ public static function __set_state(array $array)
*/
public function __construct()
{
static::$moduleConfig = config(Modules::class);
static::$moduleConfig = new Modules();
Copy link
Member

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.

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);
}

Copy link
Member

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);
 

Copy link
Member

@MGatner MGatner Oct 22, 2023

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();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'll change.

Copy link
Member Author

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 ??=

Copy link
Member

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.

* The modules configuration.
*
* @var Modules
*/
protected static $moduleConfig;

@github-actions
Copy link

👋 Hi, @paulbalandan!

We detected conflicts in your PR against the base branch 🙊
You may want to sync 🔄 your branch with upstream!

Ref: Syncing Your Branch

@github-actions github-actions bot added the stale Pull requests with conflicts label Oct 22, 2023
@paulbalandan paulbalandan removed the stale Pull requests with conflicts label Oct 22, 2023

$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();
($this->getPrivateMethodInvoker($config, 'registerProperties'))();

$this->assertTrue($this->getPrivateProperty($config, 'didDiscovery'));
Copy link
Member

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'));
     }

@kenjis kenjis merged commit 130be04 into codeigniter4:develop Oct 25, 2023
61 checks passed
@kenjis kenjis mentioned this pull request Oct 25, 2023
2 tasks
@paulbalandan paulbalandan deleted the update-phpstan-codeigniter branch October 26, 2023 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants