Skip to content

Looping through functions doesn't detect correct types #9594

@chescos

Description

@chescos

Bug report

Given the following code:

<?php declare(strict_types = 1);

class HelloWorld
{
	public function sayHello(): void
	{
		$data = [
                    [
                        'elements' => [1, 2, 3],
                        'greet' => fn (int $value) => 'I am '.$value,
                    ],
                    [
                        'elements' => ['hello', 'world'],
                        'greet' => fn (string $value) => 'I am '.$value,
                    ],
                ];
        
                foreach ($data as $entry) {
                    foreach ($entry['elements'] as $element) {
                        $entry['greet']($element);
                    }
                }
	}
}

Shouldn't PHPStan detect that the data types of the array elements match the function parameter?

Code snippet that reproduces the problem

https://phpstan.org/r/dfcecd96-e249-4263-8dba-772224daf7fc

Expected output

The issue was reported incorrectly.

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions