Skip to content

Commit

Permalink
Allow adding type instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk committed Feb 11, 2025
1 parent ca6bc55 commit e466fd8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,19 @@ public static function lookupName(self $type): string
/**
* Adds a custom type to the type map.
*
* @param string $name The name of the type.
* @param class-string<Type> $className The class name of the custom type.
* @param string $name The name of the type.
* @param class-string<Type>|Type $className The custom type or the class name of the custom type.
*
* @throws Exception
*/
public static function addType(string $name, string $className): void
public static function addType(string $name, string|Type $className): void
{
if ($className instanceof Type) {
self::getTypeRegistry()->register($name, $className);

return;
}

try {
self::getTypeRegistry()->register($name, new $className());
} catch (ArgumentCountError $e) {
Expand Down
7 changes: 7 additions & 0 deletions tests/Types/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ public function testAddTypeWhenTypeRequiresArguments(): void

Type::addType('some_type', TypeWithConstructor::class);
}

public function testAddTypeInstance(): void
{
self::assertFalse(Type::hasType('some_type'));
Type::addType('some_type', new TypeWithConstructor(true));
self::assertTrue(Type::hasType('some_type'));
}
}

0 comments on commit e466fd8

Please sign in to comment.