PHPackages                             decodelabs/coercion - 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. decodelabs/coercion

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

decodelabs/coercion
===================

Simple tools for managing PHP types

v0.3.5(10mo ago)041.1k—0%20MITPHPPHP ^8.4CI passing

Since Mar 9Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/decodelabs/coercion)[ Packagist](https://packagist.org/packages/decodelabs/coercion)[ RSS](/packages/decodelabs-coercion/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (30)Used By (20)

Coercion
========

[](#coercion)

[![PHP from Packagist](https://camo.githubusercontent.com/482cc0263514b63129262d9079c82bd7c0128ac750554220408adf2624174c52/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6465636f64656c6162732f636f657263696f6e3f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/coercion)[![Latest Version](https://camo.githubusercontent.com/eee8a705b7a3b0ed0b9452331a41459d678fa8dc8d845c860a696ddf78e385d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6465636f64656c6162732f636f657263696f6e2e7376673f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/coercion)[![Total Downloads](https://camo.githubusercontent.com/145e6575f0177da61a1da8ec4cdf4612bc0d63bb2df789a37606f3d5fb0afe23/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6465636f64656c6162732f636f657263696f6e2e7376673f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/coercion)[![GitHub Workflow Status](https://camo.githubusercontent.com/806bc85e4e0d5dfb7d7d1d1d97a1ae9ebde68fbe5795e7f9f2a64b4ae1d16612/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6465636f64656c6162732f636f657263696f6e2f696e746567726174652e796d6c3f6272616e63683d646576656c6f70)](https://github.com/decodelabs/coercion/actions/workflows/integrate.yml)[![PHPStan](https://camo.githubusercontent.com/e25c14ce011edabdd0fbd2e10415b41cc5d66ed11ef3e5b7edd074c5bdd35a2d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d3434434331312e7376673f6c6f6e6743616368653d74727565267374796c653d666c6174)](https://github.com/phpstan/phpstan)[![License](https://camo.githubusercontent.com/d0b5b295490c82e73cd9be4756fb17a970df9a6f0554348c132c4a744be3841d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6465636f64656c6162732f636f657263696f6e3f7374796c653d666c6174)](https://packagist.org/packages/decodelabs/coercion)

### Simple tools for managing PHP types

[](#simple-tools-for-managing-php-types)

Coercion offers simple tools to help neatly handle coercion of mixed parameters, especially useful when dealing with higher level static analysis tests which require strict type handling.

---

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

[](#installation)

This package requires PHP 8.4 or higher.

Install via Composer:

```
composer require decodelabs/coercion
```

Usage
-----

[](#usage)

Pass any mixed value to the available coerce methods to assert types. Methods beginning with `as` will throw an exception if the value cannot be coerced to the desired type. Methods beginning with `try` will return `null` if the value cannot be coerced to the desired type. Methods beginning with `to` will return a default value if the value cannot be coerced to the desired type.

```
use DecodeLabs\Coercion;

class MyClass {

    protected string $string;
    protected ?string $optionalString;
    protected int $int;

    public function __construct(array $params) {
        $this->string = Coercion::asString($params['maybeString']); // Throw on error
        $this->string = Coercion::toString($params['maybeString']); // Convert to empty string on error
        $this->optionalString = Coercion::tryString($params['maybeString']);
    }
}
```

### Available Methods

[](#available-methods)

```
// String
Coercion::asString($value): string // Throws exception on error
Coercion::tryString($value, bool $nonEmpty = true): ?string
Coercion::toString($value): string
Coercion::isStringable($value): bool

// Bool
Coercion::toBool($value): bool
Coercion::tryBool($value): ?bool
Coercion::parseBool($value): ?bool // Only returns true for strings if string is boolsy

// Int
Coercion::asInt($value): int // Throws exception on error
Coercion::tryInt($value): ?int
Coercion::toInt($value): int
Coercion::clampInt($value, int $min, int $max): int

// Float
Coercion::asFloat($value): float // Throws exception on error
Coercion::tryFloat($value): ?float
Coercion::toFloat($value): float
Coercion::clampFloat($value, float $min, float $max): float
Coercion::clampDegrees($value, float $min, float $max): float

// Array
Coercion::asArray($value): array // Throws exception on error
Coercion::tryArray($value): ?array
Coercion::toArray($value): array

// Iterable
Coercion::asIterable($value): iterable // Throws exception on error
Coercion::tryIterable($value): ?iterable
Coercion::toIterable($value): iterable
Coercion::iterableToArray($value): array

// Object
Coercion::asObject($value): object // Throws exception on error
Coercion::tryObject($value): ?object
Coercion::toObject($value): object

// stdClass
Coercion::asStdClass($value): stdClass // Throws exception on error
Coercion::tryStdClass($value): ?stdClass
Coercion::toStdClass($value): stdClass

// Type
Coercion::asType($value, class-string $type): T // Throws exception on error
Coercion::tryType($value, class-string $type): ?T

// Lazy
Coercion::newLazyGhost(class-string $type, callable $callback): T
Coercion::newLazyProxy(class-string $type, callable $callback): T

// DateTime
Coercion::asDateTime($value): DateTimeInterface // Throws exception on error
Coercion::tryDateTime($value): ?DateTimeInterface
Coercion::toDateTime($value): DateTimeInterface // Defaults to now

// DateInterval
Coercion::asDateInterval($value): DateInterval // Throws exception on error
Coercion::tryDateInterval($value): ?DateInterval
```

Licensing
---------

[](#licensing)

Coercion is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance63

Regular maintenance activity

Popularity25

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

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

Every ~45 days

Recently: every ~36 days

Total

28

Last Release

306d ago

PHP version history (4 changes)v0.1.0PHP ^7.2|^8.0

v0.2.0PHP ^8.0

v0.2.8PHP ^8.1

v0.2.15PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a241d64d12b3b5ee94197862ec1ec30b82ed2efa34a0cd7f4c3565a021daddd?d=identicon)[betterthanclay](/maintainers/betterthanclay)

---

Top Contributors

[![betterthanclay](https://avatars.githubusercontent.com/u/1273586?v=4)](https://github.com/betterthanclay "betterthanclay (150 commits)")

---

Tags

coercionconversionphptypestypedatastructure

### Embed Badge

![Health badge](/badges/decodelabs-coercion/health.svg)

```
[![Health](https://phpackages.com/badges/decodelabs-coercion/health.svg)](https://phpackages.com/packages/decodelabs-coercion)
```

###  Alternatives

[fakerphp/faker

Faker is a PHP library that generates fake data for you.

4.0k358.5M3.5k](/packages/fakerphp-faker)[phpoption/phpoption

Option Type for PHP

2.7k541.2M159](/packages/phpoption-phpoption)[dflydev/dot-access-data

Given a deep data structure, access data by dot notation.

720359.1M86](/packages/dflydev-dot-access-data)[jetbrains/phpstorm-stubs

PHP runtime &amp; extensions header files for PhpStorm

1.4k27.7M68](/packages/jetbrains-phpstorm-stubs)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[symfony/type-info

Extracts PHP types information.

19951.9M114](/packages/symfony-type-info)

PHPackages © 2026

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