Typhoon Type is an object abstraction over the modern PHP type system. Use this library to build tools that work with
sophisticated types:
use function Typhoon \Type \arrayShapeT ;
use function Typhoon \Type \objectT ;
use function Typhoon \Type \nonEmptyListT ;
$ json = <<<JSON { "people": [ {"name": "Valentin"}, {"name": "Andrey"} ], } JSON ;
final readonly class Person
{
public function __construct (
public string $ name ,
) {} }
$ type = arrayShapeT ([
'people ' => nonEmptyListT (objectT (Person::class)),
]);
$ request = new Mapper ()->map ($ json , $ type );composer require typhoon/type
To print any type, use stringify():
use function Typhoon \Type \stringify ;
$ type = arrayShapeT ([
'people ' => nonEmptyListT (objectT (Person::class)),
]);
var_dump (stringify ($ type ));
// array{'people': non-empty-list<Person>} Typhoon Native nullTnullvoidTvoidneverTneverfalseTfalsetrueTtrueboolTboolintTintfloatTfloatstringTstringarrayTarrayobjectTobjectobjectT(Foo::class)FooselfTselfparentTparentstaticTstaticiterableTiterablecallableTcallableresourceTresourcenullOrT(stringT)?stringunionT(intT, stringT)int|stringintersectionT(objectT(Countable::class), objectT(Traversable::class))Countable&TraversablemixedTmixed
Typhoon PHPDoc intT(123)123positiveIntTpositive-intnegativeIntTnegative-intnonPositiveIntTnon-positive-intnonNegativeIntTnon-negative-intnonZeroIntTnon-zero-intintRangeT(-5, 6)int<-5, 6>intRangeT(max: 6)int<min, 6>intRangeT(min: -5)int<-5, max>intMaskT(1, 2, 4)int-mask<1, 2, 4>intMaskT(classConstantMaskT(Foo::class, 'INT_*')int-mask-of<Foo::INT_*>floatT(12.5)12.5floatRangeT(-0.001, 2.344)float<-0.001, 2.344>
Typhoon PHPDoc nonEmptyStringTnon-empty-stringtruthyStringT, nonFalsyStringTtruthy-string, non-falsy-stringnumericStringTnumeric-stringlowercaseStringlowercase-stringstringT('abc')'abc'classT(Foo::class), classStringT(Foo::class), classT(objectT(Foo::class))class-string<Foo>stringT(Foo::class), classConstantT(Foo::class, 'class')Foo::classliteralStringTliteral-string
Typhoon PHPDoc constantT('PHP_INT_MAX')PHP_INT_MAXconstantMaskT('JSON_*')JSON_*classConstantT(Foo::class, 'BAR')Foo::BARclassConstantMaskT(Foo::class, 'IS_*')Foo::IS_*
PHPDoc arrays and iterables Typhoon PHPDoc arrayT(value: objectT(Foo::class))Foo[]listT(stringT)list<string>nonEmptyListT(stringT)non-empty-list<string>listShapeT([intT, stringT])list{int, string}arrayShapeT([intT, optional(stringT)])list{int, 1?: string}unsealedListShapeT([intT])list{int, ...}unsealedListShapeT([intT], stringT)list{int, ...<string>}arrayT(value: stringT)array<string>arrayT(intT, stringT)array<int, string>nonEmptyArrayT(arrayKeyT, stringT)non-empty-array<array-key, string>arrayShapeT()array{}arrayShapeT([intT, stringT])array{int, string}arrayShapeT([intT, 'a' => optional(stringT)])array{int, a?: string}unsealedArrayShapeT([intT])array{int, ...}unsealedArrayShapeT([floatT], intT, stringT)array{float, ...<int, string>}keyOfT(classConstantT(Foo::class, 'ARRAY'))key-of<Foo::ARRAY>valueOfT(classConstantT(Foo::class, 'ARRAY'))value-of<Foo::ARRAY>offsetT($TArray->type, $TKey->type)TArray[TKey]iterableT(objectT, stringT)iterable<object, string>iterableT(value: stringT)iterable<string>
Typhoon PHPDoc objectT(Foo::class, [stringT, floatT])Foo<string, float>selfT([stringT, floatT])self<string, float>parentT([stringT, floatT])parent<string, float>staticT([stringT, floatT])static<string, float>objectShapeT(['prop' => stringT])object{prop: string}objectShapeT(['prop' => optional(stringT))])object{prop?: string}
Typhoon PHPDoc callableT([stringT], voidT)callable(string): voidcallableT([param(stringT, default: true)])callable(string=): mixedcallableT([param(stringT, variadic: true)])callable(string...): mixedcallableT([param(stringT, byRef: true)])callable(string&): mixedclosureT([stringT], voidT)Closure(string): voidclosureT([param(stringT, default: true)])Closure(string=): mixedclosureT([param(stringT, variadic: true)])Closure(string...): mixedclosureT([param(stringT, byRef: true)])Closure(string&): mixed
Typhoon PHPDoc array-keyarrayKeyTnumericTnumericscalarTscalar