← All results

value-of<T> with a backed-enum operand

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

With an enum as the operand, value-of means the union of the *backing* values — 'hearts'|'spades' here, not the cases themselves. That inverts the usual direction of enum typing: the type holds the scalars, so a case object is exactly what it excludes, and a tool has to know enums well enough to walk them and still keep the two layers apart. References: - PHPStan phpdoc-types: value-of<BackedEnum> - Psalm utility_types.md: value-of on backed enums

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 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced reported Lv.5+
phpdoc_advanced_fallback_value_of_enum.php:54: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', 'clubs' given. [identifier=argument.type] [reported-from-level=5]
phpdoc_advanced_fallback_value_of_enum.php:58: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts given. [identifier=argument.type] [reported-from-level=5]
With strict-rules
phpdoc_advanced_fallback_value_of_enum.php:54: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', 'clubs' given. [identifier=argument.type]
phpdoc_advanced_fallback_value_of_enum.php:58: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts 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_enum.php:54: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', but 'clubs' provided [InvalidArgument]
phpdoc_advanced_fallback_value_of_enum.php:58: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', but enum(Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts) 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_enum.php:54: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue`: expected `string('hearts')|string('spades')`, but found `string('clubs')`. [invalid-argument]
phpdoc_advanced_fallback_value_of_enum.php:58: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue`: expected `string('hearts')|string('spades')`, but found `enum(Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts)`. [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_enum.php:32: Return type string is incompatible with declared return type value-of<Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit> [type_mismatch_return]
phpdoc_advanced_fallback_value_of_enum.php:47: Argument 1 ($value) expects string, got value-of<Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit> [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_enum.php:50: Argument 1 ($value) expects value-of<Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit>, got 'hearts' [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_enum.php:51: Argument 1 ($value) expects value-of<Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit>, got 'spades' [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_enum.php:54: Argument 1 ($value) expects value-of<Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit>, got 'clubs' [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_enum.php:58: Argument 1 ($value) expects value-of<Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit>, got Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit [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) 32, 47, 50, 51
Expectation diff
Line 32: Expected valid value to be accepted, got ["Return type string is incompatible with declared return type value-of<Conformance\\Tests\\PhpdocAdvancedFallbackValueOfEnum\\Suit> [type_mismatch_return]"]
Line 47: Expected valid value to be accepted, got ["Argument 1 ($value) expects string, got value-of<Conformance\\Tests\\PhpdocAdvancedFallbackValueOfEnum\\Suit> [type_mismatch_argument]"]
Line 50: Expected valid value to be accepted, got ["Argument 1 ($value) expects value-of<Conformance\\Tests\\PhpdocAdvancedFallbackValueOfEnum\\Suit>, got 'hearts' [type_mismatch_argument]"]
Line 51: Expected valid value to be accepted, got ["Argument 1 ($value) expects value-of<Conformance\\Tests\\PhpdocAdvancedFallbackValueOfEnum\\Suit>, got 'spades' [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 Partly enforced (1/2)
phpdoc_advanced_fallback_value_of_enum.php:58: Parameter 'Suit::Hearts' type is not compatible with declaration [PhpParamsInspection]
  • Recognition: spelling resolved
  • Enforcement: 1/2 of the expected violations reported
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Unrecognized
phpdoc_advanced_fallback_value_of_enum.php:28: Class or interface named \Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit does not exist [undefinedClass]
phpdoc_advanced_fallback_value_of_enum.php:40: Class or interface named \Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit does not exist [undefinedClass]
phpdoc_advanced_fallback_value_of_enum.php:58: Class constant \Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts does not exist [undefinedConstant]
  • Recognition: spelling not resolved — reported on declaration line(s) 28, 40
  • Enforcement: 1/2 of the expected violations reported — incidental, since the spelling was not resolved
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Widened to mixed

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported

Notes: value-of<Suit> projects over the lowered operand (project_value_of); the enum class name lowers to ContractTy::Class("suit"), which the projection's catch-all can't read a value set out of, so it floors to Opaque — enum backing values are not modeled as shape facts.

phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced
phpdoc_advanced_fallback_value_of_enum.php:54: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', 'clubs' given. [identifier=argument.type]
phpdoc_advanced_fallback_value_of_enum.php:58: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts 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_enum.php:54: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', but 'clubs' provided [InvalidArgument]
phpdoc_advanced_fallback_value_of_enum.php:58: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', but enum(Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts) provided [InvalidArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum;

/**
 * `value-of<T>` with a backed-enum operand
 *
 * With an enum as the operand, `value-of` means the union of the *backing*
 * values — `'hearts'|'spades'` here, not the cases themselves. That inverts
 * the usual direction of enum typing: the type holds the scalars, so a case
 * object is exactly what it excludes, and a tool has to know enums well enough
 * to walk them and still keep the two layers apart.
 *
 * References:
 * - PHPStan phpdoc-types: value-of<BackedEnum>
 * - Psalm utility_types.md: value-of on backed enums
 */

enum Suit: string
{
    case Hearts = 'hearts';
    case Spades = 'spades';
}

/**
 * @return value-of<Suit>
 */
function returnsSuitValue() // T: value-of<Suit>
{
    return \random_int(0, 1) === 1 ? 'hearts' : 'spades'; // V
}

function acceptsString(string $value): void
{
}

/**
 * @param value-of<Suit> $value
 */
function acceptsSuitValue($value): void // T: value-of<Suit>
{
}

// Every backing value of the enum is a string.
acceptsString(returnsSuitValue()); // V

// Both backing values satisfy the parameter.
acceptsSuitValue('hearts'); // V
acceptsSuitValue('spades'); // V

// A string that backs no case does not.
acceptsSuitValue('clubs'); // E?: 'clubs' backs no case of Suit

// Neither does the case object itself: the type is the backing values, not
// the cases.
acceptsSuitValue(Suit::Hearts); // E?: a case object is not one of its backing values