decodelabs / coercion
Simple tools for managing PHP types
Installs: 28 939
Dependents: 28
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- decodelabs/exceptional: ^0.4
Requires (Dev)
- decodelabs/phpstan-decodelabs: ^0.6
- nesbot/carbon: ^3.8
README
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.
Installation
Install via Composer:
composer require decodelabs/coercion
Usage
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']); } }
Licensing
Coercion is licensed under the MIT License. See LICENSE for the full license text.