This is a collection of PHPStan utilities you can use to transform types.
composer require vixducis/phpstan-utilitiesAfterwards, add the following section to your phpstan.neon configuration file:
includes:
- vendor/vixducis/phpstan-utilities/extension.neon
This package provides several type transformation utilities for use with PHPStan. Each utility can be used as a generic type in your PHPDoc or PHPStan configuration.
Sometimes, you'll have a template type that contains a template, but you still want to extract the values from it.
Usage:
/** @var ArrayValues<T> */This utility extracts the value types from an array type T. If T is an array, it returns the type of its values. Otherwise, it returns an error type.
This differs from value-of: value-of<array{a:int,b:string}> will return int|string. ArrayValues<array{a:int,b:string}> will return array{int,string}.
This utility unwraps a singleton array type, i.e., if the type is an array containing exactly one element, it yields both the original array type and the value type as a union. Otherwise, it returns the original type.
Usage:
/** @var UnwrapSingletonArray<T> */- If
Tis an array of exactly one element, returnsT|V(whereVis the value type). - Otherwise, returns
T.
This utility converts a union type into an intersection type.
Usage:
/** @var UnionToIntersection<T> */If T is a union type (e.g., A|B), UnionToIntersection<T> produces an intersection (e.g., A&B). If T is not a union, returns an error type.