|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use TypePHP\Exception\TypeError; |
| 6 | + |
| 7 | +describe('Array Shape Destructuring Type Propagation', function () { |
| 8 | + describe('Associative Array Shape Destructuring ([\'key\' => $var] = $source)', function () { |
| 9 | + test('propagates member types to extracted variables and allows valid reassignments', function () { |
| 10 | + /** @var array{id: positive-int, name: non-empty-string} $source */ |
| 11 | + $source = ['id' => 1, 'name' => 'Alice']; |
| 12 | + |
| 13 | + ['id' => $extractedId, 'name' => $extractedName] = $source; |
| 14 | + |
| 15 | + expect($extractedId)->toBe(1) |
| 16 | + ->and($extractedName)->toBe('Alice') |
| 17 | + ; |
| 18 | + |
| 19 | + $extractedId = 42; |
| 20 | + $extractedName = 'Bob'; |
| 21 | + |
| 22 | + expect($extractedId)->toBe(42) |
| 23 | + ->and($extractedName)->toBe('Bob') |
| 24 | + ; |
| 25 | + }); |
| 26 | + |
| 27 | + test('throws TypeError when variable extracted from shape is reassigned to invalid integer', function () { |
| 28 | + /** @var array{id: positive-int, name: non-empty-string} $source */ |
| 29 | + $source = ['id' => 1, 'name' => 'Alice']; |
| 30 | + |
| 31 | + ['id' => $extractedId, 'name' => $extractedName] = $source; |
| 32 | + |
| 33 | + expect(function () use (&$extractedId) { |
| 34 | + $extractedId = -5; |
| 35 | + })->toThrow(TypeError::class, 'Variable $extractedId must be of type positive-int'); |
| 36 | + |
| 37 | + expect(function () use (&$extractedId) { |
| 38 | + $extractedId = 0; |
| 39 | + })->toThrow(TypeError::class, 'Variable $extractedId must be of type positive-int'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('throws TypeError when variable extracted from shape is reassigned to invalid string', function () { |
| 43 | + /** @var array{id: positive-int, name: non-empty-string} $source */ |
| 44 | + $source = ['id' => 1, 'name' => 'Alice']; |
| 45 | + |
| 46 | + ['id' => $extractedId, 'name' => $extractedName] = $source; |
| 47 | + |
| 48 | + expect(function () use (&$extractedName) { |
| 49 | + $extractedName = ''; |
| 50 | + })->toThrow(TypeError::class, 'Variable $extractedName must be of type non-empty-string'); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('Positional Tuple Destructuring ([$a, $b] = $source)', function () { |
| 55 | + test('propagates positional tuple types and prevents invalid reassignments', function () { |
| 56 | + /** @var array{positive-int, non-empty-string} $tupleSource */ |
| 57 | + $tupleSource = [10, 'First']; |
| 58 | + |
| 59 | + [$first, $second] = $tupleSource; |
| 60 | + |
| 61 | + expect($first)->toBe(10) |
| 62 | + ->and($second)->toBe('First') |
| 63 | + ; |
| 64 | + |
| 65 | + $first = 99; |
| 66 | + $second = 'Updated'; |
| 67 | + expect($first)->toBe(99)->and($second)->toBe('Updated'); |
| 68 | + |
| 69 | + expect(function () use (&$first) { |
| 70 | + $first = -10; |
| 71 | + })->toThrow(TypeError::class, 'Variable $first must be of type positive-int'); |
| 72 | + |
| 73 | + expect(function () use (&$second) { |
| 74 | + $second = ''; |
| 75 | + })->toThrow(TypeError::class, 'Variable $second must be of type non-empty-string'); |
| 76 | + }); |
| 77 | + |
| 78 | + test('propagates types when elements are skipped using empty comma syntax', function () { |
| 79 | + /** @var array{positive-int, string, int<1, 100>} $tupleSource */ |
| 80 | + $tupleSource = [42, 'skipped', 50]; |
| 81 | + |
| 82 | + [$first, , $third] = $tupleSource; |
| 83 | + |
| 84 | + expect($first)->toBe(42) |
| 85 | + ->and($third)->toBe(50) |
| 86 | + ; |
| 87 | + |
| 88 | + expect(function () use (&$first) { |
| 89 | + $first = 0; |
| 90 | + })->toThrow(TypeError::class, 'Variable $first must be of type positive-int'); |
| 91 | + |
| 92 | + expect(function () use (&$third) { |
| 93 | + $third = 150; |
| 94 | + })->toThrow(TypeError::class, '<= 100'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('Traditional list(...) Syntax Destructuring', function () { |
| 99 | + test('propagates types when using traditional list() syntax with associative keys', function () { |
| 100 | + /** @var array{code: positive-int, label: non-empty-string} $data */ |
| 101 | + $data = ['code' => 200, 'label' => 'OK']; |
| 102 | + |
| 103 | + list('code' => $code, 'label' => $label) = $data; |
| 104 | + |
| 105 | + expect($code)->toBe(200) |
| 106 | + ->and($label)->toBe('OK') |
| 107 | + ; |
| 108 | + |
| 109 | + expect(function () use (&$code) { |
| 110 | + $code = -1; |
| 111 | + })->toThrow(TypeError::class, 'Variable $code must be of type positive-int'); |
| 112 | + |
| 113 | + expect(function () use (&$label) { |
| 114 | + $label = ''; |
| 115 | + })->toThrow(TypeError::class, 'Variable $label must be of type non-empty-string'); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('Nested Destructuring ([[\'id\' => $id]] = $source)', function () { |
| 120 | + test('recursively propagates types through nested array shape destructuring', function () { |
| 121 | + /** |
| 122 | + * @var array{ |
| 123 | + * account: array{id: positive-int, role: 'admin'|'user'}, |
| 124 | + * status: non-empty-string |
| 125 | + * } $payload |
| 126 | + */ |
| 127 | + $payload = [ |
| 128 | + 'account' => ['id' => 100, 'role' => 'admin'], |
| 129 | + 'status' => 'active', |
| 130 | + ]; |
| 131 | + |
| 132 | + ['account' => ['id' => $accountId, 'role' => $accountRole], 'status' => $status] = $payload; |
| 133 | + |
| 134 | + expect($accountId)->toBe(100) |
| 135 | + ->and($accountRole)->toBe('admin') |
| 136 | + ->and($status)->toBe('active') |
| 137 | + ; |
| 138 | + |
| 139 | + $accountRole = 'user'; |
| 140 | + expect($accountRole)->toBe('user'); |
| 141 | + |
| 142 | + expect(function () use (&$accountId) { |
| 143 | + $accountId = -99; |
| 144 | + })->toThrow(TypeError::class, 'Variable $accountId must be of type positive-int'); |
| 145 | + |
| 146 | + expect(function () use (&$accountRole) { |
| 147 | + $accountRole = 'superadmin'; |
| 148 | + })->toThrow(TypeError::class, "('admin' | 'user')"); |
| 149 | + |
| 150 | + expect(function () use (&$status) { |
| 151 | + $status = ''; |
| 152 | + })->toThrow(TypeError::class, 'Variable $status must be of type non-empty-string'); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('Complex Types Propagated from Shapes (Unions & Refinements)', function () { |
| 157 | + test('propagates literal union types and validates reassignment against union', function () { |
| 158 | + /** @var array{mode: 'sync'|'async', timeout: int<1, 60>} $options */ |
| 159 | + $options = ['mode' => 'sync', 'timeout' => 30]; |
| 160 | + |
| 161 | + ['mode' => $mode, 'timeout' => $timeout] = $options; |
| 162 | + |
| 163 | + $mode = 'async'; |
| 164 | + $timeout = 10; |
| 165 | + expect($mode)->toBe('async')->and($timeout)->toBe(10); |
| 166 | + |
| 167 | + expect(function () use (&$mode) { |
| 168 | + $mode = 'deferred'; |
| 169 | + })->toThrow(TypeError::class, "('sync' | 'async')"); |
| 170 | + |
| 171 | + expect(function () use (&$timeout) { |
| 172 | + $timeout = 120; |
| 173 | + })->toThrow(TypeError::class, '<= 60'); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe('Non-Regressions & Unannotated Fallbacks', function () { |
| 178 | + test('does not interfere with destructuring from untyped raw arrays', function () { |
| 179 | + $untyped = ['x' => 1, 'y' => 'hello']; |
| 180 | + |
| 181 | + ['x' => $x, 'y' => $y] = $untyped; |
| 182 | + |
| 183 | + expect($x)->toBe(1)->and($y)->toBe('hello'); |
| 184 | + |
| 185 | + $x = -50; |
| 186 | + $y = ''; |
| 187 | + expect($x)->toBe(-50)->and($y)->toBe(''); |
| 188 | + }); |
| 189 | + |
| 190 | + test('retaining explicit inline @var on destructuring statement overrides source shape', function () { |
| 191 | + /** @var array{id: int, name: string} $source */ |
| 192 | + $source = ['id' => 10, 'name' => 'Alice']; |
| 193 | + |
| 194 | + /** |
| 195 | + * Explicit inline @var refines $id to positive-int: |
| 196 | + * |
| 197 | + * @var positive-int $id |
| 198 | + */ |
| 199 | + ['id' => $id, 'name' => $name] = $source; |
| 200 | + |
| 201 | + expect(function () use (&$id) { |
| 202 | + $id = -1; |
| 203 | + })->toThrow(TypeError::class, 'Variable $id must be of type positive-int'); |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments