Skip to content

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

@spl1nes

Description

@spl1nes

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions