Time Is a Dependency: Injecting a Clock Into Your PHP Domain
📰 Dev.to · Gabriel Anhaia
Learn to inject a clock into your PHP domain to make time testable and avoid hidden dependencies
Action Steps
- Identify hidden dependencies in your PHP code
- Create a Clock port to define the interface for time-related operations
- Implement a SystemClock adapter to provide the current time
- Use a FrozenClock to freeze time for testing purposes
- Inject the Clock into your domain to make time testable
Who Needs to Know This
Developers and software engineers can benefit from this approach to improve the testability and maintainability of their PHP applications
Key Insight
💡 Time can be a hidden dependency in your code, but injecting a Clock makes it testable and more maintainable
Share This
💡 Make time testable in your PHP domain by injecting a Clock!
Key Takeaways
Learn to inject a clock into your PHP domain to make time testable and avoid hidden dependencies
Full Article
new DateTimeImmutable() in your domain is a hidden dependency. A Clock port, a SystemClock adapter, and a FrozenClock make time testable.
DeepCamp AI