PHPackages                             joshbrw/type-enforcement - 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. joshbrw/type-enforcement

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

joshbrw/type-enforcement
========================

Enforce types and throw InvalidArgumentExceptions upon invalid types.

v0.1.2(8y ago)051MITPHPPHP &gt;=7.0

Since Feb 1Pushed 8y agoCompare

[ Source](https://github.com/joshbrw/PHP-Type-Enforcer)[ Packagist](https://packagist.org/packages/joshbrw/type-enforcement)[ RSS](/packages/joshbrw-type-enforcement/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Type Enforcement
================

[](#type-enforcement)

If you've ever come across a time where you're writing loosely-coupled code but want to check a parameters type at the start of a method, then this library is for you.

How you're probably doing it
----------------------------

[](#how-youre-probably-doing-it)

```
class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        if (!$author instanceof SomeAuthor) {
            throw new \InvalidArgumentException('The author must be an instance of SomeAuthor');
        }
    }
}
```

Let's refactor!
---------------

[](#lets-refactor)

```
use Joshbrw\TypeEnforcement\Type;

class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        /* Throws \InvalidArgumentException on invalid input */
        Type::enforce($author, SomeAuthor::class);
    }
}
```

Custom Exception Messages
-------------------------

[](#custom-exception-messages)

By default the package will provide a useful exception message, stating which type is expected and which type has been provided, i.e:

```
Expected [Tests\NonExistentClass], [array '["array"]'] provided.

```

This exception message can be switched out by providing a custom message as the third parameter, i.e.:

```
Type::enforce($variable, Type::class, 'The variable must be a Type!');
```

Not a fan of static method calls?
---------------------------------

[](#not-a-fan-of-static-method-calls)

This package also includes a helper method that accepts the same parameters:

```
enforce_type($variable, Type::class, 'The variable must be a Type!');

```

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

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

Every ~0 days

Total

3

Last Release

3071d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/779766?v=4)[Josh Brown](/maintainers/joshbrw)[@joshbrw](https://github.com/joshbrw)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/joshbrw-type-enforcement/health.svg)

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

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

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