|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug15491; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +function idFromAlias(string $alias): ?string |
| 8 | +{ |
| 9 | + return strpos($alias, '.') !== false ? md5($alias) : null; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * @param array<string, mixed> $data |
| 14 | + * @return array<string, mixed> |
| 15 | + */ |
| 16 | +function withCoalesce(array $data): array |
| 17 | +{ |
| 18 | + $data['ids'] = array_filter( |
| 19 | + array_map( |
| 20 | + function (string $alias): ?string { |
| 21 | + $id = idFromAlias($alias); |
| 22 | + if ($id === null) { |
| 23 | + echo 'invalid'; |
| 24 | + } |
| 25 | + return $id; |
| 26 | + }, |
| 27 | + explode('|', (string) ($data['options'] ?? '')) |
| 28 | + ) |
| 29 | + ); |
| 30 | + assertType('array<int<0, max>, non-falsy-string>', $data['ids']); |
| 31 | + |
| 32 | + return $data; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @template T |
| 37 | + * @param T $x |
| 38 | + * @return T |
| 39 | + */ |
| 40 | +function identity($x) |
| 41 | +{ |
| 42 | + return $x; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @param mixed $x |
| 47 | + * @return list<string> |
| 48 | + */ |
| 49 | +function strings($x): array |
| 50 | +{ |
| 51 | + return []; |
| 52 | +} |
| 53 | + |
| 54 | +/** @param array<string, mixed> $data */ |
| 55 | +function assignOp(array $data): void |
| 56 | +{ |
| 57 | + $x = identity(array_map( |
| 58 | + function (string $alias): int { |
| 59 | + $i = 0; |
| 60 | + $i += strlen($alias); |
| 61 | + if ($i === 0) { |
| 62 | + echo 'empty'; |
| 63 | + } |
| 64 | + return $i; |
| 65 | + }, |
| 66 | + strings($data['options'] ?? '') |
| 67 | + )); |
| 68 | + assertType('list<int<0, max>>', $x); |
| 69 | +} |
| 70 | + |
| 71 | +/** @param array<string, mixed> $data */ |
| 72 | +function coalesceAssign(array $data): void |
| 73 | +{ |
| 74 | + $x = identity(array_map( |
| 75 | + function (string $alias): ?string { |
| 76 | + $id = null; |
| 77 | + $id ??= idFromAlias($alias); |
| 78 | + if ($id === null) { |
| 79 | + echo 'invalid'; |
| 80 | + } |
| 81 | + return $id; |
| 82 | + }, |
| 83 | + strings($data['options'] ?? '') |
| 84 | + )); |
| 85 | + assertType('list<string|null>', $x); |
| 86 | +} |
| 87 | + |
| 88 | +/** @param array<string, mixed> $data */ |
| 89 | +function increment(array $data): void |
| 90 | +{ |
| 91 | + $x = identity(array_map( |
| 92 | + function (string $alias): int { |
| 93 | + $i = 0; |
| 94 | + $i++; |
| 95 | + if ($i === 0) { |
| 96 | + echo 'never'; |
| 97 | + } |
| 98 | + return $i; |
| 99 | + }, |
| 100 | + strings($data['options'] ?? '') |
| 101 | + )); |
| 102 | + assertType('list<1>', $x); |
| 103 | +} |
| 104 | + |
| 105 | +/** @param array<string, mixed> $data */ |
| 106 | +function byRefOutParameter(array $data): void |
| 107 | +{ |
| 108 | + $x = identity(array_map( |
| 109 | + function (string $alias): ?string { |
| 110 | + $m = null; |
| 111 | + preg_match('/a/', $alias, $m); |
| 112 | + if ($m === null) { |
| 113 | + echo 'never'; |
| 114 | + } |
| 115 | + return $m[0] ?? null; |
| 116 | + }, |
| 117 | + strings($data['options'] ?? '') |
| 118 | + )); |
| 119 | + assertType('list<non-empty-string|null>', $x); |
| 120 | +} |
| 121 | + |
| 122 | +/** @param array<string, mixed> $data */ |
| 123 | +function listAssign(array $data): void |
| 124 | +{ |
| 125 | + $x = identity(array_map( |
| 126 | + function (string $alias): ?string { |
| 127 | + [$id] = [idFromAlias($alias)]; |
| 128 | + if ($id === null) { |
| 129 | + echo 'invalid'; |
| 130 | + } |
| 131 | + return $id; |
| 132 | + }, |
| 133 | + strings($data['options'] ?? '') |
| 134 | + )); |
| 135 | + assertType('list<string|null>', $x); |
| 136 | +} |
| 137 | + |
| 138 | +/** @param array<string, mixed> $data */ |
| 139 | +function arrowFunction(array $data): void |
| 140 | +{ |
| 141 | + $x = identity(array_map( |
| 142 | + fn (string $alias): ?string => ($id = idFromAlias($alias)) === null ? $id : $id, |
| 143 | + strings($data['options'] ?? '') |
| 144 | + )); |
| 145 | + assertType('list<string|null>', $x); |
| 146 | +} |
| 147 | + |
| 148 | +/** @param array<string, mixed> $data */ |
| 149 | +function arrayFilterCallback(array $data): void |
| 150 | +{ |
| 151 | + $x = identity(array_filter( |
| 152 | + strings($data['options'] ?? ''), |
| 153 | + function (string $alias): bool { |
| 154 | + $id = idFromAlias($alias); |
| 155 | + if ($id === null) { |
| 156 | + echo 'invalid'; |
| 157 | + } |
| 158 | + return $id !== null; |
| 159 | + } |
| 160 | + )); |
| 161 | + assertType('array<int<0, max>, string>', $x); |
| 162 | +} |
0 commit comments