|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace ArrayClosureParameters; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use function PHPStan\Testing\assertNativeType; |
| 9 | +use function PHPStan\Testing\assertType; |
| 10 | + |
| 11 | +/** @param list<callable(string, int): mixed> $callbacks */ |
| 12 | +function callbacks(array $callbacks): void {} |
| 13 | + |
| 14 | +callbacks([ |
| 15 | + function ($value, $key) { |
| 16 | + assertType('string', $value); |
| 17 | + assertType('int', $key); |
| 18 | + assertNativeType('mixed', $value); |
| 19 | + assertNativeType('mixed', $key); |
| 20 | + return $value; |
| 21 | + }, |
| 22 | + fn ($value, $key) => [assertType('string', $value), assertType('int', $key), assertNativeType('mixed', $value)], |
| 23 | + function (int $value, $key) { |
| 24 | + assertType('int', $value); |
| 25 | + assertType('int', $key); |
| 26 | + }, |
| 27 | +]); |
| 28 | + |
| 29 | +/** @param array{first: callable(string): mixed, second?: Closure(int): mixed} $callbacks */ |
| 30 | +function shape(array $callbacks): void {} |
| 31 | + |
| 32 | +shape(callbacks: [ |
| 33 | + 'second' => fn ($value) => assertType('int', $value), |
| 34 | + 'first' => function ($value) { |
| 35 | + assertType('string', $value); |
| 36 | + $unrelated = [function ($other) { |
| 37 | + assertType('mixed', $other); |
| 38 | + }]; |
| 39 | + }, |
| 40 | +]); |
| 41 | + |
| 42 | +/** @param array{callable(string): mixed, callable(int): mixed} $callbacks */ |
| 43 | +function tuple(array $callbacks): void {} |
| 44 | + |
| 45 | +tuple([ |
| 46 | + fn ($value) => assertType('string', $value), |
| 47 | + fn ($value) => assertType('int', $value), |
| 48 | +]); |
| 49 | + |
| 50 | +/** @param array{5: string, 6: callable(int): mixed} $callbacks */ |
| 51 | +function numericShape(array $callbacks): void {} |
| 52 | + |
| 53 | +numericShape([5 => 'value', fn ($value) => assertType('int', $value)]); |
| 54 | + |
| 55 | +/** @param array<string, list<callable(string): mixed>> $callbacks */ |
| 56 | +function nested(array $callbacks): void {} |
| 57 | + |
| 58 | +nested(['first' => [function ($value) { |
| 59 | + assertType('string', $value); |
| 60 | +}]]); |
| 61 | + |
| 62 | +/** @param list<callable(string): mixed>|null $callbacks */ |
| 63 | +function nullable(?array $callbacks): void {} |
| 64 | + |
| 65 | +nullable([fn ($value) => assertType('string', $value)]); |
| 66 | + |
| 67 | +/** @param iterable<callable(string): mixed> $callbacks */ |
| 68 | +function iterableCallbacks(iterable $callbacks): void {} |
| 69 | + |
| 70 | +iterableCallbacks([fn ($value) => assertType('string', $value)]); |
| 71 | + |
| 72 | +/** |
| 73 | + * @template T |
| 74 | + * @param T $value |
| 75 | + * @param list<callable(T): mixed> $callbacks |
| 76 | + */ |
| 77 | +function generic($value, array $callbacks): void {} |
| 78 | + |
| 79 | +generic(new \stdClass(), [fn ($value) => assertType('stdClass', $value)]); |
| 80 | + |
| 81 | +class Receiver |
| 82 | +{ |
| 83 | + /** @param list<callable(string): mixed> $callbacks */ |
| 84 | + public function __construct(array $callbacks) {} |
| 85 | + |
| 86 | + /** @param list<callable(int): mixed> $callbacks */ |
| 87 | + public static function run(array $callbacks): void {} |
| 88 | + |
| 89 | + /** @param list<callable(string): mixed> ...$callbacks */ |
| 90 | + public function variadic(array ...$callbacks): void {} |
| 91 | +} |
| 92 | + |
| 93 | +$receiver = new Receiver([fn ($value) => assertType('string', $value)]); |
| 94 | +Receiver::run([fn ($value) => assertType('int', $value)]); |
| 95 | +$receiver->variadic([fn ($value) => assertType('string', $value)], [fn ($value) => assertType('string', $value)]); |
| 96 | + |
| 97 | +$unrelated = [fn ($value) => assertType('mixed', $value)]; |
| 98 | +callbacks($unrelated); |
0 commit comments