PHP Type

Nette\Utils\Type is a PHP data type class.

Installation:

composer require nette/utils

All examples assume the following class alias is defined:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

The static method creates a Type object based on reflection. The parameter can be a ReflectionMethod or ReflectionFunction object (returns the type of the return value) or a ReflectionParameter or ReflectionProperty object. Resolves self, static and parent to the actual class name. If the subject has no type, it returns null.

class DemoClass
{
	public self $foo;
}

$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'

fromString(string $type): Type

The static method creates the Type object according to the text notation.

$type = Type::fromString('Foo|Bar');
echo $type;      // 'Foo|Bar'

getNames(): (string|array)[]

Returns the array of subtypes that make up the compound type as strings.

$type = Type::fromString('string|null'); // nebo '?string'
$type->getNames();  // ['string', 'null']

$type = Type::fromString('(Foo&Bar)|string');
$type->getNames();  // [['Foo', 'Bar'], 'string']

getTypes(): Type[]

Returns the array of subtypes that make up the compound type as Type objects:

$type = Type::fromString('string|null'); // or '?string'
$type->getTypes();  // [Type::fromString('string'), Type::fromString('null')]

$type = Type::fromString('(Foo&Bar)|string');
$type->getTypes();  // [Type::fromString('Foo&Bar'), Type::fromString('string')]

$type = Type::fromString('Foo&Bar');
$type->getTypes();  // [Type::fromString('Foo'), Type::fromString('Bar')]

getSingleName(): ?string

Returns the type name for simple types, otherwise null.

$type = Type::fromString('string|null');
echo $type;                       // '?string'
echo $type->getSingleName();      // 'string'

$type = Type::fromString('?Foo');
echo $type;                       // '?Foo'
echo $type->getSingleName();      // 'Foo'

$type = Type::fromString('Foo|Bar');
echo $type;                       // 'Foo|Bar'
echo $type->getSingleName();      // null

isSimple(): bool

Returns whether it is a simple type. Simple nullable types are also considered to be simple types:

$type = Type::fromString('string');
$type->isSimple();       // true
$type->isUnion();        // false

$type = Type::fromString('?Foo'); // nebo 'Foo|null'
$type->isSimple();       // true
$type->isUnion();        // true

isUnion(): bool

Returns whether it is a union type.

$type = Type::fromString('Foo&Bar');
$type->isUnion();        // true

isIntersection(): bool

Returns whether it is an intersection type.

$type = Type::fromString('string&int');
$type->isIntersection(); // true

isBuiltin(): bool

Returns whether the type is both a simple and a PHP built-in type.

$type = Type::fromString('string');
$type->isBuiltin(); // true

$type = Type::fromString('string|int');
$type->isBuiltin(); // false

$type = Type::fromString('Foo');
$type->isBuiltin(); // false

isClass(): bool

Returns whether the type is both a simple and a class name.

$type = Type::fromString('string');
$type->isClass();   // false

$type = Type::fromString('Foo|null');
$type->isClass();   // true

$type = Type::fromString('Foo|Bar');
$type->isClass();   // false

isClassKeyword(): bool

Determine whether the type is one of the internal types self, parent, static.

$type = Type::fromString('self');
$type->isClassKeyword();   // true

$type = Type::fromString('Foo');
$type->isClassKeyword();   // false

allows(string $type): bool

The allows() method verifies type compatibility. For example, it allows to check if a value of a certain type could be passed as a parameter.

$type = Type::fromString('string|null');
$type->allows('string'); // true
$type->allows('null');   // true
$type->allows('Foo');    // false

$type = Type::fromString('mixed');
$type->allows('null');   // true
version: 4.0 3.x