So if I have a function and in the PHPDoc I specify it'll return a SqsProcessResult
object, then I call a function to create a new one of those objects but that function's PHPDoc say return type of Object
(as it's a generic factory function) PhpStorm throws up an orange warning.
Can I annotate or otherwise tell PhpStorm the return type will be SqsProcessResult
?
Yes, you can .. but due to the nature of the issue it may not be cleanest/elegant solution (at least that's what others may say).
Few options:
#1. Just suppress this inspection for that line.
Suppress for statement
option -- a new inline PHPDoc comment will be added just above tat line that instructs PhpStorm to ignore that specific Inspection in the next line.Will be something like this:
/** @noinspection PhpIncompatibleReturnTypeInspection */
return \Yii::createObject(...);
#2. Use some intermediate variable that you can type hint in place.
Add something like this instead of that existing single line:
/** @var SqsProcessResult $res */
$res = \Yii::createObject(...);
return $res;
This one mainly can be used in longer methods where such variable (generic Object
) will be created in the beginning/middle of the function body and then used later.
#3. Play with .metadata functionality and provide resolution logic for actual \Yii::createObject()
(so IDE chooses the right class based on input parameter).
https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
This is how quite a few tools working: IDE helper for Laravel, Symfony helpers, DI Container helpers etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With