Nette Documentation Preview

PHP Type

Nette\Utils\Type is a PHP data type class available since nette/utils 3.2.5.

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[]

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

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

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

getTypes(): Type[]

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

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

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

getSingleName(): ?string

Returns the type name for single 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

isSingle(): bool

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

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

$type = Type::fromString('?Foo'); // nebo 'Foo|null'
$type->isSingle();       // 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 single 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 single 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

allow(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