PHPackages                             bermudaphp/types - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. bermudaphp/types

AbandonedArchivedLibrary[Testing &amp; Quality](/categories/testing)

bermudaphp/types
================

Runtime type checking and validation library for PHP

v1.0(1y ago)0141MITPHPPHP ^8.4CI failing

Since Jun 29Pushed 1y agoCompare

[ Source](https://github.com/bermudaphp/types)[ Packagist](https://packagist.org/packages/bermudaphp/types)[ RSS](/packages/bermudaphp-types/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (1)

Bermuda Types
=============

[](#bermuda-types)

Runtime type checking and validation library for PHP 8.4+.

Installation
------------

[](#installation)

```
composer require bermudaphp/types
```

Features
--------

[](#features)

- 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

Types Utility
-------------

[](#types-utility)

The `Types` class provides comprehensive runtime type checking and validation.

### Basic Type Checking

[](#basic-type-checking)

```
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
```

### Type Assertions

[](#type-assertions)

```
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');
```

### Advanced Type Detection

[](#advanced-type-detection)

```
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);
```

### Class and Interface Validation

[](#class-and-interface-validation)

```
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']);
```

### Real-world Examples

[](#real-world-examples)

```
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...
    }
}
```

API Reference
-------------

[](#api-reference)

### Type Constants

[](#type-constants)

- `Types::TYPE_ARRAY` - Array type
- `Types::TYPE_OBJECT` - Object type
- `Types::TYPE_INT` - Integer type
- `Types::TYPE_BOOL` - Boolean type
- `Types::TYPE_STRING` - String type
- `Types::TYPE_RESOURCE` - Resource type
- `Types::TYPE_CALLABLE` - Callable type
- `Types::TYPE_FLOAT` - Float type
- `Types::TYPE_NULL` - Null type

### Flag Constants

[](#flag-constants)

- `Types::CALLABLE_AS_OBJECT` - Treat callable objects as objects
- `Types::OBJECT_AS_CLASS` - Return class name instead of 'object'

### Methods

[](#methods)

#### `getType(mixed $value, int $flags = 0): string`

[](#gettypemixed-value-int-flags--0-string)

Determines the type of a variable.

#### `is(mixed $value, string $expectedType): bool`

[](#ismixed-value-string-expectedtype-bool)

Checks if the value matches the specified type.

#### `isAny(mixed $value, array $expectedTypes): bool`

[](#isanymixed-value-array-expectedtypes-bool)

Checks if the value matches any of the provided types.

#### `assert(mixed $value, string|array $expectedTypes, ?string $message = null): void`

[](#assertmixed-value-stringarray-expectedtypes-string-message--null-void)

Asserts that the value matches one of the allowed types.

#### `enforce(mixed $value, string|array $expectedTypes, ?string $message = null): void`

[](#enforcemixed-value-stringarray-expectedtypes-string-message--null-void)

Alias for assert() method (backward compatibility).

#### `assertNotNull(mixed $value, string|array $expectedTypes, ?string $message = null): mixed`

[](#assertnotnullmixed-value-stringarray-expectedtypes-string-message--null-mixed)

Asserts that the value is not null and matches one of the allowed types.

#### `isClass(mixed $value, ?string $expectedClass = null): bool`

[](#isclassmixed-value-string-expectedclass--null-bool)

Checks if the value is a valid class name.

#### `isInterface(mixed $value, ?string $expectedInterface = null): bool`

[](#isinterfacemixed-value-string-expectedinterface--null-bool)

Checks if the value is a valid interface name.

#### `isSubclassOf(mixed $value, string $parentClass): bool`

[](#issubclassofmixed-value-string-parentclass-bool)

Checks if the value is a subclass of the specified class.

#### `isInstanceOf(mixed $value, string $className): bool`

[](#isinstanceofmixed-value-string-classname-bool)

Checks if the value is an instance of the specified class or interface.

License
-------

[](#license)

MIT License. See LICENSE file for details.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance49

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

369d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/20490712?v=4)[Andrey Shelamkoff](/maintainers/Shelamkoff)[@Shelamkoff](https://github.com/Shelamkoff)

---

Top Contributors

[![Shelamkoff](https://avatars.githubusercontent.com/u/20490712?v=4)](https://github.com/Shelamkoff "Shelamkoff (37 commits)")

---

Tags

php8validationassertiontypestype-checkingruntime-types

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bermudaphp-types/health.svg)

```
[![Health](https://phpackages.com/badges/bermudaphp-types/health.svg)](https://phpackages.com/packages/bermudaphp-types)
```

###  Alternatives

[beberlei/assert

Thin assertion library for input validation in business models.

2.4k101.9M635](/packages/beberlei-assert)[zenstruck/assert

Standalone, lightweight, framework agnostic, test assertion library.

8217.2M10](/packages/zenstruck-assert)[osteel/openapi-httpfoundation-testing

Validate HttpFoundation requests and responses against OpenAPI (3+) definitions

1202.1M7](/packages/osteel-openapi-httpfoundation-testing)[fr3d/swagger-assertions

Test your API requests and responses against your swagger definition

139874.2k5](/packages/fr3d-swagger-assertions)[nilportugues/assert

A simple and elegant assertion and guard methods library for input validation.

1066.2k7](/packages/nilportugues-assert)[matthiasnoback/phpunit-asynchronicity

Library for asserting things that happen asynchronously with PHPUnit

36252.5k7](/packages/matthiasnoback-phpunit-asynchronicity)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
