Closed jrfnl closed 3 months ago
Rebased on top of https://github.com/PHPCSStandards/PHPCSUtils/pull/598 and added a commit to take advantage of the new exceptions. Moving to draft until https://github.com/PHPCSStandards/PHPCSUtils/pull/598 has been merged.
Rebased without changes, other than squashing the commits. Marking as ready as https://github.com/PHPCSStandards/PHPCSUtils/pull/598 has been merged now. Merging once the build passes.
... to contain utilities methods for analysing typestrings as retrieved via the
getParameters()
/getProperties()
methods.Initially, the class comes with the following methods:
getKeywordTypes(): array
to retrieve an array of all currently supported type keywords.isKeyword(string $type): bool
to determine whether a type is a keyword type.normalizeCase(string $type): string
to normalize the case of keyword types to lowercase, while leaving the case of OO-based types alone.isSingular(string $typeString): bool
to determine if a type is a purely singular type.isNullable(string $typeString): bool
to determine if a type is a nullable type, either by it having the nullable type operator or by includingnull
as part of a union or DNF type.isUnion(string $typeString): bool
to determine if a type is a union type.isIntersection(string $typeString): bool
to determine if a type is an intersection type.isDNF(string $typeString): bool
to determine if a type is an intersection type.toArray(string $typeString, bool $normalize = true): array
to split a type string to its individual types and optionally normalize the case of the types.toArrayUnique(string $typeString, bool $normalize = true): array
to split a type string to its unique individual types and optionally normalize the case of the types.filterKeywordTypes(array $types): array
to filter an array with individual types as found in a type string down to only the keyword types.filterOOTypes(array $types): array
to filter an array with individual types as found in a type string down to only the OO-name based types.Notes:
getParameters()
/getProperties()
methods, or non-type strings, is undefined. This includes type strings found in docblocks. Those are not typically supported by the methods in this class.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 thePHPCSUtils\Utils\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.Includes extensive unit tests.