PHPCSUtils 1.1.0

TypeString
in package

FinalYes

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. The is*() 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
see
BCFile::getMethodParameters()
see
BCFile::getMethodProperties()
see
BCFile::getMemberProperties()
see
Constants::getProperties()
see
FunctionDeclarations::getParameters()
see
FunctionDeclarations::getProperties()
see
Variables::getMemberProperties()
since
1.1.0

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
since
1.1.0
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
since
1.1.0
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
since
1.1.0
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
since
1.1.0
Return values
bool

isIntersection()

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
since
1.1.0
Return values
bool

isKeyword()

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
since
1.1.0
Return values
bool

isNullable()

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
since
1.1.0
Return values
bool

isSingular()

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
since
1.1.0
Return values
bool

isUnion()

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
since
1.1.0
Return values
bool

normalizeCase()

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
since
1.1.0
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
since
1.1.0
throws
TypeError

If passed $typeString is not a string.

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
since
1.1.0
throws
TypeError

If passed $typeString is not a string.

Return values
array<string, string>

Associative array with the unique types as both the key as well as the value.


        
On this page

Search results