sebastian/type
A collection of value objects that model the PHP type system, including simple, union, intersection, and special types. Use it to inspect, compare, and represent type information programmatically in PHP projects.
sebastian/type is the lowest-level building block for any product surface that needs to reason about PHP types — API contract generation, static analysis gates, IDE helpers, or custom validation rules. It converts "parse this type-hint string and hope" into a maintained, tested value-object model, so the buy decision is essentially settled for the type-model layer; build effort should go into the product logic on top (business rules, schema mapping, DX), not into re-implementing union/intersection/alias handling.Adopt when:
ObjectType, UnionType, intersection types) instead of string parsing.DateTimeImmutable satisfying a DateTimeInterface/DateTime expectation) and you are on PHP 8.4+ with the 7.x line.--dev or tooling-only Composer dependency.Look elsewhere when:
spatie/laravel-data-style DTO casting are the right layer; this library models types, it does not enforce them.^6.0 — v7.0.0 dropped 8.3 support, and v6 will stop receiving fixes; treat that as a hard platform constraint, not a footnote.Money or UserId type) rather than PHP type introspection — you'd be extending upstream classes for business logic, which creates maintenance coupling for little gain.For executives: This is a small, zero-runtime-dependency library from the maintainer behind PHPUnit — effectively already in our dependency graph through the test and analysis toolchain. Adopting it deliberately, rather than incidentally, lets us build type-aware validation, API contract generation, and CI quality gates on a maintained foundation instead of bespoke parsers. The 7.0.1 release closed a real correctness gap (class-alias assignability) that has historically caused false positives in analysis and wasted engineering time. Cost is a Composer line; the return is fewer type-related production defects and faster, safer refactors on our highest-risk code paths. The main decision requiring your input is platform timing: v7 requires PHP 8.4+, so standardizing there is a prerequisite, not an implementation detail.
For engineering: This is the type-model layer we'd otherwise hand-roll. ReflectionMapper bridges PHP reflection to value objects, handling unions, intersections, iterable edge cases, and aliases — exactly the cases that make DIY type checking brittle. It won't enforce anything at runtime; it's the foundation for tooling we own: PHPStan/Psalm rules, OpenAPI generation from return types, DTO compatibility tests using isAssignableFrom(). Integration is a single dev dependency plus a ReflectionMapper binding, and there's no runtime or memory cost because it's analysis-time only. Two things to get right up front: pin the major version to our supported PHP range (v7 for 8.4+, v6 if we're stuck on 8.3, accepting no future fixes), and add alias and complex-union cases to our test fixtures so the 7.0.1 behavior is locked in by tests rather than assumed.
How can I help you explore Laravel packages today?