TypeString
in package
Utility functions for use when examining type strings.
The methods in this class are intended to be used with type strings as returned by:
- [BC]File::getMethodParameters()['type_hint']
- [BC]File::getMethodProperties()['return_type']
- [BC]File::getMemberProperties()['type']
- Constants::getProperties()['type']
- FunctionDeclarations::getParameters()['type_hint']
- FunctionDeclarations::getProperties()['return_type']
- Variables::getMemberProperties()['type']
Notes:
- Type strings as retrieved from the above listed sources will not contain any whitespace, so the methods in this class have limited or no handling for surrounding or internal whitespace.
- The behaviour with type strings retrieved by other means, or non-type strings, is undefined. :warning: Using these methods with type strings retrieved from docblocks is strongly discouraged.
- The
is*()
methods will not check if the type string provided is valid, as doing so would inhibit what sniffs can flag. Theis*()
methods will only look at the form of the type string to determine if it could be valid for a certain type.- Use the NamingConventions::isValidIdentifierName() method if additional validity checks are needed on the individual "types" seen in a type string.
- And, if needed, use token walking on the tokens of the type to determine whether a type string actually complies with the type rules as set by PHP.
Tags
Table of Contents
Methods
- filterKeywordTypes() : array<int|string, string>
- Filter a list of types down to only the keyword based types.
- filterOOTypes() : array<int|string, string>
- Filter a list of types down to only the OO name based types.
- getKeywordTypes() : array<string, string>
- Retrieve a list of all PHP native keyword types.
- isDNF() : bool
- Check if a type string represents a disjunctive normal form (DNF) type.
- isIntersection() : bool
- Check if a type string represents a pure intersection type.
- isKeyword() : bool
- Check if a singular type is a PHP native keyword based type.
- isNullable() : bool
- Check if a type string represents a nullable type.
- isSingular() : bool
- Check if a type string represents a plain, singular type.
- isUnion() : bool
- Check if a type string represents a pure union type.
- normalizeCase() : string
- Normalize the case for a single type.
- toArray() : array<string|int, string>
- Split a type string to its individual types and optionally normalize the case of the types.
- toArrayUnique() : array<string, string>
- Split a type string to the unique types included and optionally normalize the case of the types.
Methods
filterKeywordTypes()
Filter a list of types down to only the keyword based types.
public
static filterKeywordTypes(array<int|string, string> $types) : array<int|string, string>
Parameters
- $types : array<int|string, string>
-
Array of types. Typically, this is an array as retrieved from the TypeString::toArray() method or the TypeString::toArrayUnique() method.
Tags
Return values
array<int|string, string> —Array with only the PHP native keyword based types. The result may be an empty array if the input array didn't contain any keyword based types or if the input was invalid.
filterOOTypes()
Filter a list of types down to only the OO name based types.
public
static filterOOTypes(array<int|string, string> $types) : array<int|string, string>
Parameters
- $types : array<int|string, string>
-
Array of types. Typically, this is an array as retrieved from the TypeString::toArray() method or the TypeString::toArrayUnique() method.
Tags
Return values
array<int|string, string> —Array with only the OO name based types. The result may be an empty array if the input array didn't contain any OO name based types or if the input was invalid.
getKeywordTypes()
Retrieve a list of all PHP native keyword types.
public
static getKeywordTypes() : array<string, string>
Tags
Return values
array<string, string> —Key and value both contain the type name in lowercase.
isDNF()
Check if a type string represents a disjunctive normal form (DNF) type.
public
static isDNF(string $typeString) : bool
This check for a strict
Parameters
- $typeString : string
-
Type string.
Tags
Return values
boolisIntersection()
Check if a type string represents a pure intersection type.
public
static isIntersection(string $typeString) : bool
Note: DNF types are not considered intersection types for the purpose of this method.
Parameters
- $typeString : string
-
Type string.
Tags
Return values
boolisKeyword()
Check if a singular type is a PHP native keyword based type.
public
static isKeyword(string $type) : bool
Parameters
- $type : string
-
The singular type.
Tags
Return values
boolisNullable()
Check if a type string represents a nullable type.
public
static isNullable(string $typeString) : bool
A nullable type in the context of this method is a type which
- starts with the nullable operator and has something after it which is being made nullable;
- or contains
null
as part of a union or DNF type.
A stand-alone null
type is not considered a nullable type, but a singular type.
Parameters
- $typeString : string
-
Type string.
Tags
Return values
boolisSingular()
Check if a type string represents a plain, singular type.
public
static isSingular(string $typeString) : bool
Note: Nullable types are not considered plain, singular types for the purposes of this method.
Parameters
- $typeString : string
-
Type string.
Tags
Return values
boolisUnion()
Check if a type string represents a pure union type.
public
static isUnion(string $typeString) : bool
Note: DNF types are not considered union types for the purpose of this method.
Parameters
- $typeString : string
-
Type string.
Tags
Return values
boolnormalizeCase()
Normalize the case for a single type.
public
static normalizeCase(string $type) : string
- Types which are recognized PHP "keyword" types will be returned in lowercase.
- Class/Interface/Enum names will be returned in their original case.
Parameters
- $type : string
-
Type to normalize the case for.
Tags
Return values
string —The case-normalized type or an empty string if the input was invalid.
toArray()
Split a type string to its individual types and optionally normalize the case of the types.
public
static toArray(string $typeString[, bool $normalize = true ]) : array<string|int, string>
Parameters
- $typeString : string
-
Type to split.
- $normalize : bool = true
-
Whether or not to normalize the case of types. Defaults to true.
Tags
Return values
array<string|int, string> —List containing all seen types in the order they were encountered.
toArrayUnique()
Split a type string to the unique types included and optionally normalize the case of the types.
public
static toArrayUnique(string $typeString[, bool $normalize = true ]) : array<string, string>
Parameters
- $typeString : string
-
Type to split.
- $normalize : bool = true
-
Whether or not to normalize the case of types. Defaults to true.
Tags
Return values
array<string, string> —Associative array with the unique types as both the key as well as the value.