Type hinting challenges with callable arguments #10706
Replies: 3 comments
-
One of the issues here is that (To confirm this hypothesis, I've broken your code completely by replacing setter calls with getter calls, and there are no PHPStan errors: https://phpstan.org/r/85f11044-66d8-4c86-83bc-e4a4186f25de) Your second example suffers from the same problem only because PHPStan resolves the union type to one callable with I would refactor the code to call |
Beta Was this translation helpful? Give feedback.
-
I agree with the refactoring. It could work this way https://phpstan.org/r/cad752d6-d0e9-41ca-9b47-31c0ad319a1d |
Beta Was this translation helpful? Give feedback.
-
Thanks, @lkrms and @VincentLanglet! I appreciate the explanations on template types. While the variadic approach was interesting, refactoring seems like the best solution here. 👍 |
Beta Was this translation helpful? Give feedback.
-
I'm experimenting with the callable function types in PHPStan and I'm getting a warning I'm not sure how to address.
My code example you could find here: https://phpstan.org/r/4c21a771-5f58-4cfb-a4b6-67515fc4f776
The setIfNotNulls method takes a variable number of argument pairs. Each pair represents a getter and setter method from a Person object. The goal is to copy properties from one Person object to another, but only if the getter on the source object returns a non-null value.
In the setIfNotNulls method of my Cloner class, I'm getting the following warning:
I alternatively tried the following approach: https://phpstan.org/r/bc5cb91e-06f9-4487-ad62-09661f3621ba
But then I run into the following warning:
Is there a way to document the $pairs parameter in setIfNotNulls so that the calls to the method don't raise this warning?
Thanks in advance for any help!
Beta Was this translation helpful? Give feedback.
All reactions