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

Missed change of variable type in foreach #4907

Closed
integer opened this issue Apr 27, 2021 · 12 comments
Closed

Missed change of variable type in foreach #4907

integer opened this issue Apr 27, 2021 · 12 comments
Labels
Milestone

Comments

@integer
Copy link
Contributor

integer commented Apr 27, 2021

Bug report

You iterate an array and overrides variable outside cycle. PHPStan not update variable type and expecting different type than real PHP runtime. Code falls with TypeError

Code snippet that reproduces the problem

function sayHello(): void
{
	$foo = [5,6,7];
	foreach ($foo as $i => $foo) {
		// ...			
	}
	// here is $foo int not array.
	bar($foo);  // TypeError: Argument 1 passed to bar() must be of the type array, integer given
}
	
/**
 * @param int[] $x
 * @return int[]
 */
function bar(array $x): array {
	return $x;
}
	
sayHello();

Same code here: https://phpstan.org/r/9743d6e1-0f56-4748-a50d-a61a0d79e6c2

Expected output

Parameter #1 $x of function bar expects array, int given.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Apr 27, 2021
@ondrejmirtes
Copy link
Member

Just use a different variable name for the value 🤣

@integer
Copy link
Contributor Author

integer commented Apr 27, 2021

Yes, this is easy fix in code. :) But when reviewer and test fails and miss this issue it will cause TypeError.

Maybe it could be check with some codestyle check not only with PHPStan.. 🤔

@ondrejmirtes
Copy link
Member

Yes, with strict-rules this is checked 😊 https://phpstan.org/r/04b78bc7-dd20-4a1d-8cc4-47e4cffbc530

@integer
Copy link
Contributor Author

integer commented Apr 27, 2021

Thanks, strict-rules are good solution. 👍 This is not a bug for me. Let's close this issue? Or are you interested in adding this check into some PHPStan level?

@ondrejmirtes
Copy link
Member

PHPStan should correctly know what's there: https://phpstan.org/r/79e304ca-c2f1-4b07-bc79-f4d724f5ed59

Right now it fails for some reason.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-No errors
+17: Function bar() should return array<int> but return statement is missing.
Full report
Line Error
17 Function bar() should return array<int> but return statement is missing.

@phpstan-bot
Copy link
Contributor

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

@@ @@
- 6: Foreach overwrites $foo with its value variable.
+17: Function bar() should return array<int> but return statement is missing.
Full report
Line Error
17 Function bar() should return array<int> but return statement is missing.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-10: Dumped type: *ERROR*
+No errors

@phpstan-bot
Copy link
Contributor

@integer After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+10: Parameter #1 $x of function bar expects array<int>, int given.
Full report
Line Error
10 Parameter #1 $x of function bar expects array<int>, int given.

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
- 6: Foreach overwrites $foo with its value variable.
+ 6: Foreach overwrites $foo with its value variable.
+10: Parameter #1 $x of function bar expects array<int>, int given.
Full report
Line Error
6 Foreach overwrites $foo with its value variable.
10 Parameter #1 $x of function bar expects array<int>, int given.

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-10: Dumped type: *ERROR*
+10: Dumped type: 5|6|7
Full report
Line Error
10 `Dumped type: 5

@github-actions
Copy link

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 Jul 31, 2023
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

3 participants