phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

Lost narrowed type from PHPDoc or Psl\Type

Open p4veI opened this issue 1 year ago • 3 comments
trafficstars

Bug report

Hello, I've come across this issue a few versions back, when I narrow e.g. an array type using a PHPDoc var annotation (but also using Psl\Type\.. features from azjezz/psl), and passing this to a function I'm using to construct a \Ds\Map of the items the narrowed type is lost.

Hope there is not something I'm missing but it started to occur with v1.11.2, additional assertion inside the mapper function can get rid of the error, but from what it seems the type should be known for phpstan without the assertion.

Code snippet that reproduces the problem

https://phpstan.org/r/cb1cf6e6-46ad-423b-addb-60ea588987de

Expected output

No errors.

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

Makes me happy every day when I see no errors :)

p4veI avatar Jul 25 '24 15:07 p4veI

@p4veI After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.0 – 8.3 (3 errors)
+==========
+
 41: Dumped type: array<array{id: string, bool: bool}>
 43: Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
+46: Dumped type: array{id: string, bool: bool}
+
+PHP 7.2 – 7.4 (5 errors)
+==========
+
+41: Dumped type: array<array{id: string, bool: bool}>
+43: Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
+45: Parameter #2 $mapper of function mapFromIterable expects callable(int|string, array{id: string, bool: bool}): Ds\Pair<mixed, mixed>, Closure(mixed, array): Ds\Pair<string, bool> given.
+45: Parameter $_ of anonymous function has invalid type mixed.
 46: Dumped type: array{id: string, bool: bool}
Full report

PHP 8.0 – 8.3 (3 errors)

Line Error
41 Dumped type: array<array{id: string, bool: bool}>
43 Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
46 Dumped type: array{id: string, bool: bool}

PHP 7.2 – 7.4 (5 errors)

Line Error
41 Dumped type: array<array{id: string, bool: bool}>
43 Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
45 `Parameter #2 $mapper of function mapFromIterable expects callable(int
45 Parameter $_ of anonymous function has invalid type mixed.
46 Dumped type: array{id: string, bool: bool}

phpstan-bot avatar Sep 04 '24 14:09 phpstan-bot

@p4veI After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.0 – 8.3 (4 errors)
+==========
+
+34: PHPDoc tag @var with type array<array{id: string, bool: bool}> is not subtype of native type array{array{id: 'c5a89f6f-5fc2-4444…', bool: true}}.
 41: Dumped type: array<array{id: string, bool: bool}>
 43: Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
+46: Dumped type: array{id: string, bool: bool}
+
+PHP 7.2 – 7.4 (6 errors)
+==========
+
+34: PHPDoc tag @var with type array<array{id: string, bool: bool}> is not subtype of native type array{array{id: 'c5a89f6f-5fc2-4444…', bool: true}}.
+41: Dumped type: array<array{id: string, bool: bool}>
+43: Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
+45: Parameter #2 $mapper of function mapFromIterable expects callable(int|string, array{id: string, bool: bool}): Ds\Pair<mixed, mixed>, Closure(mixed, array): Ds\Pair<string, bool> given.
+45: Parameter $_ of anonymous function has invalid type mixed.
 46: Dumped type: array{id: string, bool: bool}
Full report

PHP 8.0 – 8.3 (4 errors)

Line Error
34 PHPDoc tag @var with type array<array{id: string, bool: bool}> is not subtype of native type array{array{id: 'c5a89f6f-5fc2-4444…', bool: true}}.
41 Dumped type: array<array{id: string, bool: bool}>
43 Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
46 Dumped type: array{id: string, bool: bool}

PHP 7.2 – 7.4 (6 errors)

Line Error
34 PHPDoc tag @var with type array<array{id: string, bool: bool}> is not subtype of native type array{array{id: 'c5a89f6f-5fc2-4444…', bool: true}}.
41 Dumped type: array<array{id: string, bool: bool}>
43 Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
45 `Parameter #2 $mapper of function mapFromIterable expects callable(int
45 Parameter $_ of anonymous function has invalid type mixed.
46 Dumped type: array{id: string, bool: bool}

phpstan-bot avatar Sep 28 '24 11:09 phpstan-bot

@p4veI After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.0 – 8.3 (4 errors)
+==========
+
 41: Dumped type: array<array{id: string, bool: bool}>
 43: Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
+45: Parameter #2 $mapper of function mapFromIterable expects callable(int|string, array{id: string, bool: bool}): Ds\Pair<mixed, mixed>, Closure(mixed, array): Ds\Pair<string, bool> given.
+46: Dumped type: array{id: string, bool: bool}
+
+PHP 7.2 – 7.4 (5 errors)
+==========
+
+41: Dumped type: array<array{id: string, bool: bool}>
+43: Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
+45: Parameter #2 $mapper of function mapFromIterable expects callable(int|string, array{id: string, bool: bool}): Ds\Pair<mixed, mixed>, Closure(mixed, array): Ds\Pair<string, bool> given.
+45: Parameter $_ of anonymous function has invalid type mixed.
 46: Dumped type: array{id: string, bool: bool}
Full report

PHP 8.0 – 8.3 (4 errors)

Line Error
41 Dumped type: array<array{id: string, bool: bool}>
43 Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
45 `Parameter #2 $mapper of function mapFromIterable expects callable(int
46 Dumped type: array{id: string, bool: bool}

PHP 7.2 – 7.4 (5 errors)

Line Error
41 Dumped type: array<array{id: string, bool: bool}>
43 Function getMap() should return Ds\Map<string, bool> but returns Ds\Map<mixed, mixed>.
45 `Parameter #2 $mapper of function mapFromIterable expects callable(int
45 Parameter $_ of anonymous function has invalid type mixed.
46 Dumped type: array{id: string, bool: bool}

phpstan-bot avatar Oct 07 '24 07:10 phpstan-bot