Simple tools for managing PHP types

v0.2.8 2023-11-27 10:29 UTC

This package is auto-updated.

Last update: 2024-07-15 13:53:40 UTC


PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

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 test which require strict type handling.

Get news and updates on the DecodeLabs blog.


Install via Composer:

composer require decodelabs/coercion


Pass any mixed value to the available coerce methods to ensure input types in constructors:

use DecodeLabs\Coercion;

class MyClass {

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

    public function __construct(array $params) {
        $this->string = Coercion::toString($params['maybeString']);
        $this->optionalString = Coercion::toStringOrNull($params['maybeString']);
        $this->int = Coercion::toInt($params['maybeInt']);


Coercion is licensed under the MIT License. See LICENSE for the full license text.