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

Looping through an array shape has wrong count #3339

Closed
allejo opened this issue May 26, 2020 · 9 comments
Closed

Looping through an array shape has wrong count #3339

allejo opened this issue May 26, 2020 · 9 comments
Labels
Milestone

Comments

@allejo
Copy link
Contributor

allejo commented May 26, 2020

Bug report

Given I'm using an array shape like array{bool, bool, bool}, it expects a key of int<min, 2>, where I would think it should be int<min, 3> or make it inclusive?

Code snippet that reproduces the problem

https://phpstan.org/r/eaaf2943-60db-49fa-9336-a2dead66c788

<?php declare(strict_types = 1);

class HelloWorld
{
	/** @var array{bool, bool, bool} */
	private $tuple;
	
	public function main(): void
	{
		$this->tuple = [true, true, true];
		
		for ($i = 0; $i < 3; ++$i)
		{
		    $this->tuple[$i] = false;
		}
	}
}

Expected output

I would think PHPStan should accept this code as valid.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone May 26, 2020
GKFX added a commit to GKFX/phpstan-src that referenced this issue Jan 3, 2021
GKFX added a commit to GKFX/phpstan-src that referenced this issue Jan 3, 2021
GKFX added a commit to GKFX/phpstan-src that referenced this issue Jan 25, 2021
@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<int, bool>) does not accept key int<min, 2>.
+14: Array (array(bool, bool, bool)) does not accept key int<min, 2>.
Full report
Line Error
14 Array (array(bool, bool, bool)) does not accept key int<min, 2>.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<int, bool>) does not accept key int<min, 2>.
+14: Array (array{bool, bool, bool}) does not accept key int<min, 2>.
Full report
Line Error
14 Array (array{bool, bool, bool}) does not accept key int<min, 2>.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<int, bool>) does not accept key int<min, 2>.
+ 6: Property HelloWorld::$tuple is never read, only written.
+14: Array (array{bool, bool, bool}) does not accept key int<min, 2>.
Full report
Line Error
6 Property HelloWorld::$tuple is never read, only written.
14 Array (array{bool, bool, bool}) does not accept key int<min, 2>.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<int, bool>) does not accept key int<min, 2>.
+ 6: Property HelloWorld::$tuple is never read, only written.
+14: Property HelloWorld::$tuple (array{bool, bool, bool}) does not accept non-empty-array<int<min, 2>, false>.
Full report
Line Error
6 Property HelloWorld::$tuple is never read, only written.
14 Property HelloWorld::$tuple (array{bool, bool, bool}) does not accept non-empty-array<int<min, 2>, false>.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<int, bool>) does not accept key int<min, 2>.
+ 6: Property HelloWorld::$tuple is never read, only written.
+14: Property HelloWorld::$tuple (array{bool, bool, bool}) does not accept non-empty-array<int<0, 2>, false>.
Full report
Line Error
6 Property HelloWorld::$tuple is never read, only written.
14 Property HelloWorld::$tuple (array{bool, bool, bool}) does not accept non-empty-array<int<0, 2>, false>.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<int, bool>) does not accept key int<min, 2>.
+ 6: Property HelloWorld::$tuple is never read, only written.
Full report
Line Error
6 Property HelloWorld::$tuple is never read, only written.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@3b5fd94

@allejo
Copy link
Contributor Author

allejo commented Jan 13, 2022

Thank you for the fix! 🥳

@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 Feb 14, 2022
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