← All results

value-of<T> with a class-constant operand

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

The mirror of the constant-operand key-of case: the operand names a class constant, and the type is the union of that array's values, 1|10. The probe sits *between* the two members — 5 is an int like both of them, so a tool that resolves the constant but widens the values to int accepts it, and only a tool that keeps the literal union rejects it. References: - PHPStan phpdoc-types: value-of with constant operands - Psalm utility_types.md: value-of on class constants

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 Unrecognized
phpdoc_advanced_fallback_value_of_constant.php:27: Saw a token Phan may have failed to parse after '* @return value-of<Levels::MAP>': after value-of, saw '<' [PhanUnextractableAnnotationSuffix]
phpdoc_advanced_fallback_value_of_constant.php:39: Saw possibly unextractable annotation for a fragment of comment '* @param value-of<Levels::MAP> $value': after value-of, did not see an element name (will guess based on comment order) [PhanUnextractableAnnotationElementName]
phpdoc_advanced_fallback_value_of_constant.php:39: Saw a token Phan may have failed to parse after '* @param value-of<Levels::MAP> $value': after value-of, saw '<' [PhanUnextractableAnnotationSuffix]
  • Recognition: spelling not resolved — reported on declaration line(s) 27, 39
  • Enforcement: 0/2 of the expected violations reported — incidental, since the spelling was not resolved
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced reported Lv.5+
phpdoc_advanced_fallback_value_of_constant.php:54: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, 5 given. [identifier=argument.type] [reported-from-level=5]
phpdoc_advanced_fallback_value_of_constant.php:57: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, 'low' given. [identifier=argument.type] [reported-from-level=5]
With strict-rules
phpdoc_advanced_fallback_value_of_constant.php:54: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, 5 given. [identifier=argument.type]
phpdoc_advanced_fallback_value_of_constant.php:57: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, 'low' 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_value_of_constant.php:54: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, but 5 provided [InvalidArgument]
phpdoc_advanced_fallback_value_of_constant.php:57: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, but 'low' provided [InvalidArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mago 1.46.0mago 1.46.0 Enforced
phpdoc_advanced_fallback_value_of_constant.php:54: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel`: expected `int(1)|int(10)`, but found `int(5)`. [invalid-argument]
phpdoc_advanced_fallback_value_of_constant.php:57: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel`: expected `int(1)|int(10)`, but found `string('low')`. [invalid-argument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mir 0.70.1mir 0.70.1 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpantom 0.9.0phpantom_lsp 0.9.0 Incidental (2/2)
phpdoc_advanced_fallback_value_of_constant.php:31: Return type int is incompatible with declared return type value-of<Levels::MAP> [type_mismatch_return]
phpdoc_advanced_fallback_value_of_constant.php:46: Argument 1 ($value) expects int, got value-of<Levels::MAP> [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_constant.php:49: Argument 1 ($value) expects value-of<Levels::MAP>, got 1 [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_constant.php:50: Argument 1 ($value) expects value-of<Levels::MAP>, got 10 [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_constant.php:54: Argument 1 ($value) expects value-of<Levels::MAP>, got 5 [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_constant.php:57: Argument 1 ($value) expects value-of<Levels::MAP>, got 'low' [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) 31, 46, 49, 50
Expectation diff
Line 31: Expected valid value to be accepted, got ["Return type int is incompatible with declared return type value-of<Levels::MAP> [type_mismatch_return]"]
Line 46: Expected valid value to be accepted, got ["Argument 1 ($value) expects int, got value-of<Levels::MAP> [type_mismatch_argument]"]
Line 49: Expected valid value to be accepted, got ["Argument 1 ($value) expects value-of<Levels::MAP>, got 1 [type_mismatch_argument]"]
Line 50: Expected valid value to be accepted, got ["Argument 1 ($value) expects value-of<Levels::MAP>, got 10 [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_value_of_constant.php:27: Return type in PHPDoc does not match the actual return type [PhpReturnDocTypeMismatchInspection]
  • Recognition: spelling not resolved — reported on declaration line(s) 27
  • 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_value_of_constant.php:54: argument 5 to acceptsLevel() violates declared @param value-of<Levels::MAP> $value — declared contract violation [phpdoc.param-mismatch]
phpdoc_advanced_fallback_value_of_constant.php:57: argument "low" to acceptsLevel() violates declared @param value-of<Levels::MAP> $value — 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_value_of_constant.php:54: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, 5 given. [identifier=argument.type]
phpdoc_advanced_fallback_value_of_constant.php:57: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, 'low' 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_value_of_constant.php:54: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, but 5 provided [InvalidArgument]
phpdoc_advanced_fallback_value_of_constant.php:57: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant\acceptsLevel expects 1|10, but 'low' provided [InvalidArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedFallbackValueOfConstant;

/**
 * `value-of<T>` with a class-constant operand
 *
 * The mirror of the constant-operand `key-of` case: the operand names a class
 * constant, and the type is the union of that array's values, `1|10`. The
 * probe sits *between* the two members — `5` is an int like both of them, so
 * a tool that resolves the constant but widens the values to `int` accepts it,
 * and only a tool that keeps the literal union rejects it.
 *
 * References:
 * - PHPStan phpdoc-types: value-of with constant operands
 * - Psalm utility_types.md: value-of on class constants
 */

final class Levels
{
    public const MAP = ['low' => 1, 'high' => 10];
}

/**
 * @return value-of<Levels::MAP>
 */
function returnsLevel() // T: value-of<Levels::MAP>
{
    return \random_int(0, 1) === 1 ? 1 : 10; // V
}

function acceptsInt(int $value): void
{
}

/**
 * @param value-of<Levels::MAP> $value
 */
function acceptsLevel($value): void // T: value-of<Levels::MAP>
{
}

// Every value of the constant is an int.
acceptsInt(returnsLevel()); // V

// Both values of the constant satisfy the parameter.
acceptsLevel(1); // V
acceptsLevel(10); // V

// An int between them does not: the type is the union of the literals, not
// their common base type.
acceptsLevel(5); // E?: 5 is not a value of Levels::MAP

// One of the constant's keys is not one of its values either.
acceptsLevel('low'); // E?: 'low' is a key of Levels::MAP, not a value of it