← 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.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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. [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. [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.
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.
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
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.57.0mago 1.57.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.87.0mir 0.87.0 Enforced
phpdoc_advanced_fallback_value_of_enum.php:54: InvalidArgument: Argument $value of acceptsSuitValue() expects '"hearts"|"spades"', got '"clubs"' [MIR0201]
phpdoc_advanced_fallback_value_of_enum.php:58: InvalidArgument: Argument $value of acceptsSuitValue() expects '"hearts"|"spades"', got 'Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit::Hearts' [MIR0201]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpantom 0.11.1phpantom_lsp 0.11.1 Enforced
phpdoc_advanced_fallback_value_of_enum.php:54: Argument 1 ($value) expects 'hearts'|'spades', got 'clubs' [type_mismatch_argument]
phpdoc_advanced_fallback_value_of_enum.php:58: Argument 1 ($value) expects 'hearts'|'spades', got Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\Suit [type_mismatch_argument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Not enforced

No diagnostics reported.

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

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
qodana 262.10968.76PhpStorm 262.10968.76 Widened to string
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

Notes: Checked as plain `string`, the enum's backing type: `'clubs'` passes, and the `Suit::Hearts` rejection is a case object failing `string` ("Expected parameter of type 'string'"), not `value-of` being evaluated.

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.8steins 0.1.8 (2026-09-21 revision 76306a9) 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.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Enforced
phpdoc_advanced_fallback_value_of_enum.php:54: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackValueOfEnum\acceptsSuitValue expects 'hearts'|'spades', 'clubs' given.
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.
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 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