Skip to content

refactor: Fix phpstan method.impossibleType #9384

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions tests/system/Events/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use stdClass;

/**
* @internal
Expand All @@ -29,6 +30,8 @@ final class EventsTest extends CIUnitTestCase
{
/**
* Accessible event manager instance
*
* @var MockEvents
*/
private Events $manager;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions utils/phpstan-baseline/method.impossibleType.neon

This file was deleted.

7 changes: 1 addition & 6 deletions utils/phpstan-baseline/method.notFound.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 47 errors
# total 46 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -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
Expand Down
Loading