Stringable "when..." Return Types #56846
Unanswered
gavg-vaioni
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Illuminate\Support\Stringable contains several "when..." functions (whenContains, whenContainsAll, whenEmpty, whenNotEmpty etc).
These each use the "when" function in the Illuminate\Support\Traits\Conditionable trait.
However, the docblock return types of the Stringable functions don't match the docblock return types of the Conditionable when function.
The "when" function declares its return type as "@return $this|TWhenReturnType" (via @template TWhenReturnType), whereas the Stringable functions declare "@return static"
Conditionable->when
Stringable->whenEmpty
This is a tad annoying in IDEs which use these type declarations as they flag, but it is also misleading as it could be interpreted to mean the stringable "when..." function always returns the Stringable instance rather than the result of the callback.
In other places in Laravel, such as Illuminate\Support\Traits\EnumeratesValues (as used by Illuminate\Support\Arr) "when..." functions, such as "whenEmpty", also return the result of the Conditionable "when" function, but their docblocks match its return types.
EnumeratesValues->whenEmpty
Does anyone know why the Stringable class's "when..." functions might be written differently? Is there some non-obvious justification? Or is this likely a copy-paste mistake when the docblocks were written?
TIA
Beta Was this translation helpful? Give feedback.
All reactions