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

Parameter #2 $array of function array_map expects array, array given. #5872

Closed
dktapps opened this issue Nov 1, 2021 · 12 comments
Closed

Parameter #2 $array of function array_map expects array, array given. #5872

dktapps opened this issue Nov 1, 2021 · 12 comments
Labels

Comments

@dktapps
Copy link
Contributor

dktapps commented Nov 1, 2021

Bug report

Code snippet that reproduces the problem

https://phpstan.org/r/61bbc322-2593-40a7-861a-b6b64c9e70e4

Expected output

No error should be reported.

Even if this is an error for some reason I overlooked, expects array, array given isn't giving me any idea what the actual problem is.

Did PHPStan help you today? Did it make you happy in any way?

It found a ton of new bugs in my code. Not sure which are real bugs yet though ..

@voku
Copy link
Contributor

voku commented Nov 1, 2021

scalar[] instead of mixed[] fixed that: https://phpstan.org/r/73066fc1-8a77-4dcf-8e26-9ced2ae59b3b

But I don't know if this is the expected behaviour. 🤔

@dktapps
Copy link
Contributor Author

dktapps commented Nov 1, 2021

Well like I said. Whether or not this is a legit issue, it's clearly not being reported in a way that makes sense.

@voku
Copy link
Contributor

voku commented Nov 1, 2021

Well like I said. Whether or not this is a legit issue, it's clearly not being reported in a way that makes sense.

But you can't cast mixed into array so some kind of error should be reported at least with level 9, or? 🤔

@dktapps
Copy link
Contributor Author

dktapps commented Nov 1, 2021

Actually you can. It has defined behaviour in all cases.

object -> array of properties
resource -> array{resource}
array -> itself
scalar -> array{scalar}
NULL -> empty array

@voku
Copy link
Contributor

voku commented Nov 1, 2021

But is that the expected behaviour for that method? Maybe something more strict then "mixed" is a good idea 💡 here?

@dktapps
Copy link
Contributor Author

dktapps commented Nov 1, 2021

The method is beside the point. The problem is caused by something broken in the type casting handling. See similar issues #5881, #5861.

@Anamelash
Copy link

#5834

@kubawerlos
Copy link
Contributor

Another example: https://phpstan.org/r/70dfe8f0-49ca-4c39-8a78-7c0dbf5afb7c

<?php declare(strict_types = 1);

/**
 * @implements \IteratorAggregate<int>
 */
class FooIterator implements \IteratorAggregate
{
    /**
     * @return \Generator<int>
     */
	public function getIterator(): \Generator
	{
		yield 1;
		yield 2;
		yield 3;
	}
}

\array_map(
	static function (int $i): string {
		return (string) $i;
	},
	\iterator_to_array(new FooIterator())
);

@phpstan-bot
Copy link
Contributor

@dktapps After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 (1 error)
-==========
-
-10: Parameter #2 $array of function array_map expects array, array given.
-
-PHP 7.1 – 7.4 (1 error)
-==========
-
-10: Parameter #2 $input1 of function array_map expects array, array given.
+No errors

@phpstan-bot
Copy link
Contributor

@kubawerlos After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 (1 error)
-==========
-
-23: Parameter #2 $array of function array_map expects array, array<int> given.
-
-PHP 7.1 – 7.4 (1 error)
-==========
-
-23: Parameter #2 $input1 of function array_map expects array, array<int> given.
+No errors

@kubawerlos
Copy link
Contributor

Cool, thank you @ondrejmirtes

MidnightDesign pushed a commit to MidnightDesign/phpstan-src that referenced this issue Nov 30, 2021
@github-actions
Copy link

github-actions bot commented Dec 5, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants