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

Invalid array key type for 3-nested for loops #3044

Closed
spl1nes opened this issue Mar 2, 2020 · 3 comments
Closed

Invalid array key type for 3-nested for loops #3044

spl1nes opened this issue Mar 2, 2020 · 3 comments
Labels
Milestone

Comments

@spl1nes
Copy link
Contributor

spl1nes commented Mar 2, 2020

Bug report

When using 3 nested for loops on an array<int, array<int, float>> and assigning them to an array phpstan believes the first index can be int|string.

Method HelloWorld::invalidType() should return array<int, array<int, float>> but returns array<int|string, array<int, float>>. |

Code snippet that reproduces the problem

https://phpstan.org/r/899f83f5-5deb-4335-b8e7-54c4f3315abb

<?php declare(strict_types = 1);

class HelloWorld
{
	/**
	 * @return array<int, array<int, float>>
	 */
	private function getIntArrayFloatArray() : array
	{
		return [
			0 => [1.1, 2.2, 3.3],
			1 => [1.1, 2.2, 3.3],
			2 => [1.1, 2.2, 3.3],
		];
	}
	
	/**
	 * @return array<int, array<int, float>>
	 */
	public function invalidType() : array
	{
		$X = $this->getIntArrayFloatArray();
		$L = $this->getIntArrayFloatArray();
		
                $n = 3;
		$m = 3;

                for ($k = 0; $k < $m; ++$k) {
                    for ($j = 0; $j < $n; ++$j) {
                        for ($i = 0; $i < $k; ++$i) {
                            $X[$k][$j] -= $X[$i][$j] * $L[$k][$i];
                        }
                    }
                }

                return $X;
	}
}

Expected output

No error.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Nov 28, 2020
@phpstan-bot
Copy link
Contributor

@spl1nes PHPStan now reports different result with your code snippet:

@@ @@
-36: Method HelloWorld::invalidType() should return array<int, array<int, float>> but returns array<int|string, array<int, float>>.
+PHP 8.0 (2 errors)
+==========
+
+ 8: Method HelloWorld::getIntArrayFloatArray() return type has no value type specified in iterable type array.
+20: Method HelloWorld::invalidType() return type has no value type specified in iterable type array.
+
+PHP 7.4 (1 error)
+==========
+
+36: Method HelloWorld::invalidType() should return array<int, array<int, float>> but returns array<int|string, array<int, float>>.
+
+PHP 7.1 – 7.3 (2 errors)
+==========
+
+ 8: Method HelloWorld::getIntArrayFloatArray() return type has no value type specified in iterable type array.
+20: Method HelloWorld::invalidType() return type has no value type specified in iterable type array.
Full report

PHP 8.0 (2 errors)

Line Error
8 Method HelloWorld::invalidType() return type has no value type specified in iterable type array.
20 Method HelloWorld::invalidType() return type has no value type specified in iterable type array.

PHP 7.4 (1 error)

Line Error
36 Method HelloWorld::invalidType() return type has no value type specified in iterable type array.

PHP 7.1 – 7.3 (2 errors)

Line Error
8 Method HelloWorld::invalidType() should return array<int, array<int, float>> but returns array<int
20 Method HelloWorld::invalidType() should return array<int, array<int, float>> but returns array<int

@phpstan-bot
Copy link
Contributor

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

@@ @@
-36: Method HelloWorld::invalidType() should return array<int, array<int, float>> but returns array<int|string, array<int, float>>.
+No errors

@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 13, 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