Skip to content

Commit 3780287

Browse files
committed
minor #20017 [TypeInfo] Better explain the getBaseType() method (javiereguiluz)
This PR was merged into the 7.1 branch. Discussion ---------- [TypeInfo] Better explain the `getBaseType()` method Fixes #19843. `@mtarld` if you have some time, please review this PR. Thanks a lot! Commits ------- 09e7fab [TypeInfo] Better explain the getBaseType() method
2 parents d0ed74f + 09e7fab commit 3780287

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

components/type_info.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,20 @@ based on reflection or a simple string::
6262
$type = Type::list(Type::nullable(Type::bool()));
6363

6464
// Type instances have several helper methods
65-
$type->getBaseType() // returns an "array" Type instance
66-
$type->getCollectionKeyType(); // returns an "int" Type instance
67-
$type->getCollectionValueType()->isNullable(); // returns true
65+
66+
// returns the main type (e.g. in this example ir returns an "array" Type instance)
67+
// for nullable types (e.g. string|null) returns the non-null type (e.g. string)
68+
// and for compound types (e.g. int|string) it throws an exception because both types
69+
// can be considered the main one, so there's no way to pick one
70+
$baseType = $type->getBaseType();
71+
72+
// for collections, it returns the type of the item used as the key
73+
// in this example, the collection is a list, so it returns and "int" Type instance
74+
$keyType = $type->getCollectionKeyType();
75+
76+
// you can chain the utility methods e.g. to introspect the values of the collection
77+
// the following code will return true
78+
$isValueNullable = $type->getCollectionValueType()->isNullable();
6879

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

0 commit comments

Comments
 (0)