Skip to content

[TypeInfo] Better explain the getBaseType() method #20017

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
Jul 8, 2024
Merged
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
17 changes: 14 additions & 3 deletions components/type_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,20 @@ based on reflection or a simple string::
$type = Type::list(Type::nullable(Type::bool()));

// Type instances have several helper methods
$type->getBaseType() // returns an "array" Type instance
$type->getCollectionKeyType(); // returns an "int" Type instance
$type->getCollectionValueType()->isNullable(); // returns true

// returns the main type (e.g. in this example ir returns an "array" Type instance)
// for nullable types (e.g. string|null) returns the non-null type (e.g. string)
// and for compound types (e.g. int|string) it throws an exception because both types
// can be considered the main one, so there's no way to pick one
$baseType = $type->getBaseType();

// for collections, it returns the type of the item used as the key
// in this example, the collection is a list, so it returns and "int" Type instance
$keyType = $type->getCollectionKeyType();

// you can chain the utility methods e.g. to introspect the values of the collection
// the following code will return true
$isValueNullable = $type->getCollectionValueType()->isNullable();

Each of these calls will return you a ``Type`` instance that corresponds to the
static method used. You can also resolve types from a string (as shown in the
Expand Down
Loading