|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use TypePHP\Exception\TypeError; |
| 6 | + |
| 7 | +/** |
| 8 | + * 1. Open tuple with single value constraint: array{string, int, ...<float>} |
| 9 | + * |
| 10 | + * @param array{string, int, ...<float>} $tuple |
| 11 | + */ |
| 12 | +function tddOpenTupleSingleValueType(array $tuple): array |
| 13 | +{ |
| 14 | + return $tuple; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * 2. Open tuple with array spread syntax: array{string, int, ...float[]} |
| 19 | + * |
| 20 | + * @param array{string, int, ...float[]} $tuple |
| 21 | + */ |
| 22 | +function tddOpenTupleArraySpread(array $tuple): array |
| 23 | +{ |
| 24 | + return $tuple; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * 3. Open tuple with list spread syntax: array{string, ...list<positive-int>} |
| 29 | + * |
| 30 | + * @param array{string, ...list<positive-int>} $data |
| 31 | + */ |
| 32 | +function tddOpenTupleListSpread(array $data): array |
| 33 | +{ |
| 34 | + return $data; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * 4. Bare open tuple (allows any extra elements): array{string, int, ...} |
| 39 | + * |
| 40 | + * @param array{string, int, ...} $tuple |
| 41 | + */ |
| 42 | +function tddBareOpenTuple(array $tuple): array |
| 43 | +{ |
| 44 | + return $tuple; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * 5. Open associative shape with single value type: array{id: positive-int, ...<string>} |
| 49 | + * |
| 50 | + * @param array{id: positive-int, ...<string>} $payload |
| 51 | + */ |
| 52 | +function tddOpenShapeSingleValueType(array $payload): array |
| 53 | +{ |
| 54 | + return $payload; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * 6. Open sequential list tuple: list{string, int, ...<float>} |
| 59 | + * |
| 60 | + * @param list{string, int, ...<float>} $list |
| 61 | + */ |
| 62 | +function tddOpenListTuple(array $list): array |
| 63 | +{ |
| 64 | + return $list; |
| 65 | +} |
| 66 | + |
| 67 | +describe('Open / Variadic Tuples and Shapes (PHPStan Spec Baseline)', function () { |
| 68 | + describe('1. Single Value Constraint (...<float>)', function () { |
| 69 | + test('accepts tuple with zero extra elements', function () { |
| 70 | + expect(tddOpenTupleSingleValueType(['Alice', 10]))->toBe(['Alice', 10]); |
| 71 | + }); |
| 72 | + |
| 73 | + test('accepts tuple with multiple valid trailing float elements', function () { |
| 74 | + expect(tddOpenTupleSingleValueType(['Alice', 10, 1.5, 2.5, 3.5])) |
| 75 | + ->toBe(['Alice', 10, 1.5, 2.5, 3.5]) |
| 76 | + ; |
| 77 | + }); |
| 78 | + |
| 79 | + test('rejects tuple when trailing element violates float constraint', function () { |
| 80 | + expect(fn () => tddOpenTupleSingleValueType(['Alice', 10, 1.5, 'not_a_float'])) |
| 81 | + ->toThrow(TypeError::class, "['3'] must be of type float") |
| 82 | + ; |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('2. Array Spread Syntax (...float[])', function () { |
| 87 | + test('accepts tuple with zero extra elements using array spread', function () { |
| 88 | + expect(tddOpenTupleArraySpread(['Bob', 20]))->toBe(['Bob', 20]); |
| 89 | + }); |
| 90 | + |
| 91 | + test('unwraps float[] so trailing elements are validated as individual floats', function () { |
| 92 | + expect(tddOpenTupleArraySpread(['Bob', 20, 1.5, 2.5]))->toBe(['Bob', 20, 1.5, 2.5]); |
| 93 | + }); |
| 94 | + |
| 95 | + test('rejects trailing element violating unwrapped float constraint', function () { |
| 96 | + expect(fn () => tddOpenTupleArraySpread(['Bob', 20, 1.5, 'invalid'])) |
| 97 | + ->toThrow(TypeError::class, "['3'] must be of type float") |
| 98 | + ; |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('3. List Spread Syntax (...list<positive-int>)', function () { |
| 103 | + test('unwraps list<positive-int> so trailing elements are validated as positive integers', function () { |
| 104 | + expect(tddOpenTupleListSpread(['tag_primary', 10, 20, 30])) |
| 105 | + ->toBe(['tag_primary', 10, 20, 30]) |
| 106 | + ; |
| 107 | + }); |
| 108 | + |
| 109 | + test('rejects trailing element violating positive-int constraint', function () { |
| 110 | + expect(fn () => tddOpenTupleListSpread(['tag_primary', 10, -5, 30])) |
| 111 | + ->toThrow(TypeError::class, "['2'] must be of type positive-int") |
| 112 | + ; |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('4. Bare ... Open Tuple (array{string, int, ...})', function () { |
| 117 | + test('accepts tuple with zero extra elements', function () { |
| 118 | + expect(tddBareOpenTuple(['Alice', 10]))->toBe(['Alice', 10]); |
| 119 | + }); |
| 120 | + |
| 121 | + test('accepts arbitrary trailing elements of any type', function () { |
| 122 | + $data = ['Alice', 10, true, 3.14, new stdClass(), ['nested']]; |
| 123 | + expect(tddBareOpenTuple($data))->toBe($data); |
| 124 | + }); |
| 125 | + |
| 126 | + test('still strictly validates the fixed leading elements', function () { |
| 127 | + expect(fn () => tddBareOpenTuple([123, 10, 'extra'])) |
| 128 | + ->toThrow(TypeError::class, "['0'] must be of type string") |
| 129 | + ; |
| 130 | + |
| 131 | + expect(fn () => tddBareOpenTuple(['Alice', 'not_an_int', 'extra'])) |
| 132 | + ->toThrow(TypeError::class, "['1'] must be of type int") |
| 133 | + ; |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('5. Associative Open Shape with Single Value Type (array{id: int, ...<string>})', function () { |
| 138 | + test('accepts shape with extra string values under arbitrary keys', function () { |
| 139 | + $payload = [ |
| 140 | + 'id' => 1, |
| 141 | + 'name' => 'Alice', |
| 142 | + 'role' => 'admin', |
| 143 | + 'dept' => 'engineering', |
| 144 | + ]; |
| 145 | + |
| 146 | + expect(tddOpenShapeSingleValueType($payload))->toBe($payload); |
| 147 | + }); |
| 148 | + |
| 149 | + test('rejects extra element when value is not a string', function () { |
| 150 | + $badPayload = [ |
| 151 | + 'id' => 1, |
| 152 | + 'name' => 'Alice', |
| 153 | + 'count' => 50, |
| 154 | + ]; |
| 155 | + |
| 156 | + expect(fn () => tddOpenShapeSingleValueType($badPayload)) |
| 157 | + ->toThrow(TypeError::class, "['count'] must be of type string") |
| 158 | + ; |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe('6. Open Sequential List Tuples (list{string, int, ...<float>})', function () { |
| 163 | + test('accepts sequential list with trailing floats', function () { |
| 164 | + expect(tddOpenListTuple(['test', 1, 1.1, 2.2]))->toBe(['test', 1, 1.1, 2.2]); |
| 165 | + }); |
| 166 | + |
| 167 | + test('rejects open list tuple when passed non-sequential associative keys', function () { |
| 168 | + $associative = [ |
| 169 | + 0 => 'test', |
| 170 | + 1 => 1, |
| 171 | + 'non_sequential' => 1.5, |
| 172 | + ]; |
| 173 | + |
| 174 | + expect(fn () => tddOpenListTuple($associative)) |
| 175 | + ->toThrow(TypeError::class, 'must be a list') |
| 176 | + ; |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments