Skip to content

Closure return type lost during analysis? #10254

@devnix

Description

@devnix

Bug report

In the given example, there's this piece of code

$value1 = $value->map(function ($value) {
	PHPStan\dumpType($value[0]);
	PHPStan\dumpType($value[1]);
	return $value[0] + $value[1];
});

PHPStan\dumpType($value1);

that is said to return a sum of two integers, but in the generic the type is inferred as a union:

Option<(array|float|int)>

Code snippet that reproduces the problem

https://phpstan.org/r/e53857ea-5194-433e-ade5-725a7126864b

Expected output

If we manually type the return of the closure

$value2 = $value->map(function ($value): int {
	PHPStan\dumpType($value[0]);
	PHPStan\dumpType($value[1]);
	return $value[0] + $value[1];
});

PHPStan\dumpType($value2);

then the correct generic is inferred:

Option<int>

While this is manageable by typing the return type of the closure, maybe having this bug fixed would help to infer more precise types to a whole codebase

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

I absolutely love PHPStan!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions