key-of<T> with a class-constant operandkey-of test writes its shape out inline; here the operand is a
reference to a class constant holding an array. Evaluating it needs one step
more than the inline form: the constant has to be resolved to its value and
the value to its keys before there is anything to take the keys *of*. A tool
can support key-of<array{...}> and still not follow the reference.
References:
- PHPStan phpdoc-types: key-of with constant operands
- Psalm utility_types.md: key-of on class constants| Analyzer | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 | Unrecognized | phpdoc_advanced_fallback_key_of_constant.php:27: Saw a token Phan may have failed to parse after '* @return key-of<Config::MAP>': after key-of, saw '<' [PhanUnextractableAnnotationSuffix] phpdoc_advanced_fallback_key_of_constant.php:39: Saw possibly unextractable annotation for a fragment of comment '* @param key-of<Config::MAP> $key': after key-of, did not see an element name (will guess based on comment order) [PhanUnextractableAnnotationElementName] phpdoc_advanced_fallback_key_of_constant.php:39: Saw a token Phan may have failed to parse after '* @param key-of<Config::MAP> $key': after key-of, saw '<' [PhanUnextractableAnnotationSuffix]
|
| phpstan | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Enforced reported Lv.5+ | phpdoc_advanced_fallback_key_of_constant.php:53: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\acceptsConfigKey expects 'debug'|'verbose', 'missing' given. [identifier=argument.type] [reported-from-level=5] With strict-rulesphpdoc_advanced_fallback_key_of_constant.php:53: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\acceptsConfigKey expects 'debug'|'verbose', 'missing' given. [identifier=argument.type]
|
| psalm | 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac next: 7.0.0-beta19 |
Enforced | phpdoc_advanced_fallback_key_of_constant.php:53: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\acceptsConfigKey expects 'debug'|'verbose', but 'missing' provided [InvalidArgument]
|
| mago | 1.46.0mago 1.46.0 | Enforced | phpdoc_advanced_fallback_key_of_constant.php:53: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\acceptsConfigKey`: expected `string('debug')|string('verbose')`, but found `string('missing')`. [invalid-argument]
|
| mir | 0.70.1mir 0.70.1 | Not enforced | No diagnostics reported.
|
| phpantom | 0.9.0phpantom_lsp 0.9.0 | Incidental (1/1) | phpdoc_advanced_fallback_key_of_constant.php:31: Return type string is incompatible with declared return type key-of<Config::MAP> [type_mismatch_return] phpdoc_advanced_fallback_key_of_constant.php:46: Argument 1 ($value) expects string, got key-of<Config::MAP> [type_mismatch_argument] phpdoc_advanced_fallback_key_of_constant.php:49: Argument 1 ($key) expects key-of<Config::MAP>, got 'debug' [type_mismatch_argument] phpdoc_advanced_fallback_key_of_constant.php:50: Argument 1 ($key) expects key-of<Config::MAP>, got 'verbose' [type_mismatch_argument] phpdoc_advanced_fallback_key_of_constant.php:53: Argument 1 ($key) expects key-of<Config::MAP>, got 'missing' [type_mismatch_argument]
Expectation diffLine 31: Expected valid value to be accepted, got ["Return type string is incompatible with declared return type key-of<Config::MAP> [type_mismatch_return]"] Line 46: Expected valid value to be accepted, got ["Argument 1 ($value) expects string, got key-of<Config::MAP> [type_mismatch_argument]"] Line 49: Expected valid value to be accepted, got ["Argument 1 ($key) expects key-of<Config::MAP>, got 'debug' [type_mismatch_argument]"] Line 50: Expected valid value to be accepted, got ["Argument 1 ($key) expects key-of<Config::MAP>, got 'verbose' [type_mismatch_argument]"] |
| intelephense | 1.18.5intelephense 1.18.5 | Not enforced | No diagnostics reported.
|
| phpy | 0.2.0phpy 0.2.0 | Not enforced | No diagnostics reported.
|
| qodana | 262.8665.325Qodana 262.8665.325 | Unrecognized | phpdoc_advanced_fallback_key_of_constant.php:27: Return type in PHPDoc does not match the actual return type [PhpReturnDocTypeMismatchInspection]
|
| noverify | 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 | Not enforced | No diagnostics reported.
|
| steins | 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) | Enforced | phpdoc_advanced_fallback_key_of_constant.php:53: argument "missing" to acceptsConfigKey() violates declared @param key-of<Config::MAP> $key — declared contract violation [phpdoc.param-mismatch]
|
| phpstan-strict | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Enforced | phpdoc_advanced_fallback_key_of_constant.php:53: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\acceptsConfigKey expects 'debug'|'verbose', 'missing' given. [identifier=argument.type]
|
| psalm-next | 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 | Enforced | phpdoc_advanced_fallback_key_of_constant.php:53: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\acceptsConfigKey expects 'debug'|'verbose', but 'missing' provided [InvalidArgument]
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant;
/**
* `key-of<T>` with a class-constant operand
*
* The plain `key-of` test writes its shape out inline; here the operand is a
* reference to a class constant holding an array. Evaluating it needs one step
* more than the inline form: the constant has to be resolved to its value and
* the value to its keys before there is anything to take the keys *of*. A tool
* can support `key-of<array{...}>` and still not follow the reference.
*
* References:
* - PHPStan phpdoc-types: key-of with constant operands
* - Psalm utility_types.md: key-of on class constants
*/
final class Config
{
public const MAP = ['debug' => false, 'verbose' => true];
}
/**
* @return key-of<Config::MAP>
*/
function returnsConfigKey() // T: key-of<Config::MAP>
{
return \random_int(0, 1) === 1 ? 'debug' : 'verbose'; // V
}
function acceptsString(string $value): void
{
}
/**
* @param key-of<Config::MAP> $key
*/
function acceptsConfigKey($key): void // T: key-of<Config::MAP>
{
}
// Every key of the constant is a string.
acceptsString(returnsConfigKey()); // V
// Both keys of the constant satisfy the parameter.
acceptsConfigKey('debug'); // V
acceptsConfigKey('verbose'); // V
// A string that is not one of them does not.
acceptsConfigKey('missing'); // E?: 'missing' is not a key of Config::MAP