From e3a139db12fb48910c3a876b383c57615a04a2fa Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Fri, 10 Jan 2025 16:12:42 +0300 Subject: [PATCH] refactor: Fix phpstan method.impossibleType (#9384) * refactor: Fix phpstan method.impossibleType * refactor: Fix phpstan method.notFound for EventsTest * fix: Run fixer --- tests/system/Events/EventsTest.php | 67 ++++++++++++------- utils/phpstan-baseline/loader.neon | 1 - .../method.impossibleType.neon | 13 ---- utils/phpstan-baseline/method.notFound.neon | 7 +- 4 files changed, 43 insertions(+), 45 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..f586a91de307 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,13 @@ 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 712e5cb76c77..e0c0cb703cd7 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -19,7 +19,6 @@ includes: - method.alreadyNarrowedType.neon - method.childParameterType.neon - method.childReturnType.neon - - method.impossibleType.neon - method.notFound.neon - missingType.callable.neon - missingType.iterableValue.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 diff --git a/utils/phpstan-baseline/method.notFound.neon b/utils/phpstan-baseline/method.notFound.neon index 648e5637378a..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: @@ -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