From 39fc73a1ec92ec6026813be81aaba6154d258428 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Wed, 8 Jan 2025 17:15:14 +0300 Subject: [PATCH 1/3] refactor: Fix phpstan method.impossibleType --- tests/system/Events/EventsTest.php | 66 ++++++++++++------- utils/phpstan-baseline/loader.neon | 1 - .../method.impossibleType.neon | 13 ---- 3 files changed, 41 insertions(+), 39 deletions(-) delete mode 100644 utils/phpstan-baseline/method.impossibleType.neon diff --git a/tests/system/Events/EventsTest.php b/tests/system/Events/EventsTest.php index db975aaa1825..ba26e63a448a 100644 --- a/tests/system/Events/EventsTest.php +++ b/tests/system/Events/EventsTest.php @@ -20,6 +20,7 @@ use PHPUnit\Framework\Attributes\PreserveGlobalState; use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\Attributes\WithoutErrorHandler; +use stdClass; /** * @internal @@ -29,6 +30,8 @@ final class EventsTest extends CIUnitTestCase { /** * Accessible event manager instance + * + * @var MockEvents */ private Events $manager; @@ -181,63 +184,68 @@ public function testPriorityWithMultiple(): void public function testRemoveListener(): void { - $result = false; + $user = $this->getEditableObject(); - $callback = static function () use (&$result): void { - $result = true; + $callback = static function () use (&$user): void { + $user->name = 'Boris'; + $user->age = 40; }; Events::on('foo', $callback); - Events::trigger('foo'); - $this->assertTrue($result); + $this->assertTrue(Events::trigger('foo')); + $this->assertSame('Boris', $user->name); + + $user = $this->getEditableObject(); - $result = false; $this->assertTrue(Events::removeListener('foo', $callback)); - Events::trigger('foo'); - $this->assertFalse($result); + $this->assertTrue(Events::trigger('foo')); + $this->assertSame('Ivan', $user->name); } public function testRemoveListenerTwice(): void { - $result = false; + $user = $this->getEditableObject(); - $callback = static function () use (&$result): void { - $result = true; + $callback = static function () use (&$user): void { + $user->name = 'Boris'; + $user->age = 40; }; Events::on('foo', $callback); - Events::trigger('foo'); - $this->assertTrue($result); + $this->assertTrue(Events::trigger('foo')); + $this->assertSame('Boris', $user->name); + + $user = $this->getEditableObject(); - $result = false; $this->assertTrue(Events::removeListener('foo', $callback)); $this->assertFalse(Events::removeListener('foo', $callback)); - Events::trigger('foo'); - $this->assertFalse($result); + $this->assertTrue(Events::trigger('foo')); + $this->assertSame('Ivan', $user->name); } public function testRemoveUnknownListener(): void { - $result = false; + $user = $this->getEditableObject(); - $callback = static function () use (&$result): void { - $result = true; + $callback = static function () use (&$user): void { + $user->name = 'Boris'; + $user->age = 40; }; Events::on('foo', $callback); - Events::trigger('foo'); - $this->assertTrue($result); + $this->assertTrue(Events::trigger('foo')); + $this->assertSame('Boris', $user->name); - $result = false; - $this->assertFalse(Events::removeListener('bar', $callback)); + $user = $this->getEditableObject(); - Events::trigger('foo'); - $this->assertTrue($result); + $this->assertFalse(Events::removeListener('bar', $callback)); + $this->assertTrue(Events::trigger('foo')); + $this->assertSame('Boris', $user->name); } public function testRemoveAllListenersWithSingleEvent(): void @@ -315,4 +323,12 @@ public function testSimulate(): void $this->assertSame(0, $result); } + + private function getEditableObject(): stdClass { + $user = new stdClass(); + $user->name = 'Ivan'; + $user->age = 30; + + return clone $user; + } } diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 7301539443d0..592b8ed141db 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -20,7 +20,6 @@ includes: - method.alreadyNarrowedType.neon - method.childParameterType.neon - method.childReturnType.neon - - method.impossibleType.neon - method.notFound.neon - method.unused.neon - missingType.callable.neon diff --git a/utils/phpstan-baseline/method.impossibleType.neon b/utils/phpstan-baseline/method.impossibleType.neon deleted file mode 100644 index 359920fe3658..000000000000 --- a/utils/phpstan-baseline/method.impossibleType.neon +++ /dev/null @@ -1,13 +0,0 @@ -# total 2 errors - -parameters: - ignoreErrors: - - - message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertFalse\(\) with false will always evaluate to false\.$#' - count: 2 - path: ../../tests/system/Events/EventsTest.php - - - - message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with false will always evaluate to false\.$#' - count: 1 - path: ../../tests/system/Events/EventsTest.php From 7f0151818757cd2120029309d0afa8be4785ef09 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Wed, 8 Jan 2025 17:15:58 +0300 Subject: [PATCH 2/3] refactor: Fix phpstan method.notFound for EventsTest --- utils/phpstan-baseline/method.notFound.neon | 5 ----- 1 file changed, 5 deletions(-) diff --git a/utils/phpstan-baseline/method.notFound.neon b/utils/phpstan-baseline/method.notFound.neon index 648e5637378a..83866b2c0f0a 100644 --- a/utils/phpstan-baseline/method.notFound.neon +++ b/utils/phpstan-baseline/method.notFound.neon @@ -77,11 +77,6 @@ parameters: count: 3 path: ../../tests/system/Debug/ExceptionHandlerTest.php - - - message: '#^Call to an undefined method CodeIgniter\\Events\\Events\:\:unInitialize\(\)\.$#' - count: 1 - path: ../../tests/system/Events/EventsTest.php - - message: '#^Call to an undefined method CodeIgniter\\HTTP\\Request\:\:getCookie\(\)\.$#' count: 2 From b44901c90998ebbee49cad6c26a645ade8c70aa5 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Wed, 8 Jan 2025 20:15:31 +0300 Subject: [PATCH 3/3] fix: Run fixer --- tests/system/Events/EventsTest.php | 3 ++- utils/phpstan-baseline/method.notFound.neon | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/system/Events/EventsTest.php b/tests/system/Events/EventsTest.php index ba26e63a448a..f586a91de307 100644 --- a/tests/system/Events/EventsTest.php +++ b/tests/system/Events/EventsTest.php @@ -324,7 +324,8 @@ public function testSimulate(): void $this->assertSame(0, $result); } - private function getEditableObject(): stdClass { + private function getEditableObject(): stdClass + { $user = new stdClass(); $user->name = 'Ivan'; $user->age = 30; diff --git a/utils/phpstan-baseline/method.notFound.neon b/utils/phpstan-baseline/method.notFound.neon index 83866b2c0f0a..3917c59695e8 100644 --- a/utils/phpstan-baseline/method.notFound.neon +++ b/utils/phpstan-baseline/method.notFound.neon @@ -1,4 +1,4 @@ -# total 47 errors +# total 46 errors parameters: ignoreErrors: