Skip to content
Open
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
6 changes: 0 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1175,9 +1175,6 @@
</UnsupportedPropertyReferenceUsage>
</file>
<file src="src/Parser/Typecast.php">
<DocblockTypeContradiction>
<code><![CDATA[$reflection->getParameters()[1]]]></code>
</DocblockTypeContradiction>
<MixedArgument>
<code><![CDATA[$key]]></code>
<code><![CDATA[$value]]></code>
Expand All @@ -1203,9 +1200,6 @@
<code><![CDATA[$key]]></code>
<code><![CDATA[$key]]></code>
</PossiblyUndefinedVariable>
<RedundantConditionGivenDocblockType>
<code><![CDATA[$reflection->getParameters()[1]?->getType()]]></code>
</RedundantConditionGivenDocblockType>
<UnnecessaryVarAnnotation>
<code><![CDATA[class-string<\BackedEnum>]]></code>
</UnnecessaryVarAnnotation>
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Typecast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture;

final class CallableWithArguments
{
public static function typed(string $value, int $suffix): string
{
return $value . ':' . $suffix;
}

public static function untyped(string $value, $suffix): string
{
return $value . ':' . $suffix;
}

public static function union(string $value, int|string $suffix): string
{
return $value . ':' . $suffix;
}

public static function single(string $value): string
{
return $value;
}
}
23 changes: 23 additions & 0 deletions tests/ORM/Functional/Driver/Common/Typecast/TypecastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture\Book2;
use Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture\BookNestedStates;
use Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture\BookStates;
use Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture\CallableWithArguments;
use Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture\IDCaster;
use Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture\JsonTypecast;
use Cycle\ORM\Tests\Functional\Driver\Common\Typecast\Fixture\ParentTypecast;
Expand All @@ -28,6 +29,14 @@ abstract class TypecastTest extends BaseTest
{
use TableTrait;

public static function callableArgumentTypes(): iterable
{
yield 'typed' => ['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
Expand Down Expand Up @@ -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
Expand Down
Loading