jbboehr / phpstan-laravel-validation

Experimental phpstan plugin for Laravel Validation
GNU Affero General Public License v3.0
4 stars 1 forks source link

Narrow types are not accepted #9

Closed Daanra closed 1 year ago

Daanra commented 1 year ago

Ran into a weird issue using this plugin. Not entirely sure whether it is actually caused by this library. When I attempt to run collect() on the value that resides in the request, I get an error that the array type is not accepted which makes zero sense to me.

$x = $request->validate(['values.*' => 'required|integer'])['values'];
dumpType($x); // array<int|string, int|numeric-string> 
$t = collect($x); // Parameter #1 $value of function collect expects Illuminate\Contracts\Support\Arrayable<int|string, int|string>|iterable<int|string, int|string>|null, array<int|string, int|numeric-string> given.  

The weird thing is, the inferred type should be accepted. When I instantiate a new variable and explicitly give it this type, PHPStan returns no errors:

/** @var array<int|string, int|numeric-string> $val */
$val = [1, '1'];
dumpType($val); // array<int|string, int|numeric-string>
collect($val); // No errors

I have no clue why the former code fragment displays and error but the latter does not. I figured there might be a small bug in the type inference.

Using PHPStan level 9

jbboehr commented 1 year ago

That was a bit of a weird one. I guess numeric-string is represented internally as essentially string & numeric-string. Thanks for reporting it!