Skip to content

Conversation

@HellPat
Copy link

@HellPat HellPat commented Apr 23, 2020

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 extract to 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

Copy link
Author

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]] ) : int
Copy link
Contributor

@adaamz adaamz Apr 23, 2020

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

Copy link
Author

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?

@HellPat HellPat requested a review from ondrejmirtes April 27, 2020 14:43
Copy link
Member

@ondrejmirtes ondrejmirtes left a 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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Author

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.)

Copy link
Member

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.

@ondrejmirtes ondrejmirtes force-pushed the master branch 2 times, most recently from f54fd5f to eca550a Compare October 28, 2020 19:41
@ondrejmirtes ondrejmirtes force-pushed the master branch 3 times, most recently from d45166a to 3526237 Compare December 12, 2020 10:56
@ondrejmirtes
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants