|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug14549; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class Foo |
| 8 | +{ |
| 9 | + public function foo(array $task): void |
| 10 | + { |
| 11 | + if (\is_callable($task)) { |
| 12 | + assertType('list{class-string|object, string}&callable(): mixed', $task); |
| 13 | + assertType('class-string|object', $task[0]); |
| 14 | + assertType('string', $task[1]); |
| 15 | + |
| 16 | + foreach ($task as $key => $value) { |
| 17 | + assertType('object|string', $value); |
| 18 | + assertType('0|1', $key); |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public function testCallableArrayIterableTypes(callable $value): void |
| 24 | + { |
| 25 | + if (is_array($value)) { |
| 26 | + assertType('list{class-string|object, string}&callable(): mixed', $value); |
| 27 | + |
| 28 | + foreach ($value as $key => $val) { |
| 29 | + assertType('0|1', $key); |
| 30 | + assertType('object|string', $val); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** @param array{string, string} $task */ |
| 36 | + public function testConstantArrayNarrowing(array $task): void |
| 37 | + { |
| 38 | + if (\is_callable($task)) { |
| 39 | + // ConstantArrayType keeps its shape, callable narrows offset access |
| 40 | + assertType('list{string, string}&callable(): mixed', $task); |
| 41 | + assertType('class-string', $task[0]); |
| 42 | + assertType('string', $task[1]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** @param array<string> $task */ |
| 47 | + public function testTypedArrayNarrowing(array $task): void |
| 48 | + { |
| 49 | + if (\is_callable($task)) { |
| 50 | + // When value type is string, intersect with class-string|object gives class-string |
| 51 | + // and intersect with string gives string |
| 52 | + assertType('list{class-string, string}&callable(): mixed', $task); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** @param callable-array $value */ |
| 57 | + public function testCallableArrayPhpDoc(array $value): void |
| 58 | + { |
| 59 | + assertType('array&callable(): mixed', $value); |
| 60 | + assertType('class-string|object', $value[0]); |
| 61 | + assertType('string', $value[1]); |
| 62 | + } |
| 63 | +} |
0 commit comments