Download the PHP package rcalicdan/ramsey-collection-typephp-extension without Composer
On this page you can find all versions of the php package rcalicdan/ramsey-collection-typephp-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rcalicdan/ramsey-collection-typephp-extension
More information about rcalicdan/ramsey-collection-typephp-extension
Files in rcalicdan/ramsey-collection-typephp-extension
Package ramsey-collection-typephp-extension
Short Description Make Ramsey Collection enforce Reified generics
License MIT
Informations about the package ramsey-collection-typephp-extension
Ramsey Collection TypePHP Extension
Enables TypePHP runtime type checking and reified generic enforcement for ramsey/collection.
Overview
The ramsey/collection library provides typed collection structures in PHP. However, because its internal type checking relies on standard string comparisons and instanceof checks against basic class names or primitive types (int, string, object), it cannot enforce complex static analysis annotations at runtime.
This extension bridges TypePHP and ramsey/collection, allowing all collection classes (Collection, Set, Queue, DoubleEndedQueue, TypedMap, etc.) to enforce modern PHPDoc type contracts dynamically without changing their public API.
Native Ramsey Collection Limitations vs. TypePHP Capabilities
| Feature | Native ramsey/collection |
With TypePHP Extension |
|---|---|---|
| Basic Class Checking | Supported (new Collection(User::class)) |
Supported and reified |
| Scalar Refinements | Unsupported (Only checks broad int or string) |
Supported (positive-int, non-empty-string, int<1, 100>, numeric-string, etc.) |
| Union Types | Unsupported (Cannot accept Dog \| Cat without base class) |
Supported (Collection<Dog \| Cat>) |
| Array Shapes | Unsupported (Only checks broad array) |
Supported (Collection<array{id: positive-int, name: non-empty-string}>) |
| Intersection & DNF Types | Unsupported | Supported (Collection<Countable & ArrayAccess>) |
| Closure Parameter Validation | Unsupported (Closure arguments in filter() are not checked) |
Supported (filter(callable(T): bool)) |
| Runtime Generic Introspection | Unsupported (Cannot inspect template T via Reflection) |
Supported (TypePHP::getGenericType($collection)) |
| Cloning State Preservation | Partial | Fully preserved and isolated in memory via \WeakMap |
Key Capabilities Added to Ramsey Collections
- Scalar Refinements within Generics: Native collections can only restrict elements to basic primitives like
intorstring. With TypePHP, you can restrict collections topositive-int,non-empty-string,int<min, max>,uppercase-string, and bitmasks. - Union Types within Generics: Native collections require all elements to extend a single common ancestor or be marked as
object. TypePHP allows heterogeneous union types such asCollection<Dog|Cat>. - Array Shapes within Generics: Validate structured associative arrays and tuples directly when inserted into a collection, ensuring nested keys and values adhere to schema rules.
- Higher-Order Closures: Methods like
filter()validate incoming elements against the declared callback signature, preventing runtime type mismatches inside closure bodies. - Reified Generic Introspection: Inspect the concrete generic type assigned to any collection instance at runtime using the
TypePHP::getGenericType()API.
Installation
Development & Testing (Recommended)
This library is primarily intended as a development dependency for local development, testing suites (Pest, PHPUnit), and CI/CD pipelines:
Production Runtime (Optional)
If your architecture requires strict runtime generic enforcement and input boundary validation in production environments, you may install it as a standard dependency:
Note: In production environments, it is recommended to enable OPcache, JIT, and execute php bin/typephp cache:warm during your deployment pipeline for zero-overhead AST execution.
Configuration
Register the extension in your project's typephp.php configuration file:
Usage Examples
1. Reified Class Generics (Collection<T>)
2. Refined Scalar Constraints (positive-int, non-empty-string)
3. Array Shapes inside Collections
4. Union Generics (Collection<Dog|Cat>)
5. Generic Typed Maps (TypedMap<K, T>)
Enforces both key (K) and value (T) constraints simultaneously:
6. Queues & Double-Ended Queues (Queue<T> & DoubleEndedQueue<T>)
7. Custom Collection Subclassing (@extends AbstractCollection<T>)
When creating your own collection subclasses, TypePHP inherits the generic parameter directly from your class DocBlock:
8. Runtime Generic Introspection & Cloning
Supported Ramsey Collection Classes
| Class | Generic Parameters | Supported Operations |
|---|---|---|
Ramsey\Collection\Collection<T> |
T |
add(), offsetSet(), filter(), first(), last(), where(), merge() |
Ramsey\Collection\Set<T> |
T |
add(), offsetSet(), duplicate rejection with type check |
Ramsey\Collection\Queue<T> |
T |
add(), offer(), element(), peek(), poll(), remove() |
Ramsey\Collection\DoubleEndedQueue<T> |
T |
addFirst(), addLast(), offerFirst(), offerLast(), firstElement(), lastElement() |
Ramsey\Collection\AbstractArray<T> |
T |
Array access, serialization, and iteration |
Ramsey\Collection\Map\TypedMap<K, T> |
K of array-key, T |
put(), putIfAbsent(), replace(), offsetSet() |
Ramsey\Collection\Map\AbstractTypedMap<K, T> |
K of array-key, T |
Subclasses inheriting custom typed maps |
Testing
Run the test suite using Pest:
License
This project is open-source software licensed under the MIT License.