How to identify the type of a variable? #7813
-
QuestionIs there a way to try to identify what is the expected type of a variable? Imagine I have
I would like to check if $may_be_array has been defined previously in the file and eventually check also if the var is an array or not. If this is possible, I can prepend a force init of array as empty array only when needed. this because in our code base we have > 300 situations where we have error reported about the second variable being not an array |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can use: $scope->getType($expr); see example use case on https://github.com/rectorphp/rector-src/blob/2ae2430ceb73c3533dded760b0ddda9e5d68c61e/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php#L117 |
Beta Was this translation helpful? Give feedback.
You can use:
see example use case on https://github.com/rectorphp/rector-src/blob/2ae2430ceb73c3533dded760b0ddda9e5d68c61e/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php#L117