Skip to content
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

Fix analysis on array_map with named arguments #3763

Open
wants to merge 4 commits into
base: 2.1.x
Choose a base branch
from

Conversation

takaram
Copy link
Contributor

@takaram takaram commented Jan 1, 2025

Comment on lines 19 to 22
$b = array_map(
array: $a,
callback: static fn(Uuid $c): string => (string) $c,
);
Copy link
Contributor

@staabm staabm Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add another test like:

$b = array_map(
			callback: static fn(Uuid $c): string => (string) $c,
			array: $a,
		);

(parameters in different order, which is allowed for named arguments).


I think there is a another case with more then 2 args which needs testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some more test cases.

@staabm
Copy link
Contributor

staabm commented Jan 1, 2025

it might make sense to re-use ArgumentsNormalizer for this fix

@ondrejmirtes
Copy link
Member

I wouldn't probably do that, we could spin in an infinite recursion maybe.

@takaram
Copy link
Contributor Author

takaram commented Jan 2, 2025

I'm not sure why integration test fails... make tests-integration succeeds on my machine.

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.

What we typically do to normalize args like this is to:

  1. Add $callback = null; $array = null;.
  2. Iterate over $args and assign these variables either based on $i counter in the foreach, or by arg name.
  3. Recreate $args by doing [$callback, $array] in case both of them were found.

Thanks.

if ($args[1]->name !== null && $args[1]->name->name === 'callback') {
$callbackPos = 1;
}
[$callback] = array_splice($args, $callbackPos, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what's going on at this line. Please make the code easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some refactors.


$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-12317.php'], []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be a good idea to add an example that actually produces an error, to verify the args still look like they should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion. I added examples and fixed a bug caught by them.

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.

I'm sorry but it seems like you haven't followed my comment (#3763 (review)) at all. There's no foreach in the diff so it's wrong. Code using array_slice/splice is very hard to read and I don't know if it's correct.

Also I'd like the array from the visitor to always look the same way (callback first, array 2nd), so that no other code has to be adjusted for named arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parameter $callback of function array_map expects (callable(mixed): mixed))
3 participants