Runtime type checking and validation library for PHP 8.4+.
composer require bermudaphp/types
- Runtime type checking and validation via
Types
class - Support for built-in PHP types and custom classes
- Type assertions with detailed error messages
- Backward compatibility with legacy
enforce()
method - Full PHP 8.4 compatibility with typed constants
- Static analysis friendly (PHPStan Level 9, Psalm Level 1)
- Zero dependencies
- Lightweight and performant
The Types
class provides comprehensive runtime type checking and validation.
use Bermuda\Stdlib\Types;
// Check basic types
Types::is($value, Types::TYPE_STRING); // true if string
Types::is($value, Types::TYPE_INT); // true if integer
Types::is($value, 'array'); // true if array
// Check multiple types
Types::isAny($value, [Types::TYPE_STRING, Types::TYPE_INT]); // true if string OR int
// Check object instances
Types::is($user, User::class); // true if $user instanceof User
Types::isInstanceOf($user, UserInterface::class); // true if implements interface
use Bermuda\Stdlib\Types;
// Throws InvalidArgumentException if not matching
Types::assert($value, Types::TYPE_STRING);
Types::assert($value, [Types::TYPE_STRING, Types::TYPE_INT]);
// enforce() is an alias for assert() (backward compatibility)
Types::enforce($value, Types::TYPE_STRING);
// Assert not null with type check
$user = Types::assertNotNull($maybeUser, User::class);
// $user is guaranteed to be non-null User instance
// Custom error messages
Types::assert($value, Types::TYPE_INT, 'ID must be an integer');
use Bermuda\Stdlib\Types;
// Get the actual type
$type = Types::getType($value); // returns 'string', 'int', 'array', etc.
// Get class name for objects
$type = Types::getType($object, Types::OBJECT_AS_CLASS); // returns 'App\User'
// Handle callables as objects
$type = Types::getType($callable, Types::CALLABLE_AS_OBJECT);
use Bermuda\Stdlib\Types;
// Check if string is a valid class name
Types::isClass('App\User'); // true if class exists
Types::isClass($value, User::class); // true if $value === 'App\User' (case-insensitive)
// Check if string is a valid interface name
Types::isInterface('App\UserInterface'); // true if interface exists
// Check subclass relationships
Types::isSubclassOf('App\Admin', 'App\User'); // true if Admin extends User
Types::isSubclassOfAny('App\Admin', ['App\User', 'App\Manager']);
use Bermuda\Stdlib\Types;
class UserService
{
public function createUser(mixed $data): User
{
Types::assert($data, Types::TYPE_ARRAY, 'User data must be an array');
$id = Types::assertNotNull($data['id'] ?? null, Types::TYPE_INT);
$email = Types::assertNotNull($data['email'] ?? null, Types::TYPE_STRING);
return new User($id, $email);
}
public function processPayment(mixed $handler): void
{
Types::assert($handler, [
PaymentInterface::class,
Types::TYPE_CALLABLE
], 'Payment handler must implement PaymentInterface or be callable');
// Process payment...
}
}
Types::TYPE_ARRAY
- Array typeTypes::TYPE_OBJECT
- Object typeTypes::TYPE_INT
- Integer typeTypes::TYPE_BOOL
- Boolean typeTypes::TYPE_STRING
- String typeTypes::TYPE_RESOURCE
- Resource typeTypes::TYPE_CALLABLE
- Callable typeTypes::TYPE_FLOAT
- Float typeTypes::TYPE_NULL
- Null type
Types::CALLABLE_AS_OBJECT
- Treat callable objects as objectsTypes::OBJECT_AS_CLASS
- Return class name instead of 'object'
Determines the type of a variable.
Checks if the value matches the specified type.
Checks if the value matches any of the provided types.
Asserts that the value matches one of the allowed types.
Alias for assert() method (backward compatibility).
Asserts that the value is not null and matches one of the allowed types.
Checks if the value is a valid class name.
Checks if the value is a valid interface name.
Checks if the value is a subclass of the specified class.
Checks if the value is an instance of the specified class or interface.
MIT License. See LICENSE file for details.