PHPackages                             spatie/better-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/better-types

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/better-types
===================

Improved abstraction for dealing with union and named types.

1.1.0(4mo ago)823.8M—5.4%53MITPHPPHP ^8.2CI passing

Since Sep 3Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/spatie/better-types)[ Packagist](https://packagist.org/packages/spatie/better-types)[ Docs](https://github.com/spatie/better-types)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-better-types/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (9)Dependencies (4)Versions (12)Used By (3)

Better types
============

[](#better-types)

Check whether a reflection type or method accepts a given input

[![Latest Version on Packagist](https://camo.githubusercontent.com/417117da5c46dded7cc31a223ad36977422d273148dc0f5cc92261cce7f2d287/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6265747465722d74797065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/better-types)[![GitHub Tests Action Status](https://github.com/spatie/better-types/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/better-types/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://github.com/spatie/better-types/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/spatie/better-types/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/20e3d901c9002f6a3a65fa6f8344d2bdcd31437620f522c89c7443c9139e9b0d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6265747465722d74797065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/better-types)

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/5f0d68191059593d08682e692593eb042a4a63836dd2261c56db6deb772bb54a/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6265747465722d74797065732e6a70673f743d31)](https://spatie.be/github-ad-click/better-types)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

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

[](#installation)

You can install the package via composer:

```
composer require spatie/better-types
```

Usage
-----

[](#usage)

Using the `Type` class directly:

```
function (FooInterface $foo) {}

$reflectionType = …

$type = new Type($reflectionType);

$type->accepts(new Foo()); // true
$type->accepts('invalid string'); // false
```

Using the `Method` class:

```
function (?FooInterface $foo, ?BarInterface $bar) {}

$reflectionMethod = …

$method = new Method($reflectionMethod);

$method->accepts(new Foo(), new Bar()); // true
$method->accepts(bar: new Bar() foo: new Foo()); // true
$method->accepts(null, new Bar()); // true
$method->accepts(null, null); // true

$method->accepts('string', 1); // false
$method->accepts(new Foo()); // false, you can't omit values
```

Using `Handlers` to determine which methods accept a given set of input:

```
class Foo
{
    public function acceptsString(string $a) {}

    public function acceptsStringToo(string $a) {}

    public function acceptsInt(int $a) {}
}

$reflectionClass = …

$handlers = new Handlers($reflectionClass);

$handlers->accepts('string')->all(); // ['acceptsString', 'acceptsStringToo']
$handlers->accepts(1)->first(); // 'acceptsInt'
```

Using the `Attributes` class to find and instantiate attributes with a fluent API:

```
Attributes::new(AttributesTestClass::class)
    ->instanceOf(AttributesTestAttribute::class)
    ->first();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Brent Roose](https://github.com/spatie)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

63

—

FairBetter than 99% of packages

Maintenance86

Actively maintained with recent releases

Popularity57

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~204 days

Recently: every ~281 days

Total

9

Last Release

133d ago

Major Versions

0.2.0 → 1.0.02025-02-20

PHP version history (2 changes)0.0.1PHP ^8.0

1.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (20 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (6 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (6 commits)")[![maartenpaauw](https://avatars.githubusercontent.com/u/4550875?v=4)](https://github.com/maartenpaauw "maartenpaauw (6 commits)")[![zackrowe](https://avatars.githubusercontent.com/u/4922356?v=4)](https://github.com/zackrowe "zackrowe (3 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (3 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![inmanturbo](https://avatars.githubusercontent.com/u/47095624?v=4)](https://github.com/inmanturbo "inmanturbo (1 commits)")[![erikgaal](https://avatars.githubusercontent.com/u/1234268?v=4)](https://github.com/erikgaal "erikgaal (1 commits)")

---

Tags

spatiebetter-types

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[illuminate/support

The Illuminate Support package.

630113.0M41.4k](/packages/illuminate-support)[illuminate/events

The Illuminate Events package.

13557.0M2.1k](/packages/illuminate-events)[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[illuminate/config

The Illuminate Config package.

10944.5M2.5k](/packages/illuminate-config)[illuminate/pagination

The Illuminate Pagination package.

12234.1M1.0k](/packages/illuminate-pagination)[illuminate/session

The Illuminate Session package.

9939.3M849](/packages/illuminate-session)

PHPackages © 2026

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