← All results

key-of<T>

Group: Advanced PHPDoc types · Category: ecosystem · File: phpdoc_advanced_fallback_key_of.php

The union of an array shape's keys, computed from another type rather than written out. That makes it the first spelling in this group an analyzer has to *evaluate*: recognising the keyword is not enough, it has to read the shape and derive 'name'|'age'. Analyzers that stop at recognition fall back to array-key and accept any key. References: - PHPStan TypeNodeResolver key-of resolves through Type::getKeysArray()

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Enforced
phpdoc_advanced_fallback_key_of.php:47: Argument 1 ($key) is 'missing' of type string but \Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey() takes string|string (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_key_of.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_key_of.php:51: Argument 1 ($key) is 0 of type int (value: 0) but \Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey() takes string|string (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_key_of.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced reported Lv.5+
phpdoc_advanced_fallback_key_of.php:47: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', 'missing' given. [identifier=argument.type] [reported-from-level=5]
phpdoc_advanced_fallback_key_of.php:51: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', 0 given. [identifier=argument.type] [reported-from-level=5]
With strict-rules
phpdoc_advanced_fallback_key_of.php:47: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', 'missing' given. [identifier=argument.type]
phpdoc_advanced_fallback_key_of.php:51: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', 0 given. [identifier=argument.type]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Enforced
phpdoc_advanced_fallback_key_of.php:47: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', but 'missing' provided [InvalidArgument]
phpdoc_advanced_fallback_key_of.php:51: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', but 0 provided [InvalidArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mago 1.46.0mago 1.46.0 Enforced
phpdoc_advanced_fallback_key_of.php:47: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey`: expected `string('age')|string('name')`, but found `string('missing')`. [invalid-argument]
phpdoc_advanced_fallback_key_of.php:51: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey`: expected `string('age')|string('name')`, but found `int(0)`. [invalid-argument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mir 0.70.1mir 0.70.1 Enforced
phpdoc_advanced_fallback_key_of.php:47: InvalidArgument: Argument $key of acceptsShapeKey() expects '"name"|"age"', got '"missing"' [MIR0201]
phpdoc_advanced_fallback_key_of.php:51: InvalidArgument: Argument $key of acceptsShapeKey() expects '"name"|"age"', got '0' [MIR0201]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpantom 0.9.0phpantom_lsp 0.9.0 Incidental (2/2)
phpdoc_advanced_fallback_key_of.php:25: Return type string is incompatible with declared return type key-of<array{name: string, age: int}> [type_mismatch_return]
phpdoc_advanced_fallback_key_of.php:40: Argument 1 ($value) expects int|string, got key-of<array{name: string, age: int}> [type_mismatch_argument]
phpdoc_advanced_fallback_key_of.php:43: Argument 1 ($key) expects key-of<array{name: string, age: int}>, got 'name' [type_mismatch_argument]
phpdoc_advanced_fallback_key_of.php:44: Argument 1 ($key) expects key-of<array{name: string, age: int}>, got 'age' [type_mismatch_argument]
phpdoc_advanced_fallback_key_of.php:47: Argument 1 ($key) expects key-of<array{name: string, age: int}>, got 'missing' [type_mismatch_argument]
phpdoc_advanced_fallback_key_of.php:51: Argument 1 ($key) expects key-of<array{name: string, age: int}>, got 0 [type_mismatch_argument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 25, 40, 43, 44
Expectation diff
Line 25: Expected valid value to be accepted, got ["Return type string is incompatible with declared return type key-of<array{name: string, age: int}> [type_mismatch_return]"]
Line 40: Expected valid value to be accepted, got ["Argument 1 ($value) expects int|string, got key-of<array{name: string, age: int}> [type_mismatch_argument]"]
Line 43: Expected valid value to be accepted, got ["Argument 1 ($key) expects key-of<array{name: string, age: int}>, got 'name' [type_mismatch_argument]"]
Line 44: Expected valid value to be accepted, got ["Argument 1 ($key) expects key-of<array{name: string, age: int}>, got 'age' [type_mismatch_argument]"]
intelephense 1.18.5intelephense 1.18.5 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpy 0.2.0phpy 0.2.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
qodana 262.8665.325Qodana 262.8665.325 Unrecognized
phpdoc_advanced_fallback_key_of.php:21: Return type in PHPDoc does not match the actual return type [PhpReturnDocTypeMismatchInspection]
  • Recognition: spelling not resolved — reported on declaration line(s) 21
  • Enforcement: 0/2 of the expected violations reported — incidental, since the spelling was not resolved
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.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Enforced
phpdoc_advanced_fallback_key_of.php:47: argument "missing" to acceptsShapeKey() violates declared @param key-of<array{name: string, age: int}> $key — declared contract violation [phpdoc.param-mismatch]
phpdoc_advanced_fallback_key_of.php:51: argument 0 to acceptsShapeKey() violates declared @param key-of<array{name: string, age: int}> $key — declared contract violation [phpdoc.param-mismatch]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced
phpdoc_advanced_fallback_key_of.php:47: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', 'missing' given. [identifier=argument.type]
phpdoc_advanced_fallback_key_of.php:51: Parameter #1 $key of function Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', 0 given. [identifier=argument.type]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Enforced
phpdoc_advanced_fallback_key_of.php:47: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', but 'missing' provided [InvalidArgument]
phpdoc_advanced_fallback_key_of.php:51: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackKeyOf\acceptsShapeKey expects 'age'|'name', but 0 provided [InvalidArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedFallbackKeyOf;

/**
 * `key-of<T>`
 *
 * The union of an array shape's keys, computed from another type rather than
 * written out. That makes it the first spelling in this group an analyzer has
 * to *evaluate*: recognising the keyword is not enough, it has to read the
 * shape and derive `'name'|'age'`. Analyzers that stop at recognition fall back
 * to `array-key` and accept any key.
 *
 * References:
 * - PHPStan TypeNodeResolver `key-of` resolves through Type::getKeysArray()
 */

/**
 * @return key-of<array{name: string, age: int}>
 */
function returnsShapeKey() // T: key-of<array{name: string, age: int}>
{
    return \random_int(0, 1) === 1 ? 'name' : 'age'; // V
}

function acceptsArrayKeyNatively(int|string $value): void
{
}

/**
 * @param key-of<array{name: string, age: int}> $key
 */
function acceptsShapeKey($key): void // T: key-of<array{name: string, age: int}>
{
}

// A key of the shape is an `int|string` whichever key it is.
acceptsArrayKeyNatively(returnsShapeKey()); // V

// Both keys of the shape satisfy the parameter.
acceptsShapeKey('name'); // V
acceptsShapeKey('age'); // V

// A string that is not one of them does not.
acceptsShapeKey('missing'); // E?: 'missing' is not a key of array{name: string, age: int}

// Neither does one of the shape's *values*, which is the mistake the spelling
// invites.
acceptsShapeKey(0); // E?: 0 is not a key of array{name: string, age: int}