php-standard-library/vec
php-standard-library/vec provides small, focused helpers for working with sequential 0-indexed arrays (lists). Create, map, filter, transform, and compose list operations with predictable behavior and clean APIs—part of the PHP Standard Library collection.
Collection facade compatibility). Could complement existing Illuminate\Support\Collection for domain-specific use cases.Collection for critical paths.Vec::fromArray()). Could conflict with Laravel’s Collection if not scoped carefully.Vec<T> generics, but runtime type enforcement is manual (no PHPDoc/attribute integration).map, filter) may shadow Laravel’s Collection methods, risking confusion. Requires clear naming conventions (e.g., Vec::map() vs. Collection::map()).Vec solve problems Laravel’s Collection or native arrays cannot? (e.g., immutability, strict typing, or DSL-like syntax).Array.prototype methods)? If not, ramp-up time may be high.Collection for target operations (e.g., reduce, chunk)?Vec instances be tested? (e.g., mocking, property-based testing with pestphp?)Collection mutability is undesirable (e.g., caching intermediate results).Vec::fromArray($users)->map(fn($u) => $u->name)->filter(...)).Collection suffices (e.g., Eloquent query results).Vec for critical paths (e.g., Vec::fromArray($request->input('items'))).Vec alongside Collection where needed (e.g., Vec::fromCollection($collection)).app(VecHelper::class)) to standardize Vec usage and handle edge cases (e.g., array-to-Vec conversion).class VecHelper {
public static function fromInput(array $input, string $key): Vec { ... }
}
array_map/array_filter with Vec::map()/Vec::filter() in new code.Vec for return types in domain objects (e.g., public function getItems(): Vec).$vec = Vec::fromArray($collection->toArray());
$collection = Collection::make($vec->toArray());
array_* functions via Vec::toArray(), but loses type safety.assertVecEquals()).php-standard-library/vec to composer.json.map, filter).Vec usage guidelines (e.g., "Use Vec for X, not Y").Vec.Vec patterns (e.g., chaining, immutability).composer.json until adoption stabilizes (e.g., ^1.0).Vec::get(99) throws vs. array[99] returning null)./** @var Vec<int, User> */
$users = Vec::fromArray($rawUsers);
Collection for memory/CPU usage. Likely comparable for small-to-medium datasets.Vec for very large datasets (>100K items) unless profiling confirms benefits.| Risk | Mitigation | Detection |
|---|---|---|
| API Confusion | Enforce naming conventions (e.g., Vec::map vs. Collection::map). |
Static analysis (PHPStan/Psalm). |
| Runtime Errors | Validate inputs early (e.g., Vec::ensureIndexed()). |
Unit tests with edge cases. |
| Adoption Resistance | Start with opt-in usage in new features. | Team feedback loops. |
| Package Abandonment | Fork or vendor the package if needed. | Monitor GitHub activity. |
Vec vs. arrays/collections, with live coding examples.Vec improves readability/maintainability (e.g., "This Vec::pipe() chain is easier to debug than nested array_map calls").Vec cheat sheet for common operations (e.g., "Use Vec::take(5) instead of array_slice($array, 0, 5)").Vec in onboarding tasks (e.g., "Refactor this array-heavy service to use Vec").How can I help you explore Laravel packages today?