PHPackages                             ashallendesign/type-safe - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. ashallendesign/type-safe

AbandonedArchivedLibrary[Validation &amp; Sanitization](/categories/validation)

ashallendesign/type-safe
========================

A package for ensuring variable types in PHP

v1.0.0(4y ago)14262MITPHPPHP ^8.0

Since Nov 30Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ash-jc-allen/type-safe)[ Packagist](https://packagist.org/packages/ashallendesign/type-safe)[ Docs](https://github.com/ash-jc-allen/type-safe)[ GitHub Sponsors](https://github.com/ash-jc-allen)[ RSS](/packages/ashallendesign-type-safe/feed)WikiDiscussions master Synced 1mo ago

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

[![PHP Type Safe logo](/docs/logo.png)](/docs/logo.png)

[![Latest Version on Packagist](https://camo.githubusercontent.com/98f1b364ad6df64bbe95390884804b32dc7989499197bc9e10877ce47700d265/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f617368616c6c656e64657369676e2f747970652d736166652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ashallendesign/type-safe)[![Build Status](https://camo.githubusercontent.com/339e1ae4c524c7b4d78494d0401c8bbf1250022b8ec2e9a61dcf4db165890147/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6173682d6a632d616c6c656e2f747970652d736166652f72756e2d74657374733f7374796c653d666c61742d737175617265)](https://github.com/ash-jc-allen/type-safe)[![Total Downloads](https://camo.githubusercontent.com/cc51cdb6944f483bd5c896fef4e85cc4ae777c1edbf4fe0d5ea1dea751598d19/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f617368616c6c656e64657369676e2f747970652d736166652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ashallendesign/type-safe)[![PHP from Packagist](https://camo.githubusercontent.com/4413983f62e51463b6b0b44b40fcd5696590ffd06392ef7eeefde82c667f6fab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f617368616c6c656e64657369676e2f747970652d736166653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ashallendesign/type-safe)[![GitHub license](https://camo.githubusercontent.com/ceebe99e31f9e729716d8b5b863ed5acee6ad4ecf3aef6e1feff56837b6ab7bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6173682d6a632d616c6c656e2f747970652d736166653f7374796c653d666c61742d737175617265)](https://github.com/ash-jc-allen/short-url/blob/master/LICENSE)

Table of Contents
-----------------

[](#table-of-contents)

- [Overview](#overview)
- [Installation](#installation)
- [Usage](#usage)
    - [Simple Checks](#simple-checks)
    - [Advanced Checks](#advanced-checks)
    - [Custom Checks](#custom-checks)
    - [Skipping Checks](#skipping-checks)
- [Testing](#testing)
- [Security](#security)
- [Contribution](#contribution)
- [Credits](#credits)
- [Changelog](#changelog)
- [License](#license)

Overview
--------

[](#overview)

Type Safe is a lightweight package that you can use in your PHP projects to ensure your variables' types.

If you're interested in reading about why I made this package, [check out this blog post](https://ashallendesign.co.uk/blog/type-safe-package-for-php).

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

[](#installation)

You can install the package via Composer:

```
composer require ashallendesign/type-safe
```

The package has been developed and tested to work with the following minimum requirements:

- PHP 8.0

Usage
-----

[](#usage)

### Simple Checks

[](#simple-checks)

Validating that a property is an integer:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::INT);
```

Validating that a property is a string:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::STRING);
```

Validating that a property is a boolean:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::BOOLEAN);
```

Validating that a property is a closure:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::CLOSURE);
```

Validating that a property is an object:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::OBJECT);
```

Validating that a property is an array:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::ARRAY);
```

Validating that a property is an associative array:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::ASSOC_ARRAY);
```

### Advanced Checks

[](#advanced-checks)

Validating that a property is an object of a specific class:

```
use App\Models\User;
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::object(User::class));
```

Validating that a property is an array containing specific fields:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::arrayOf(Type::INT));
```

Validating that a property is an associative array containing specific fields:

```
use AshAllenDesign\TypeSafe\Type;

$validatedField = safe($field, Type::assocArrayOf(Type::STRING, Type::STRING));
```

### Custom Checks

[](#custom-checks)

You might want to use your own custom checks that aren't provided in the package by default. To do this, you can create your own class that implements the `AshAllenDesign\TypeSafe\Check` interface.

The interface enforces two methods: `passes()` and `message()`. The `passes()` method is used to define your logic that determines if the field is the correct type. The `message()` method is used to return the message that will be passed to the thrown exception if the validation fails.

For example, if we wanted to create a custom check to assert that our field was a Laravel `Collection` that only contained `User` models, it might look something like this:

```
use App\Models\User;
use AshAllenDesign\TypeSafe\Check;
use Illuminate\Support\Collection;

class LaravelUserCollection implements Check
{
    public function passes(mixed $prop): bool
    {
        if (!$prop instanceof Collection) {
            return false;
        }

        return $prop->whereInstanceOf(User::class)->count() === $prop->count();
    }

    public function message(mixed $prop): string
    {
        return 'One of the items is not a User model.';
    }
}
```

We could then use that check like so:

```
$collection = collect([new User(), new TestCase()]);

safe($collection, new LaravelUserCollection());
```

### Skipping Checks

[](#skipping-checks)

There may be times when you don't want to run the type checks. For example, you might want to disable them in production environments and only run them in local, testing and staging environments. To skip the checks, you can simply use the `skipChecks` like shown in the example below:

```
use AshAllenDesign\TypeSafe\Type;
use AshAllenDesign\TypeSafe\TypeSafe;

TypeSafe::skipChecks();

$validatedField = safe($field, Type::ASSOC_ARRAY);
```

### Helpers Methods

[](#helpers-methods)

There are three different ways that you can use the package to add type safe checks to your code.

The first method is by using the `TypeSafe` object itself like so:

```
use AshAllenDesign\TypeSafe\TypeSafe;

$validatedField = (new TypeSafe())->safe($field, Type::INT);
```

Alternatively, you can use the `safe()` helper function that achieves the same thing as the code above. You can use the helper function like so:

```
$validatedField = safe($field, Type::INT);
```

The `TypeSafe` also includes helper methods that you can use for all the simple checks. The example shows how you can validate an integer field:

```
use AshAllenDesign\TypeSafe\TypeSafe;

$validatedField = TypeSafe::int($field);
```

Testing
-------

[](#testing)

To run the tests for the package, you can use the following command:

```
composer test
```

Security
--------

[](#security)

If you find any security related issues, please contact me directly at  to report it.

Contribution
------------

[](#contribution)

If you wish to make any changes or improvements to the package, feel free to make a pull request.

To contribute to this library, please use the following guidelines before submitting your pull request:

- Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests pass and write more if needed.
- Follow [PSR-12](https://www.php-fig.org/psr/psr-12/) coding standards.
- Make all pull requests to the `master` branch.

Credits
-------

[](#credits)

- [Ash Allen](https://ashallendesign.co.uk)
- [All Contributors](https://github.com/ash-jc-allen/type-safe/graphs/contributors)

Changelog
---------

[](#changelog)

Check the [CHANGELOG](CHANGELOG.md) to get more information about the latest changes.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.8% 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

1620d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b3920670b9243211cc80d219d943d782bd3ecae81b81c524e03538c0f094f392?d=identicon)[ashallendesign](/maintainers/ashallendesign)

---

Top Contributors

[![ash-jc-allen](https://avatars.githubusercontent.com/u/39652331?v=4)](https://github.com/ash-jc-allen "ash-jc-allen (23 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

phpashallendesigntype-safety

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ashallendesign-type-safe/health.svg)

```
[![Health](https://phpackages.com/badges/ashallendesign-type-safe/health.svg)](https://phpackages.com/packages/ashallendesign-type-safe)
```

PHPackages © 2026

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