[php-src] Issue #20571: New type operator to apply array_values to a template T
| From: | thg2k | Date: | Mon, 24 Nov 2025 10:12:20 +0000 |
| Subject: | [php-src] Issue #20571: New type operator to apply array_values to a template T | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-251292@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/20571
Author: thg2k
### Description
Greetings.
# Description
I'm looking for a way to describe a type that is the result of applying
array_values to a generic template T.
The closest I got is with value-of<T>, but for a specified type such as
array{a: int, b: string}, the result is int|string, instead I want
array{0: int, 1: string}.
# The use case
The use case I have is for a generic record manipulator that, very simplified, looks like this:
```php
/**
* ...
*
* @template T of array<mixed>
*/
class C {
/** @var array<T> */
private $r;
/** @param array<T> $r */
public function __construct($r) {
$this->r = $r;
}
/** @return array<T> */
public function full(): array {
return $this->r;
}
/** @return array<value-of<T>> */
public function values(): array {
$r = array();
foreach ($this->r as $v) {
$r[] = array_values($v);
}
return $r;
}
}
/** @var C<array{a: int, b: string}> */
$c = new C(array(
array("a" => 1, "b" => "two"),
array("a" => 3, "b" => "four")));
\PHPStan\dumpType($c->full()); // array<array{a: int, b: string}>
\PHPStan\dumpType($c->values()); // array<int|string> --> array{int, string}
```
Is there a way to obtain the desired result without a writing a plugin specific for this class C?
If not, is the use case reasonable enough to add a new feature? I would contribute the
implementation.
Alternatively, would it be possible in the forseeable future to have a plugin system for type
manipulation? So to say, a write a plugin that adds a custom type operator like
strip-keys<T>?