vistik/typed-collections
A Laravel/PHP package for type-safe collections: enforce element types at runtime, improve IDE and static-analysis support, and keep familiar Collection ergonomics. Define typed collections for models, DTOs, scalars, and custom classes.
Collection of "users" that silently contains an array, an Eloquent model of the wrong class, or a raw scalar after a refactor. Decisions it informs: whether our service layer returns typed collections or bare Illuminate\Support\Collection, and whether that contract is enforced at runtime or only asserted in tests.Collection that overrides item-insertion paths (offsetSet, push, constructor) and throws on mismatch. That is small enough that "buy" (add a dependency) is almost never the right answer versus "build" (a 40-line abstract base class we own) — and this package is archived, which closes the buy option anyway.@template/@extends annotations on Illuminate\Support\Collection, Collection::ensure() for one-off assertions, and PHP 8 typed properties/readonly for the elements themselves. Choosing one of these is a standards decision with real cost implications (CI config, developer education, review checklists), and this package is a useful artifact for framing that trade-off.Consider it as reference material, not as a dependency, when:
*Service must be typed") and want a minimal prior-art example to sanity-check scope and API shape.Look elsewhere — i.e., do not adopt — when:
readonly; unverified against Laravel 8/9/10/11 and against illuminate/collections split-package versions. No security response path, no issue triage, and 11 stars means effectively no user base to surface breakage.protected $type string cannot express Collection<int, Collection<int, Money>>; static analysers can.Collection::ensure() plus Illuminate\Support\Collection for assertions; PHPStan/Psalm @template docblocks for compile-time enforcement; hand-rolled collection subclasses overriding offsetSet/push for domain-specific collections; Spatie laravel-data or similar for typed DTO collections with validation and transformation built in.For executives (30 seconds). We evaluated vistik/typed-collections, a small library that makes Laravel collections enforce a single element type. Recommendation: do not adopt. It is archived, last released in July 2017, has negligible community footprint, has never been validated against current PHP or Laravel versions, and carries no security-maintenance commitment. The capability it provides is approximately one afternoon of engineering to reproduce in-house with modern PHP 8 types and static analysis — which we would own, test, and upgrade on our own schedule. Adopting it would add a supply-chain and upgrade-blocker risk for negligible return; the sensible action is a short internal spike, not a dependency.
For engineering leads. Read the source as prior art, then write our own. A TypedCollection extends Illuminate\Support\Collection that overrides offsetSet, push, and the constructor to throw InvalidArgumentException on a wrong element type is roughly 40 lines and fully testable. Pair it with PHPStan @template/@extends annotations so violations fail in CI, not at runtime in production — that gives us both a static guarantee and a runtime backstop, which this package alone cannot. Prefer Laravel's built-in ensure() for one-off assertions rather than introducing a base class everywhere. Also note the package predates PHP 7.4 typed properties and PHP 8 enums, so its design will feel dated in review and shouldn't be used as an API template without modernization.
Known risks and decision triggers. Risks: archived maintenance status, no PHP 8 / current Laravel compatibility, single-type-only expressiveness, and a fork burden at the next Laravel major. Trigger to revisit: if the team's in-house implementation proves insufficient for nested or generic element types, evaluate laravel-data or a static-analysis-first approach rather than returning to this package. Verdict: reference-only; keep out of composer.json.
How can I help you explore Laravel packages today?