Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/VM/BackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public static function caseForValue(ClassEntry $enum, Variable $value): ?EnumCas
}
EnumSupport::ensureBackedEnumValuesUnique($enum);
$normalized = self::normalizeBackingArgument($enum, $value->resolveIndirect());
$match = self::matchCaseForBackingValue($enum, $normalized);
if (null !== $match) {
return $match;
}

return self::matchCaseForBackingValueFromConstants($enum, $normalized);
}

private static function matchCaseForBackingValue(ClassEntry $enum, Variable $normalized): ?EnumCaseEntry
{
foreach ($enum->enumCases as $case) {
$backing = self::caseBackingScalar($enum->backedType, $case['value']);
if (!self::backingValuesMatch($enum->backedType, $backing, $normalized)) {
Expand All @@ -42,6 +52,31 @@ public static function caseForValue(ClassEntry $enum, Variable $value): ?EnumCas
return null;
}

/**
* Fallback when {@see ClassEntry::$enumCases} is empty or stale but case constants remain (#9603).
*
* @see Zend/zend_enum.c — zend_enum_from_case() backed-value hash
*/
private static function matchCaseForBackingValueFromConstants(
ClassEntry $enum,
Variable $normalized
): ?EnumCaseEntry {
foreach ($enum->constants as $memberLc => $stored) {
$caseName = EnumSupport::enumCaseNameForConstantMember($enum, $memberLc);
if (null === $caseName) {
continue;
}
$backing = self::caseBackingScalar($enum->backedType, $stored);
if (!self::backingValuesMatch($enum->backedType, $backing, $normalized)) {
continue;
}

return new EnumCaseEntry($enum, $caseName, clone $backing);
}

return null;
}

/**
* Canonical enum case variable for a matched case name (Zend singleton identity, #5533).
*/
Expand Down
9 changes: 5 additions & 4 deletions lib/VM/EnumFromHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ public function __construct(

public function execute(Frame $frame): void
{
EnumSupport::ensureBackedEnumValuesUnique($this->enum);
$enum = EnumSupport::resolveRuntimeEnumClass($frame->vmContext, $this->enum);
EnumSupport::ensureBackedEnumValuesUnique($enum);
if (\count($frame->calledArgs) < 1) {
throw new \LogicException(
$this->getName().'() requires exactly 1 argument in this compiler build'
);
}
$arg = $frame->calledArgs[0];
$match = BackedEnum::caseForValue($this->enum, $arg);
$match = BackedEnum::caseForValue($enum, $arg);
if (null === $match) {
if (!$this->try) {
throw new \ValueError(BackedEnum::valueErrorMessage($this->enum, $arg));
throw new \ValueError(BackedEnum::valueErrorMessage($enum, $arg));
}
if (null !== $frame->returnVar) {
$frame->returnVar->null();
Expand All @@ -43,7 +44,7 @@ public function execute(Frame $frame): void
return;
}
if (null !== $frame->returnVar) {
$canonical = BackedEnum::canonicalCaseVariable($this->enum, $match->caseName);
$canonical = BackedEnum::canonicalCaseVariable($enum, $match->caseName);
if (null !== $canonical && EnumCaseSupport::isEnumCaseVariable($canonical)) {
$frame->returnVar->copyFrom($canonical);
} else {
Expand Down
14 changes: 14 additions & 0 deletions test/repro/issue_9603_enum_from.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
enum E: string {
case A = 'a';
}

var_dump(E::tryFrom('b')); // Zend: NULL
var_dump(E::tryFrom('a')); // Zend: enum(E::A)

try {
E::from('a');
echo "from ok\n";
} catch (ValueError $e) {
echo 'from fail: ', $e->getMessage(), "\n";
}
23 changes: 23 additions & 0 deletions test/unit/VmEnumCanonicalCaseVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,27 @@ enum E: string { case A = 'x'; case B = 'y'; }
$this->assertNotNull($canonical);
$this->assertTrue(EnumCaseSupport::isEnumCaseVariable($canonical));
}

/** Issue #9603 — from()/tryFrom() must resolve via constants when enumCases table is empty. */
public function testFromResolvesWhenEnumCasesTableEmpty(): void
{
$runtime = new Runtime();
$runtime->run($runtime->parseAndCompile(<<<'PHP'
<?php
enum E: string { case A = 'x'; case B = 'y'; }
PHP, 'enum_decl.php'));
$enum = $runtime->vmContext->classes['e'];
$this->assertNotEmpty($enum->enumCases);
$enum->enumCases = [];

ob_start();
$runtime->run($runtime->parseAndCompile(<<<'PHP'
<?php
echo E::tryFrom('x')->name;
echo E::from('y')->name;
PHP, 'enum_from_empty_cases.php'));
$output = ob_get_clean();

$this->assertSame('AB', $output);
}
}
29 changes: 29 additions & 0 deletions test/unit/VmEnumFromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,33 @@ enum Level: int { case Low = 1; }
$this->expectExceptionMessage('Level::from(): Argument #1 ($value) must be of type int, string given');
$runtime->run($block);
}

/** Issue #9603 — exact maintainer repro: valid backing values resolve to enum cases. */
public function testIssue9603EnumFromTryFromRepro(): void
{
$code = <<<'PHP'
<?php
enum E: string {
case A = 'a';
}

var_export(E::tryFrom('b') === null);
echo "\n";
var_export(E::tryFrom('a')->name);
echo "\n";
try {
E::from('a');
echo "from ok\n";
} catch (ValueError $e) {
echo 'from fail: ', $e->getMessage(), "\n";
}
PHP;
$runtime = new Runtime();
$block = $runtime->parseAndCompile($code, 'issue_9603_enum_from.php');
ob_start();
$runtime->run($block);
$output = ob_get_clean();

$this->assertSame("true\n'A'\nfrom ok\n", $output);
}
}