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

Add array_udiff to arrayFunctions.stub #1407

Merged
merged 2 commits into from Jun 9, 2022

Conversation

akalineskou
Copy link
Contributor

@akalineskou akalineskou commented Jun 8, 2022

@ondrejmirtes
Copy link
Member

Please add a regression test in CallToFunctionParametersRuleTest with a code sample from phpstan/phpstan#6105 (and maybe add another call that shows that wrong closure will be reported). Thanks!

@akalineskou
Copy link
Contributor Author

akalineskou commented Jun 9, 2022

@ondrejmirtes Before I push the test and run the whole suite again, would something like this work?
This https://phpstan.org/r/9509075f-ec61-4eba-941d-a8835055f0d1 is not returning an error (so I'm guessing that's correct?)
It passes.

array_udiff.php (the line comments are to show the line number, wont be in the file)

<?php declare(strict_types = 1);

array_udiff(
	[1,2,3],
	[4,5,6],
	function(string $a, string $b): string { // line 6
		return $a . $b;
	},
);

array_udiff(
	[1,2,3],
	[4,5,6],
	function(int $a, int $b): string { // line 14
		return $a . $b;
	},
);

array_udiff(
	null, // line 20
	null, // line 21
	function(string $a, int $b): string { // line 22
		return $a . $b;
	},
);

array_udiff(
	[25,26],
	[26,27],
	static function(int $a, int $b): int {
		return $a <=> $b;
	},
);
public function testArrayUdiffCallback(): void
{
	$this->analyse([__DIR__ . '/data/array_udiff.php'], [
		[
			'Parameter #3 $data_comp_func of function array_udiff expects callable(int, int): int<-1, 1>, Closure(string, string): string given.',
			6,
		],
		[
			'Parameter #3 $data_comp_func of function array_udiff expects callable(int, int): int<-1, 1>, Closure(int, int): non-empty-string given.',
			14,
		],
		[
			'Parameter #1 $arr1 of function array_udiff expects array<string>, null given.',
			20,
		],
		[
			'Parameter #2 $arr2 of function array_udiff expects array<string>, null given.',
			21,
		],
		[
			'Parameter #3 $data_comp_func of function array_udiff expects callable(string, string): int<-1, 1>, Closure(string, int): non-empty-string given.',
			22,
		],
	]);
}

@ondrejmirtes
Copy link
Member

Just push it and I'll see if it makes sense.

@ondrejmirtes
Copy link
Member

Thank you!

@akalineskou akalineskou deleted the patch-1 branch June 9, 2022 11:52
@bytestream
Copy link
Contributor

@ondrejmirtes
Copy link
Member

@bytestream These compare functions should all be updated to note they also return int<-1, 1>.

@nkl-kst
Copy link

nkl-kst commented Jan 2, 2023

I'm not sure if this instead belongs to a new issue, but shouldn't the callback of an array_udiff be able to return any int?

From https://www.php.net/manual/en/function.array-udiff.php:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

There is nothing that restricts the callback to return int<-1, 1>.

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