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 yesterday

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 27% 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

3022d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/18d14d64b6e8e25cc473c10fbb699946e427b993cd78e7fae2c5a373f2ae5a54?d=identicon)[joshbrw](/maintainers/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

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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