Skip to content

Commit

Permalink
refactor: Fix phpstan method.impossibleType (#9384)
Browse files Browse the repository at this point in the history
* refactor: Fix phpstan method.impossibleType

* refactor: Fix phpstan method.notFound for EventsTest

* fix: Run fixer
  • Loading branch information
neznaika0 authored Jan 10, 2025
1 parent 131c5eb commit e3a139d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 45 deletions.
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 @@ -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
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

0 comments on commit e3a139d

Please sign in to comment.