lcobucci/clock
PSR-20 clock implementation for PHP. Provides a simple, testable way to access the current time with interchangeable clocks like system and frozen clocks, making time-dependent code predictable and easy to unit test.
Architecture fit: Laravel's dependency injection container aligns perfectly with the Clock abstraction. The interface-based design allows seamless binding of SystemClock in production and FixedClock in tests via service providers, eliminating direct DateTimeImmutable usage in domain logic. This promotes clean architecture and testability while respecting Laravel's existing DI patterns.
Integration feasibility: High. The package is PSR-compliant and framework-agnostic, requiring only minimal Laravel-specific setup (e.g., binding in AppServiceProvider). Existing Laravel time helpers like now() can be wrapped to use the Clock instance without major refactoring. Composer integration is trivial.
Technical risk: Low. Minimal dependencies, small codebase (<100 LOC), and active maintenance (despite the 2025 release date anomaly). Primary risks include timezone handling inconsistencies (the Clock interface doesn't enforce timezones) and legacy code refactoring complexity where DateTimeImmutable is deeply embedded.
Key questions:
DateTimeImmutable directly without breaking existing functionality?travelTo), or require custom implementations?Stack fit: Ideal for Laravel's DI-driven architecture. The Clock interface can be bound to the container with minimal code (e.g., bind(Clock::class, fn() => new SystemClock())), and Laravel's now() helper can be overridden to use the injected Clock instance. Carbon compatibility is maintained by constructing Carbon objects from Clock's DateTimeImmutable outputs.
Migration path:
Clock to SystemClock in production and FixedClock in tests. Wrap Laravel's now() helper to use the Clock instance.Clock via constructor/method injection.DateTimeImmutable in legacy code (prioritizing time-sensitive logic like scheduling, expiration checks, and analytics).time(), strtotime(), and Carbon directHow can I help you explore Laravel packages today?