Skip to content

Commit e3a139d

Browse files
authored
refactor: Fix phpstan method.impossibleType (#9384)
* refactor: Fix phpstan method.impossibleType * refactor: Fix phpstan method.notFound for EventsTest * fix: Run fixer
1 parent 131c5eb commit e3a139d

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

tests/system/Events/EventsTest.php

+42-25
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPUnit\Framework\Attributes\PreserveGlobalState;
2121
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
2222
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
23+
use stdClass;
2324

2425
/**
2526
* @internal
@@ -29,6 +30,8 @@ final class EventsTest extends CIUnitTestCase
2930
{
3031
/**
3132
* Accessible event manager instance
33+
*
34+
* @var MockEvents
3235
*/
3336
private Events $manager;
3437

@@ -181,63 +184,68 @@ public function testPriorityWithMultiple(): void
181184

182185
public function testRemoveListener(): void
183186
{
184-
$result = false;
187+
$user = $this->getEditableObject();
185188

186-
$callback = static function () use (&$result): void {
187-
$result = true;
189+
$callback = static function () use (&$user): void {
190+
$user->name = 'Boris';
191+
$user->age = 40;
188192
};
189193

190194
Events::on('foo', $callback);
191195

192-
Events::trigger('foo');
193-
$this->assertTrue($result);
196+
$this->assertTrue(Events::trigger('foo'));
197+
$this->assertSame('Boris', $user->name);
198+
199+
$user = $this->getEditableObject();
194200

195-
$result = false;
196201
$this->assertTrue(Events::removeListener('foo', $callback));
197202

198-
Events::trigger('foo');
199-
$this->assertFalse($result);
203+
$this->assertTrue(Events::trigger('foo'));
204+
$this->assertSame('Ivan', $user->name);
200205
}
201206

202207
public function testRemoveListenerTwice(): void
203208
{
204-
$result = false;
209+
$user = $this->getEditableObject();
205210

206-
$callback = static function () use (&$result): void {
207-
$result = true;
211+
$callback = static function () use (&$user): void {
212+
$user->name = 'Boris';
213+
$user->age = 40;
208214
};
209215

210216
Events::on('foo', $callback);
211217

212-
Events::trigger('foo');
213-
$this->assertTrue($result);
218+
$this->assertTrue(Events::trigger('foo'));
219+
$this->assertSame('Boris', $user->name);
220+
221+
$user = $this->getEditableObject();
214222

215-
$result = false;
216223
$this->assertTrue(Events::removeListener('foo', $callback));
217224
$this->assertFalse(Events::removeListener('foo', $callback));
218225

219-
Events::trigger('foo');
220-
$this->assertFalse($result);
226+
$this->assertTrue(Events::trigger('foo'));
227+
$this->assertSame('Ivan', $user->name);
221228
}
222229

223230
public function testRemoveUnknownListener(): void
224231
{
225-
$result = false;
232+
$user = $this->getEditableObject();
226233

227-
$callback = static function () use (&$result): void {
228-
$result = true;
234+
$callback = static function () use (&$user): void {
235+
$user->name = 'Boris';
236+
$user->age = 40;
229237
};
230238

231239
Events::on('foo', $callback);
232240

233-
Events::trigger('foo');
234-
$this->assertTrue($result);
241+
$this->assertTrue(Events::trigger('foo'));
242+
$this->assertSame('Boris', $user->name);
235243

236-
$result = false;
237-
$this->assertFalse(Events::removeListener('bar', $callback));
244+
$user = $this->getEditableObject();
238245

239-
Events::trigger('foo');
240-
$this->assertTrue($result);
246+
$this->assertFalse(Events::removeListener('bar', $callback));
247+
$this->assertTrue(Events::trigger('foo'));
248+
$this->assertSame('Boris', $user->name);
241249
}
242250

243251
public function testRemoveAllListenersWithSingleEvent(): void
@@ -315,4 +323,13 @@ public function testSimulate(): void
315323

316324
$this->assertSame(0, $result);
317325
}
326+
327+
private function getEditableObject(): stdClass
328+
{
329+
$user = new stdClass();
330+
$user->name = 'Ivan';
331+
$user->age = 30;
332+
333+
return clone $user;
334+
}
318335
}

utils/phpstan-baseline/loader.neon

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ includes:
1919
- method.alreadyNarrowedType.neon
2020
- method.childParameterType.neon
2121
- method.childReturnType.neon
22-
- method.impossibleType.neon
2322
- method.notFound.neon
2423
- missingType.callable.neon
2524
- missingType.iterableValue.neon

utils/phpstan-baseline/method.impossibleType.neon

-13
This file was deleted.

utils/phpstan-baseline/method.notFound.neon

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 47 errors
1+
# total 46 errors
22

33
parameters:
44
ignoreErrors:
@@ -77,11 +77,6 @@ parameters:
7777
count: 3
7878
path: ../../tests/system/Debug/ExceptionHandlerTest.php
7979

80-
-
81-
message: '#^Call to an undefined method CodeIgniter\\Events\\Events\:\:unInitialize\(\)\.$#'
82-
count: 1
83-
path: ../../tests/system/Events/EventsTest.php
84-
8580
-
8681
message: '#^Call to an undefined method CodeIgniter\\HTTP\\Request\:\:getCookie\(\)\.$#'
8782
count: 2

0 commit comments

Comments
 (0)