| Methods |
public
|
__construct(
list<ConstantIntegerType|ConstantStringType> $keyTypes,
array<int, Type> $valueTypes,
list<int> $nextAutoIndexes = [0],
int[] $optionalKeys = [],
?TrinaryLogic $isList = null,
array{Type, Type}|null $unsealed = null,
bool $keyOrderKnown = true,
)
Parameters
| $keyOrderKnown |
false for a shape that only lists the keys that are present, like one written in PHPDoc
|
Overridden by
|
#
|
public
|
isKeyOrderKnown(): bool
Whether the keys come in the order they are listed. A literal array knows its key order;
an array shape written in PHPDoc only lists the keys that are present. A list and a shape
with fewer than two keys know the order either way.
Whether the keys come in the order they are listed. A literal array knows its key order;
an array shape written in PHPDoc only lists the keys that are present. A list and a shape
with fewer than two keys know the order either way.
|
#
|
public
|
forgetKeyOrder(): self
The same shape without the order of its keys, as a PHPDoc shape that lists them.
A template type's bound keeps it - pass its bound instead.
The same shape without the order of its keys, as a PHPDoc shape that lists them.
A template type's bound keeps it - pass its bound instead.
|
#
|
public
|
sharesKeyOrderWith(self $other): bool
Whether both shapes know their key order and list the keys they share in the same order.
Two such shapes that list them in different orders never hold the same array.
Whether both shapes know their key order and list the keys they share in the same order.
Two such shapes that list them in different orders never hold the same array.
|
#
|
public
|
isSealed(): TrinaryLogic
|
#
|
public
|
isUnsealed(): TrinaryLogic
|
#
|
public
|
getUnsealedTypes(): array{Type, Type}|null
The unsealed key and value types, with the template an unsealed key declares
standing for its bound: ...<string, list<string>> for ...<K of string, list<K>>.
The unsealed key and value types, with the template an unsealed key declares
standing for its bound: ...<string, list<string>> for ...<K of string, list<K>>.
|
#
|
public
|
getUnsealedDeclaredTypes(): array{Type, Type}|null
The unsealed key and value types as they are declared, as ...<K of string, list<K>>
for an unsealed key that declares a template the value is written with, for what keeps
each extra key with its value.
The unsealed key and value types as they are declared, as ...<K of string, list<K>>
for an unsealed key that declares a template the value is written with, for what keeps
each extra key with its value.
|
#
|
protected
|
recreate(
list<ConstantIntegerType|ConstantStringType> $keyTypes,
array<int, Type> $valueTypes,
list<int> $nextAutoIndexes,
int[] $optionalKeys,
?TrinaryLogic $isList,
array{Type, Type}|null $unsealed,
): self
|
#
|
protected
|
keepUnsealedDeclaration(array{Type, Type}|null $unsealed): array{Type, Type}|null
The declared unsealed types in place of the bound form of them, when they are what is passed on unchanged.
The declared unsealed types in place of the bound form of them, when they are what is passed on unchanged.
|
#
|
public
|
getConstantArrays(): array
Only ConstantArrayType instances (array shapes with known keys).
Only ConstantArrayType instances (array shapes with known keys).
Implements
|
#
|
public
|
getReferencedClasses(): array
Returns all class names referenced anywhere in this type, recursively
(including generic arguments, callable signatures, etc.).
Returns all class names referenced anywhere in this type, recursively
(including generic arguments, callable signatures, etc.).
Implements
|
#
|
public
|
getIterableKeyType(): Type
Works for both arrays and Traversable objects.
Works for both arrays and Traversable objects.
Implements
|
#
|
public
|
getIterableValueType(): Type
|
#
|
public
|
getKeyType(): Type
|
#
|
public
|
getItemType(): Type
|
#
|
public
|
isConstantValue(): TrinaryLogic
Is Type of a known constant value? Includes literal strings, integers, floats, true, false, null, and array shapes.
Is Type of a known constant value? Includes literal strings, integers, floats, true, false, null, and array shapes.
Unlike isConstantScalarValue(), this also returns yes for constant array types (array shapes
with known keys and values). Use this when you need to detect any constant value including arrays.
Implements
|
#
|
public
|
getNextAutoIndexes(): list<int>
|
#
|
public
|
getOptionalKeys(): int[]
|
#
|
public
|
getAllArrays(): self[]
|
#
|
public
|
getKeyTypes(): list<ConstantIntegerType|ConstantStringType>
|
#
|
public
|
getValueTypes(): array<int, Type>
|
#
|
public
|
isOptionalKey(int $i): bool
|
#
|
public
|
sortKeys(): self
|
#
|
public
|
inKeyOrderOf(self ...$shapes): self
This shape with its keys in the order of the first of the shapes that knows its key order
and lists every key of this one, or without a known key order when none of them does.
Fits the intersection of the shapes, whose keys come from more than one of them.
This shape with its keys in the order of the first of the shapes that knows its key order
and lists every key of this one, or without a known key order when none of them does.
Fits the intersection of the shapes, whose keys come from more than one of them.
|
#
|
public
|
accepts(Type $type, bool $strictTypes): AcceptsResult
Unlike isSuperTypeOf(), accepts() takes into account PHP's implicit type coercion.
With $strictTypes = false, int is accepted by float, and Stringable objects are
accepted by string.
Unlike isSuperTypeOf(), accepts() takes into account PHP's implicit type coercion.
With $strictTypes = false, int is accepted by float, and Stringable objects are
accepted by string.
Implements
|
#
|
public
|
isSuperTypeOf(Type $type): IsSuperTypeOfResult
"Does every value of $type belong to $this type?"
"Does every value of $type belong to $this type?"
Preferable to instanceof checks because it correctly handles
union types, intersection types, and all other composite types.
Implements
|
#
|
public
|
looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
|
#
|
public
|
equals(Type $type): bool
|
#
|
public
|
isCallable(): TrinaryLogic
|
#
|
public
|
getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
|
#
|
public
|
findTypeAndMethodNames(): ConstantArrayTypeAndMethod[]
|
#
|
public
|
hasOffsetValueType(Type $offsetType): TrinaryLogic
|
#
|
public
|
getOffsetValueType(Type $offsetType): Type
|
#
|
public
|
setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
May add a new key. When $offsetType is null, appends (like $a[] = $value).
May add a new key. When $offsetType is null, appends (like $a[] = $value).
Parameters
| $unionValues |
Ignored, the written value is always unioned with the existing values. Will be removed in 3.0.
|
Implements
|
#
|
public
|
setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
Unlike setOffsetValueType(), assumes the key already exists.
Preserves the array shape and list type.
Unlike setOffsetValueType(), assumes the key already exists.
Preserves the array shape and list type.
Implements
|
#
|
public
|
unsetOffset(Type $offsetType, bool $preserveListCertainty = false): Type
Removes or marks as optional the key(s) matching the given offset type from this constant array.
Removes or marks as optional the key(s) matching the given offset type from this constant array.
By default, the method assumes an actual unset() call was made, which actively modifies the
array and weakens its list certainty to "maybe". However, in some contexts, such as the else
branch of an array_key_exists() check, the key is statically known to be absent without any
modification, so list certainty should be preserved as-is.
Implements
|
#
|
public
|
chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
Models array_chunk($array, $length, $preserveKeys).
Models array_chunk($array, $length, $preserveKeys).
Implements
|
#
|
public
|
fillKeysArray(Type $valueType): Type
Models array_fill_keys($keys, $value).
Models array_fill_keys($keys, $value).
Implements
|
#
|
public
|
flipArray(): Type
Models array_flip($array).
Models array_flip($array).
Implements
|
#
|
public
|
intersectKeyArray(Type $otherArraysType): Type
Models array_intersect_key($array, ...$otherArrays).
Models array_intersect_key($array, ...$otherArrays).
Implements
|
#
|
public
|
popArray(): Type
Models array_pop() effect on the array.
Models array_pop() effect on the array.
Implements
|
#
|
public
|
reverseArray(TrinaryLogic $preserveKeys): Type
Models array_reverse($array, $preserveKeys).
Models array_reverse($array, $preserveKeys).
Implements
|
#
|
public
|
searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
Models array_search($needle, $array, $strict).
Models array_search($needle, $array, $strict).
Implements
|
#
|
public
|
shiftArray(): Type
Models array_shift() effect on the array.
Models array_shift() effect on the array.
Implements
|
#
|
public
|
shuffleArray(): Type
Models shuffle() effect on the array. Result is always a list.
Models shuffle() effect on the array. Result is always a list.
It's also used to model array after sort / rsort / usort calls.
Implements
|
#
|
public
|
sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
Models array_slice($array, $offset, $length, $preserveKeys).
Models array_slice($array, $offset, $length, $preserveKeys).
Implements
|
#
|
public
|
spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
Models array_splice() effect on the array (the modified array, not the removed portion).
Models array_splice() effect on the array (the modified array, not the removed portion).
Implements
|
#
|
public
|
truncateListToSize(Type $sizeType): Type
Narrows a list-shaped array type to "size lies in $sizeType" — the
type-system equivalent of count($list) === N / count($list) >= N
/ count($list) in [N, M]. Used by TypeSpecifier when specifying
types for count() comparisons; the call site is responsible for
gating on outer list-ness, so each implementation may assume it's
narrowing a list shape.
Narrows a list-shaped array type to "size lies in $sizeType" — the
type-system equivalent of count($list) === N / count($list) >= N
/ count($list) in [N, M]. Used by TypeSpecifier when specifying
types for count() comparisons; the call site is responsible for
gating on outer list-ness, so each implementation may assume it's
narrowing a list shape.
$sizeType is expected to be a ConstantIntegerType (exact size) or
an IntegerRangeType (a min/max bound). Concrete implementations
(ConstantArrayType, ArrayType) rebuild the array with a required
prefix [0, min) and an optional middle [min, max) (when max is
set), or extend with HasOffsetValueType accessories when the upper
bound is unbounded. Non-array types return ErrorType.
Implements
|
#
|
public
static
|
extractTruncateListBounds(Type $sizeType): array{?int, ?int}
Extracts (min, max) bounds from a size type for truncateListToSize:
the smallest and the largest size. A size type without integers returns
[null, null] and the caller falls back to the non-precise path.
Extracts (min, max) bounds from a size type for truncateListToSize:
the smallest and the largest size. A size type without integers returns
[null, null] and the caller falls back to the non-precise path.
|
#
|
public
|
isIterableAtLeastOnce(): TrinaryLogic
|
#
|
public
|
getArraySize(): Type
Returns the count of elements as a Type (typically IntegerRangeType).
Returns the count of elements as a Type (typically IntegerRangeType).
Implements
|
#
|
public
|
getFirstIterableKeyType(): Type
The type of the first key; the union of the keys when their order is not known.
The type of the first key; the union of the keys when their order is not known.
Implements
|
#
|
public
|
getLastIterableKeyType(): Type
The type of the last key; the union of the keys when their order is not known.
The type of the last key; the union of the keys when their order is not known.
Implements
|
#
|
public
|
getFirstIterableValueType(): Type
The type of the first value; the union of the values when the order of the keys is not known.
The type of the first value; the union of the values when the order of the keys is not known.
Implements
|
#
|
public
|
getLastIterableValueType(): Type
The type of the last value; the union of the values when the order of the keys is not known.
The type of the last value; the union of the values when the order of the keys is not known.
Implements
|
#
|
public
|
isConstantArray(): TrinaryLogic
|
#
|
public
|
isList(): TrinaryLogic
A list is an array with sequential integer keys starting from 0 with no gaps.
A list is an array with sequential integer keys starting from 0 with no gaps.
Implements
|
#
|
public
|
toBoolean(): BooleanType
|
#
|
public
|
toInteger(): Type
|
#
|
public
|
toFloat(): Type
|
#
|
public
|
generalize(GeneralizePrecision $precision): Type
Removes constant value information. E.g. 'foo' -> string, 1 -> int.
Used when types become too complex to track precisely (e.g. loop iterations).
Removes constant value information. E.g. 'foo' -> string, 1 -> int.
Used when types become too complex to track precisely (e.g. loop iterations).
Implements
|
#
|
public
|
generalizeValues(): self
|
#
|
public
|
getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
Models array_keys($array, $searchValue, $strict).
Models array_keys($array, $searchValue, $strict).
Implements
|
#
|
public
|
getKeysArray(): self
Models array_keys($array).
Models array_keys($array).
Implements
|
#
|
public
|
getValuesArray(): self
Models array_values($array).
Models array_values($array).
Implements
|
#
|
public
|
getValuesInKeyOrder(): self
The values in the order the array has them, for code that walks the values by position: this shape when it
knows its key order, otherwise the list of the values that can come at each position.
The values in the order the array has them, for code that walks the values by position: this shape when it
knows its key order, otherwise the list of the values that can come at each position.
|
#
|
public
|
inRenumberingOrder(): self
This shape as code that renumbers its integer keys by their positions, like array_merge() and unpacking, sees
it: when it does not know its key order, each of the integer keys holds any of their values, the required
ones listed first. The string keys keep their values.
This shape as code that renumbers its integer keys by their positions, like array_merge() and unpacking, sees
it: when it does not know its key order, each of the integer keys holds any of their values, the required
ones listed first. The string keys keep their values.
|
#
|
public
|
describe(VerbosityLevel $level): string
|
#
|
public
|
inferTemplateTypes(Type $receivedType): TemplateTypeMap
Infers the real types of TemplateTypes found in $this, based on
the received Type. E.g. if $this is array<T> and $receivedType
is array<int>, infers T = int.
Infers the real types of TemplateTypes found in $this, based on
the received Type. E.g. if $this is array<T> and $receivedType
is array<int>, infers T = int.
Implements
|
#
|
public
|
getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
Returns the template types referenced by this Type, recursively.
Returns the template types referenced by this Type, recursively.
The return value is a list of TemplateTypeReferences, who contain the
referenced template type as well as the variance position in which it was
found.
For example, calling this on array<Foo<T>,Bar> (with T a template type)
will return one TemplateTypeReference for the type T.
Parameters
| $positionVariance |
The variance position in
which the receiver type was
found.
|
Implements
|
#
|
public
|
tryRemove(Type $typeToRemove): ?Type
|
#
|
public
|
traverse(callable $cb): Type
Returns a new instance with all inner types mapped through $cb.
Returns the same instance if inner types did not change.
Returns a new instance with all inner types mapped through $cb.
Returns the same instance if inner types did not change.
Not used directly — use TypeTraverser::map() instead.
Implements
|
#
|
public
|
traverseSimultaneously(Type $right, callable $cb): Type
Like traverse(), but walks two types simultaneously.
Like traverse(), but walks two types simultaneously.
Not used directly — use SimultaneousTypeTraverser::map() instead.
Implements
|
#
|
public
|
isKeysSupersetOf(self $otherArray): bool
|
#
|
public
|
mergeWith(self $otherArray): self
|
#
|
public
|
inMergedKeyOrderOf(self $a, self $b): self
This shape, holding the keys of two shapes merged into one, with its keys in an order both of them keep:
a key only one of them lists stays between the keys it is listed between there. Without a known key order
when the shapes do not list the keys they share in one known order.
This shape, holding the keys of two shapes merged into one, with its keys in an order both of them keep:
a key only one of them lists stays between the keys it is listed between there. Without a known key order
when the shapes do not list the keys they share in one known order.
|
#
|
public
|
getKeyTypesInMergedOrderWith(self $other): list<ConstantIntegerType|ConstantStringType>|null
The keys of both shapes in an order both of them keep: a key only one of them lists stays between the keys
it is listed between there. Null when the shapes do not list the keys they share in one known order.
The keys of both shapes in an order both of them keep: a key only one of them lists stays between the keys
it is listed between there. Null when the shapes do not list the keys they share in one known order.
|
#
|
public
|
makeOffsetRequired(Type $offsetType): self
|
#
|
public
|
makeList(): Type
|
#
|
public
|
makeListMaybe(): Type
Downgrades the list-ness of the array from Yes to Maybe (e.g. for
asort/uksort/etc. which preserve keys but break list ordering).
Other shape information (keys, values, accessories like NonEmpty) is
preserved.
Downgrades the list-ness of the array from Yes to Maybe (e.g. for
asort/uksort/etc. which preserve keys but break list ordering).
Other shape information (keys, values, accessories like NonEmpty) is
preserved.
Implements
|
#
|
public
|
mapValueType(callable $cb): Type
Models "same keys, every value transformed" (e.g. array_walk,
array_map($cb, $a), preg_replace* over an array subject). Keys
and accessories like list-ness / non-emptiness are preserved.
Models "same keys, every value transformed" (e.g. array_walk,
array_map($cb, $a), preg_replace* over an array subject). Keys
and accessories like list-ness / non-emptiness are preserved.
Implements
|
#
|
public
|
mapKeyType(callable $cb): Type
Replaces the iterable key type via $cb($currentKeyType). For
ArrayType rewrites the key type wholesale; for ConstantArrayType
the explicit keys (which are already precise constants) are preserved
— pass-through, matching the prior TypeTraverser-based callers.
Used to widen / narrow the key type after a foreach narrowed $key
via is_int($key) / is_string($key) checks.
Replaces the iterable key type via $cb($currentKeyType). For
ArrayType rewrites the key type wholesale; for ConstantArrayType
the explicit keys (which are already precise constants) are preserved
— pass-through, matching the prior TypeTraverser-based callers.
Used to widen / narrow the key type after a foreach narrowed $key
via is_int($key) / is_string($key) checks.
Implements
|
#
|
public
|
makeAllArrayKeysOptional(): Type
Marks every explicit key in a ConstantArrayType as optional (the
shape can have any subset of the original keys). For non-CAT arrays
this is a no-op — they already model arbitrary subsets. Used by
preg_replace* over array subjects, where the callback can drop
entries.
Marks every explicit key in a ConstantArrayType as optional (the
shape can have any subset of the original keys). For non-CAT arrays
this is a no-op — they already model arbitrary subsets. Used by
preg_replace* over array subjects, where the callback can drop
entries.
Implements
|
#
|
public
|
changeKeyCaseArray(?int $case): Type
Models array_change_key_case($a, $case). String keys are case-folded
(constant ones to a specific value, general ones via accessories);
non-string keys, values, accessories and list-ness are preserved.
$case matches PHP's CASE_LOWER / CASE_UPPER; null means the
case is non-constant and the result is the union of both folds.
Models array_change_key_case($a, $case). String keys are case-folded
(constant ones to a specific value, general ones via accessories);
non-string keys, values, accessories and list-ness are preserved.
$case matches PHP's CASE_LOWER / CASE_UPPER; null means the
case is non-constant and the result is the union of both folds.
Implements
|
#
|
public
|
filterArrayRemovingFalsey(): Type
Models array_filter($a) (no callback): drops entries whose value is
definitely falsey, marks possibly-falsey entries optional, keeps
definitely-truthy entries unchanged. Keys are preserved; list-ness
is downgraded since gaps may appear.
Models array_filter($a) (no callback): drops entries whose value is
definitely falsey, marks possibly-falsey entries optional, keeps
definitely-truthy entries unchanged. Keys are preserved; list-ness
is downgraded since gaps may appear.
Implements
|
#
|
public
|
toPhpDocNode(): TypeNode
|
#
|
public
|
toNativePhpTypeNode(NativeTypePosition $position)
The native PHP type declaration for this type at the given position,
widened to what the position allows, e.g. non-empty-string to string.
Returns null when the type cannot be declared natively there.
The native PHP type declaration for this type at the given position,
widened to what the position allows, e.g. non-empty-string to string.
Returns null when the type cannot be declared natively there.
Implements
|
#
|
public
static
|
isValidIdentifier(string $value): bool
|
#
|
public
|
getFiniteTypes(): array
Returns a list of finite values this type can take.
Returns a list of finite values this type can take.
Examples:
- for bool: [true, false]
- for int<0, 3>: [0, 1, 2, 3]
- for enums: list of enum cases
- for scalars: the scalar itself
For infinite types it returns an empty array.
Implements
|
#
|
public
|
hasTemplateOrLateResolvableType(): bool
Performance optimization to skip template resolution when no templates are present.
Performance optimization to skip template resolution when no templates are present.
Implements
|
#
|
public
|
hasUnresolvedTemplateArgumentType(): bool
Whether an UnresolvedTemplateArgumentType occurs anywhere in the type:
a template argument that is still being inferred.
Whether an UnresolvedTemplateArgumentType occurs anywhere in the type:
a template argument that is still being inferred.
Implements
|
#
|
public
|
tryUnion(Type $otherType): ?Type
The union with the other type as this type alone, when this type absorbs
it by more than being its supertype, such as mixed~int|1 being
mixed~(int<min, 0>|int<2, max>). Null when it does not; TypeCombinator::union()
asks the other type the same.
The union with the other type as this type alone, when this type absorbs
it by more than being its supertype, such as mixed~int|1 being
mixed~(int<min, 0>|int<2, max>). Null when it does not; TypeCombinator::union()
asks the other type the same.
Implements
|
#
|
public
|
tryIntersect(Type $otherType): ?Type
The intersection with the other type as one type, when this type knows it
better than as the subtype of the two, such as Foo & object~Bar being
Foo~Bar. Null when it does not; TypeCombinator::intersect() asks the
other type the same, and asks both before it drops supertypes. A type that
compares the two to find out may return the one the other holds instead.
The intersection with the other type as one type, when this type knows it
better than as the subtype of the two, such as Foo & object~Bar being
Foo~Bar. Null when it does not; TypeCombinator::intersect() asks the
other type the same, and asks both before it drops supertypes. A type that
compares the two to find out may return the one the other holds instead.
Implements
|
#
|