You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Throws: TypeError: processTuple(): Argument $tuple['0'] must be of type positive-int
240
240
```
241
241
242
+
Here is the updated documentation with a special, dedicated section for **`key-of<T>`** and **`value-of<T>`** inside `docs/supported-types/arrays-and-shapes.md`.
243
+
244
+
---
245
+
## Key & Value Extraction (`key-of<T>` & `value-of<T>`)
246
+
247
+
TypePHP supports dynamically restricting function parameters, return types, property writes, or array shape fields to the keys or values of an array constant, an array shape, or a PHP 8.1 Backed Enum using `key-of<T>` and `value-of<T>` type operators.
248
+
249
+
> **Performance & Visibility:** TypePHP caches array and enum extractions in static memory, guaranteeing **$O(1)$ constant lookup times** during execution. Furthermore, it uses Reflection to safely bypass PHP visibility restrictions, allowing you to reference `private` or `protected` class constants (e.g., `key-of<self::PRIVATE_MAP>`) in docblocks without throwing runtime errors.
|**`key-of<T>`**| Array Constant, Array Shape, Enum | Validates that the value matches a valid **array key** or **Enum case name** (e.g., `'Active'`). |
254
+
|**`value-of<T>`**| Array Constant, Backed Enum | Validates that the value matches a valid **array value** or **Enum backing value** (e.g., `'active'`). |
255
+
256
+
---
257
+
258
+
### 1. Extracting from Class Constants
259
+
260
+
Extract allowed keys or values directly from `public`, `protected`, or `private` class constant arrays:
261
+
262
+
```php
263
+
<?php
264
+
265
+
declare(strict_types=1);
266
+
267
+
namespace App\Database;
268
+
269
+
class DriverManager
270
+
{
271
+
/**
272
+
* Private constant array
273
+
*/
274
+
private const DRIVER_MAP = [
275
+
'pdo_mysql' => 'PDO\MySQL\Driver',
276
+
'pdo_sqlite' => 'PDO\SQLite\Driver',
277
+
];
278
+
279
+
/**
280
+
* @param key-of<self::DRIVER_MAP> $driverKey
281
+
* @param value-of<self::DRIVER_MAP> $driverClass
282
+
*/
283
+
public function connect(string $driverKey, string $driverClass): void
0 commit comments