Skip to content

Add array_udiff to arrayFunctions.stub#1407

Merged
ondrejmirtes merged 2 commits intophpstan:1.7.xfrom
akalineskou:patch-1
Jun 9, 2022
Merged

Add array_udiff to arrayFunctions.stub#1407
ondrejmirtes merged 2 commits intophpstan:1.7.xfrom
akalineskou:patch-1

Conversation

@akalineskou
Copy link
Copy Markdown
Contributor

@akalineskou akalineskou commented Jun 8, 2022

@ondrejmirtes
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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

@ondrejmirtes ondrejmirtes merged commit 98a3b07 into phpstan:1.7.x Jun 9, 2022
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you!

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

@ondrejmirtes
Copy link
Copy Markdown
Member

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

@nkl-kst
Copy link
Copy Markdown

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

Development

Successfully merging this pull request may close these issues.

array_udiff callback parameter types are not supported

4 participants