PaginatorContract should implement Countable and other shared interfaces #57005
-
Hello Laravel Team, I've noticed a small discrepancy between the The ProblemWhen using the simplePaginate method, the documented return type is
The code runs without any errors because the concrete class returned, The same issue applies to other interfaces implemented by the Paginator class, such as Arrayable, ArrayAccess, IteratorAggregate, Jsonable, and JsonSerializable. Proposed SolutionTo improve type safety and the overall developer experience, I suggest that the contract should reflect the full public API of its primary implementation. The PaginatorContract could be updated to extend all the interfaces that the concrete Paginator class implements. Current PaginatorContract:
Current Paginator class:
Suggested ChangeThe interfaces should be moved from the concrete class to the contract. New PaginatorContract:
New Paginator class:
This change would make the contract a complete representation of the Paginator's capabilities, immediately fixing the type hinting issues without introducing any breaking changes to the method signatures themselves. Thank you for your consideration and for building such a great framework! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
This is doable only if the interface is not used by other classes from laravel or 3rd party libs. |
Beta Was this translation helpful? Give feedback.
TBH, there are a lot of times where typehinting against a contract gives you an incomplete API because the reality is things in Laravel are designed against the concrete implementations. The one that always irritates me is how the container (and by extension, the application) contract doesn't implement
ArrayAccess
, yet it is not uncommon to see$this->app['whatever']
or$app['whatever']
in code interacting with the container or application (scan your vendor folder for Laravel service providers and see how often it comes up, both in Laravel itself and any packages you use). These types of hidden dependencies really make working solely with Laravel's interfaces annoying.Just make the B/C b…