- Notifications
You must be signed in to change notification settings - Fork 545
Helps to understand extract function #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/Analyser/NodeScopeResolver.php Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2 more possible Parameters here. I'd like to Support only the default parameters,
but had problems here.
I get ConstantType as second param, but one could also drop the corresponding int value in.
How do I check for that?
extract(array &$array [,int $flags=EXTR_OVERWRITE [, string $prefix=NULL]] ) : intThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$flags = $scope->getType($expr->args[1]->value);but you need to check if there is some argument, for example via count($expr->args) >= 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be solved now.
I did a quick research, how often the flags-Attibutes are beeing used.
| supported | const | usages | url |
|---|---|---|---|
| [x] | EXTR_OVERWRITE | ∞ | its the default |
| [ ] | EXTR_SKIP | 675,812 | https://github.com/search?l=PHP&q=EXTR_SKIP&type=Code |
| [ ] | EXTR_PREFIX_ALL | 34,886 | https://github.com/search?l=PHP&q=EXTR_PREFIX_ALL&type=Code |
| [ ] | EXTR_PREFIX_INVALID | 3,321 | https://github.com/search?l=PHP&q=EXTR_PREFIX_INVALID&type=Code |
| [ ] | EXTR_IF_EXISTS | 21,810 | https://github.com/search?l=PHP&q=EXTR_IF_EXISTS&type=Code |
| [ ] | EXTR_PREFIX_IF_EXISTS | mostly tests | ... |
| [ ] | EXTR_REFS | mostly tests | https://github.com/search?l=PHP&q=EXTR_REFS&type=Code |
Since it's possible to combine the constants, many possible use cases exist.
For my case, only EXP_OVERWRITE ist relevant, so I implemented only this case.
If you want, I could provide support for EXPR_SKIP and EXTR_PREFIX_ALL, too, since they seem to be widely used (but could also go into an other PR).
I just don't want to support combinations like EXTR_OVERWRITE | EXTR_REFS.
WDYT?
ondrejmirtes left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a bit more work, but we're on a good path :) Thank you.
src/Analyser/NodeScopeResolver.php Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're accessing args[0] before checking that the argument was passed to the function call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that analyzing code with extract() will fail with an internal error. Which shouldn't be the case.
src/Analyser/NodeScopeResolver.php Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like this usage of array_map (with void) - array map is supposed to transform the array. I'd much rather like a foreach here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should at least differentiate between: missing second arg (same as OVERWRITE), having OVERWRITE there, having SKIP there.
Currently, you only support OVERWRITE. Supporting SKIP and the PREFIX variants should be there too. If you ask with bitwise mechanics, it's really easy. ($flags->getValue() & EXTR_SKIP === EXTR_SKIP). It's just a question of a few if branches.
I'm pretty sure your current code will crash when the function is called without second argument (extract($vars)) - this combination isn't tested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback and your patience :-)
I'll need some time to get this right, but I'm on it.
(I don't want to waste your time, and I'll write many more tests to do so.)
src/Analyser/NodeScopeResolver.php Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a better idea to use the actual constant instead of a hardcoded integer. The constant value might change between PHP versions.
f54fd5f to eca550a Compare d45166a to 3526237 Compare | Hi, thanks for the effort, but I have to close this abandoned PR. Feel free to open a new one if you decide to resume work on this. |
Hey there, I love PHPStan and want to try my first contribution.
I'd love to gather feedback and suggestions for improvement.
The Legacy-Project I'm working on uses
extractto pass variables to the View:This maybe helps with Yii , also see phpstan/phpstan#351
or in older FuelPHP-Projects
I followed your hint at this ticket: phpstan/phpstan#2047