diff --git a/psalm-baseline.xml b/psalm-baseline.xml index f476f489b..b0badb61a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1175,9 +1175,6 @@ - - getParameters()[1]]]> - @@ -1203,9 +1200,6 @@ - - getParameters()[1]?->getType()]]> - ]]> diff --git a/src/Parser/Typecast.php b/src/Parser/Typecast.php index 6ede87914..2a24bd1d0 100644 --- a/src/Parser/Typecast.php +++ b/src/Parser/Typecast.php @@ -135,7 +135,8 @@ public function setRules(array $rules): array // The callable accepts DatabaseInterface as second argument $reflection = new \ReflectionFunction($closure); - if ($reflection->getParameters()[1]?->getType()->getName() === DatabaseInterface::class) { + $type = ($reflection->getParameters()[1] ?? null)?->getType(); + if ($type instanceof \ReflectionNamedType && $type->getName() === DatabaseInterface::class) { $this->casters[$key] = fn(mixed $value): mixed => $closure($value, $this->database, ...$args); continue; } diff --git a/tests/ORM/Functional/Driver/Common/Typecast/Fixture/CallableWithArguments.php b/tests/ORM/Functional/Driver/Common/Typecast/Fixture/CallableWithArguments.php new file mode 100644 index 000000000..73ee2a612 --- /dev/null +++ b/tests/ORM/Functional/Driver/Common/Typecast/Fixture/CallableWithArguments.php @@ -0,0 +1,28 @@ + ['typed', [3], 'hello@world.com:3']; + yield 'untyped' => ['untyped', [3], 'hello@world.com:3']; + yield 'union' => ['union', [3], 'hello@world.com:3']; + yield 'single parameter' => ['single', [], 'hello@world.com']; + } + // Insert command public function testAIIdTypecastingOnInsert(): void @@ -147,6 +156,20 @@ public function testCompositeTypecastWith(): void $this->assertNull($book2->title); } + /** + * @dataProvider callableArgumentTypes + */ + public function testLoadWithCallableArguments(string $method, array $args, string $expected): void + { + $schema = $this->orm->getSchema()->toArray(); + $schema['user'][SchemaInterface::TYPECAST]['email'] = [CallableWithArguments::class, $method, $args]; + $this->orm = $this->withSchema(new Schema($schema)); + + $user = $this->orm->getRepository(User::class)->findByPK(1); + + $this->assertSame($expected, $user->email); + } + // Select public function testSelectOne(): void