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.11PHPStan - PHP Static Analysis Tool 2.2.11 | 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. [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.
|
| 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.47.4mago 1.47.4 | 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.72.1mir 0.72.1 | Incidental (1/1) | phpdoc_advanced_fallback_key_of_constant.php:31: InvalidReturnType: Return type '"debug"|"verbose"' is not compatible with declared 'key-of<Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\Config::MAP>' [MIR0200] phpdoc_advanced_fallback_key_of_constant.php:46: InvalidArgument: Argument $value of acceptsString() expects 'string', got 'key-of<Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\Config::MAP>' [MIR0201] phpdoc_advanced_fallback_key_of_constant.php:49: InvalidArgument: Argument $key of acceptsConfigKey() expects 'key-of<Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\Config::MAP>', got '"debug"' [MIR0201] phpdoc_advanced_fallback_key_of_constant.php:50: InvalidArgument: Argument $key of acceptsConfigKey() expects 'key-of<Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\Config::MAP>', got '"verbose"' [MIR0201] phpdoc_advanced_fallback_key_of_constant.php:53: InvalidArgument: Argument $key of acceptsConfigKey() expects 'key-of<Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\Config::MAP>', got '"missing"' [MIR0201]
Expectation diffLine 31: Expected valid value to be accepted, got ["InvalidReturnType: Return type '\"debug\"|\"verbose\"' is not compatible with declared 'key-of<Conformance\\Tests\\PhpdocAdvancedFallbackKeyOfConstant\\Config::MAP>' [MIR0200]"] Line 46: Expected valid value to be accepted, got ["InvalidArgument: Argument $value of acceptsString() expects 'string', got 'key-of<Conformance\\Tests\\PhpdocAdvancedFallbackKeyOfConstant\\Config::MAP>' [MIR0201]"] Line 49: Expected valid value to be accepted, got ["InvalidArgument: Argument $key of acceptsConfigKey() expects 'key-of<Conformance\\Tests\\PhpdocAdvancedFallbackKeyOfConstant\\Config::MAP>', got '\"debug\"' [MIR0201]"] Line 50: Expected valid value to be accepted, got ["InvalidArgument: Argument $key of acceptsConfigKey() expects 'key-of<Conformance\\Tests\\PhpdocAdvancedFallbackKeyOfConstant\\Config::MAP>', got '\"verbose\"' [MIR0201]"] |
| phpantom | 0.10.0phpantom_lsp 0.10.0 | Enforced | phpdoc_advanced_fallback_key_of_constant.php:53: Argument 1 ($key) expects 'debug'|'verbose', got 'missing' [type_mismatch_argument]
|
| intelephense | 1.18.5intelephense 1.18.5 | Not enforced | No diagnostics reported.
|
| phpactor | 2026.07.22.0Phpactor 2026.07.22.0 | Not enforced | No diagnostics reported.
|
| phpy | 0.2.0phpy 0.2.0 | Not enforced | No diagnostics reported.
|
| qodana | 262.9437.196Qodana 262.9437.196 | 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.6steins 0.1.6 (2026-08-25 revision c13b4c7) | 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.11PHPStan - PHP Static Analysis Tool 2.2.11 | Enforced | phpdoc_advanced_fallback_key_of_constant.php:53: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOfConstant\acceptsConfigKey expects 'debug'|'verbose', 'missing' given.
|
| 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