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(2mo ago)813.1M—5.6%53MITPHPPHP ^8.2CI passing

Since Sep 3Pushed 2mo 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 1mo 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://camo.githubusercontent.com/c950cc275841cc2b62c07088472de24a94887abdee2357b2d9328c982f73595b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6265747465722d74797065732f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/spatie/better-types/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/d5e59b4ee92203b380edd1acd317f3acc1811e8210f1a527134f339ba32f2461/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6265747465722d74797065732f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](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

62

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity56

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

86d 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

[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (28 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (19 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

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[spatie/laravel-collection-macros

A set of useful Laravel collection macros

1.9k5.7M29](/packages/spatie-laravel-collection-macros)

PHPackages © 2026

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