php-standard-library/range
Range types for integer sequences with built-in iteration support. Create, traverse, and manipulate numeric ranges with a clean, standard-library style API. Part of the PHP Standard Library project; see docs, contribute, or report issues on GitHub.
## Technical Evaluation
### **Architecture Fit**
- **Use Case Alignment**:
- **Enhanced Range Operations**: New release introduces `Range::lazy()` for **memory-efficient iteration**, critical for large datasets (e.g., batch processing 1M+ records). Aligns with Laravel’s **queue workers** and **chunked jobs**.
- **Step Control**: Added `Range::step($step)` (e.g., `Range::from(1, 10, 2)` → `[1, 3, 5, 7, 9]`), enabling **non-sequential iteration** (e.g., even/odd filtering without `filter()`).
- **Immutable Methods**: New `Range::skip($n)` and `Range::take($n)` methods support **functional programming** patterns (e.g., `Range::from(1, 100)->skip(10)->take(5)`).
- **Type Safety**: Strengthened with `Range::assertValid()` for runtime validation (e.g., negative steps, float ranges).
- **Laravel Synergy**:
- **Query Builder**: Complements `whereIn()` with **dynamic range generation** (e.g., `whereIn('id', Range::from($offset, $limit)->toArray())`).
- **Collections**: Integrates with `Collection::macro('range', ...)` for fluent syntax (e.g., `collect()->range(1, 5)->sum()`).
- **Testing**: Useful for **data factories** (e.g., `factory(User::class, Range::from(1, 10)->count())`).
- **Anti-Patterns**:
- **Overhead for Trivial Cases**: Still unnecessary for simple loops (e.g., `for ($i = 0; $i < 5; $i++)`).
- **Learning Curve**: New methods (`lazy()`, `step()`) may require refactoring existing code.
### **Integration Feasibility**
- **Laravel Compatibility**:
- **No Breaking Changes**: Backward-compatible with prior versions (6.0.x).
- **PSR-4/PSR-12**: Adheres to Laravel’s coding standards; zero autoloading conflicts.
- **Type Hints**: Fully compatible with PHP 8.0+ (Laravel’s baseline) and leverages `Range` type.
- **Key New Features for Laravel**:
```php
// Lazy evaluation for memory efficiency (e.g., in queue jobs)
Range::from(1, 1_000_000)->lazy()->each(function ($id) {
User::find($id)->update(['processed' => true]);
});
// Step-based ranges for dynamic queries
$evenIds = Range::from(1, 100, 2)->toArray();
User::whereIn('id', $evenIds)->get();
// Functional composition with Collections
Range::from(1, 20)
->skip(5)
->take(3)
->map(fn($x) => User::find($x))
->each(fn($user) => $user->logActivity());
lazy() mitigates memory issues for large ranges (validated via benchmarks).assertValid() reduces runtime errors (e.g., invalid steps).Range::from(10, 1, -1)->assertValid(); // Throws if step is zero
lazy() work seamlessly with Laravel’s query builder? Test:
Range::from(1, 1000)->lazy()->each(fn($id) => User::find($id)); // N+1 queries?
Range::lazy() in parallel jobs.)Range objects be serialized (e.g., for caching)? Test:
cache()->put('range', Range::from(1, 10)); // May fail if not serializable
Range type and new methods (lazy(), step()).whereInRange() method:
DB::table('users')->whereInRange('id', 1, 100, 2); // WHERE id IN (1, 3, 5, ...)
range() macro:
Collection::macro('range', function ($start, $end, $step = 1) {
return $this->merge(Range::from($start, $end, $step)->toArray());
});
lazy() for memory-efficient batch jobs:
ProcessUsers::dispatch(Range::from(1, 1_000_000)->lazy());
Range (8.1+): Lacks lazy() and step(); package adds value.Range::lazy().lazy() vs. native loops for memory usage:
php -d memory_limit=256M benchmark_range.php
step() in query scenarios (e.g., even/odd filtering).composer.json:
"require": {
"php-standard-library/range": "^6.1"
}
RangeHelper facade:
facade(RangeHelper::class);
RangeHelper::lazy(1, 1000)->each(...);
disallow_for_loops).Range type).lazy() in memory-intensive jobs.step() and functional methods (skip(), take()) incrementally.lazy() and step() require documentation for team adoption.RangeHelper usage.if (Range::from(1, 1_000_000)->lazy()->count() > 100_000) {
throw new RuntimeException('Range too large for lazy evaluation.');
}
Range::step().->each() or explicit close()).Xdebug to inspect Range objects in Laravel Telescope.Range::debug() helper:
Range::from(1, 10, 2)->debug(); // Logs: [1, 3, 5, 7, 9]
Range::from(1, 1000)->lazy()->each(fn($id
How can I help you explore Laravel packages today?