Skip to content

[TypeInfo] Proxies methods to non-nullable and fail gracefully #54679

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
merged 1 commit into from
May 31, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@ public function testIsA()
$this->assertTrue($type->isA(TypeIdentifier::OBJECT));
$this->assertTrue($type->isA(self::class));
}

public function testProxiesMethodsToBaseType()
{
$type = new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ARRAY), Type::string(), Type::bool()));
$this->assertEquals([Type::string(), Type::bool()], $type->getVariableTypes());
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Type/GenericTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ public function testIsA()
$this->assertFalse($type->isA(TypeIdentifier::STRING));
$this->assertTrue($type->isA(self::class));
}

public function testProxiesMethodsToBaseType()
{
$type = new GenericType(Type::object(self::class), Type::float());
$this->assertSame(self::class, $type->getClassName());
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Type/UnionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,23 @@ public function testIsA()
$type = new UnionType(Type::string(), Type::intersection(Type::int(), Type::int()));
$this->assertTrue($type->isA(TypeIdentifier::INT));
}

public function testProxiesMethodsToNonNullableType()
{
$this->assertEquals(Type::string(), (new UnionType(Type::list(Type::string()), Type::null()))->getCollectionValueType());

try {
(new UnionType(Type::int(), Type::null()))->getCollectionValueType();
$this->fail();
} catch (LogicException) {
$this->addToAssertionCount(1);
}

try {
(new UnionType(Type::list(Type::string()), Type::string()))->getCollectionValueType();
$this->fail();
} catch (LogicException) {
$this->addToAssertionCount(1);
}
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public function testCannotGetBaseTypeOnCompoundType()
$this->expectException(LogicException::class);
Type::union(Type::int(), Type::string())->getBaseType();
}

public function testThrowsOnUnexistingMethod()
{
$this->expectException(LogicException::class);
Type::int()->unexistingMethod();
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/TypeInfo/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\TypeInfo;

use Symfony\Component\TypeInfo\Exception\LogicException;
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\ObjectType;

Expand Down Expand Up @@ -45,4 +46,14 @@ public function isNullable(): bool
{
return $this->is(fn (Type $t): bool => $t->isA(TypeIdentifier::NULL) || $t->isA(TypeIdentifier::MIXED));
}

/**
* Graceful fallback for unexisting methods.
*
* @param list<mixed> $arguments
*/
public function __call(string $method, array $arguments): mixed
{
throw new LogicException(sprintf('Cannot call "%s" on "%s" type.', $method, $this));
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,24 @@ public function __toString(): string

return $string;
}

/**
* Proxies all method calls to the original non-nullable type.
*
* @param list<mixed> $arguments
*/
public function __call(string $method, array $arguments): mixed
{
$nonNullableType = $this->asNonNullable();

if (!$nonNullableType instanceof self) {
if (!method_exists($nonNullableType, $method)) {
throw new LogicException(sprintf('Method "%s" doesn\'t exist on "%s" type.', $method, $nonNullableType));
}

return $nonNullableType->{$method}(...$arguments);
}

throw new LogicException(sprintf('Cannot call "%s" on "%s" compound type.', $method, $this));
}
}
Loading