Skip to content

Commit

Permalink
Add Type::lookupName() shortcut method (#6130)
Browse files Browse the repository at this point in the history
|      Q       |   A
|------------- | -----------
| Type         | feature
| Fixed issues | n/a

#### Summary

`Type` class has static methods for global `TypeRegistry` instance
shorter access like `Type::getType`.

This feature adds a static method for `TypeRegistry::lookupName`.

Thanks to #6082, the reverse lookup
is O(1) thus fine to be used massively - for ex. schema introspection
returns type as object, and this method is needed to obtain a string
name.

In DBAL 4.0, the method can be renamed to `Type::getName(Type $type):
string` to be more consistent with `Type::getType(string $name): Type`.
  • Loading branch information
mvorisek authored Aug 22, 2023
1 parent 420961e commit 0f3f92c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public static function getType($name)
return self::getTypeRegistry()->get($name);
}

/**
* Finds a name for the given type.
*
* @throws Exception
*/
public static function lookupName(self $type): string
{
return self::getTypeRegistry()->lookupName($type);
}

/**
* Adds a custom type to the type map.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Types/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public function testDefaultTypesAreRegistered(string $name): void
self::assertTrue(Type::hasType($name));
}

/** @dataProvider defaultTypesProvider() */
public function testDefaultTypesReverseLookup(string $name): void
{
$type = Type::getType($name);
self::assertSame($name, Type::lookupName($type));
}

/** @return iterable<string[]> */
public static function defaultTypesProvider(): iterable
{
Expand Down

0 comments on commit 0f3f92c

Please sign in to comment.