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
6 changes: 5 additions & 1 deletion lib/VM.php
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,10 @@ private function runFrames(): int
}
$propertyObject = $var->toObject();
VM\LazyObjectSupport::ensureInitialized($this, $propertyObject);
if (EnumCaseSupport::isEnumCase($propertyObject)) {
$result->copyFrom(EnumCaseSupport::getProperty($propertyObject, $name));
break;
}
$hookValue = $this->fetchPropertyWithHooks($propertyObject, $name, $frame);
if (null !== $hookValue) {
$result->copyFrom($hookValue);
Expand Down Expand Up @@ -2375,7 +2379,7 @@ private function isPropertyHookRawWrite(Frame $frame, string $propName): bool
$methodLc = strtolower($func->name);
$wantSet = strtolower(SourcePreprocessor\PropertyHooks::setHookMethodName($propName));

return $methodLc === $wantSet || $methodLc === strtolower($func->class.'::'.$wantSet);
return $methodLc === $wantSet || $methodLc === strtolower($func->class->value.'::'.$wantSet);
}

private function linkPropertyHooks(ClassEntry $entry, VM\ClassProperty $prop): void
Expand Down
5 changes: 3 additions & 2 deletions lib/VM/EnumCaseSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ public static function getProperty(ObjectEntry $object, string $name): Variable
throw new \LogicException('getProperty called on non-enum-case object');
}
$result = new Variable();
if ('name' === $name) {
$lc = strtolower($name);
if ('name' === $lc) {
$result->string($object->enumCaseName ?? '');

return $result;
}
if ('value' === $name) {
if ('value' === $lc) {
if (null === $object->enumCaseValue) {
throw new \LogicException('Cannot read value on a unit enum case');
}
Expand Down
2 changes: 2 additions & 0 deletions test/compliance/EnumVMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static function providePHPTests(): \Generator
'enum_instanceof.phpt',
'enum_implements_metadata.phpt',
'enum_static_method.phpt',
'enum_user_method.phpt',
'enum_backed_user_method.phpt',
] as $file
) {
yield $file => self::parsePHPT(
Expand Down
16 changes: 16 additions & 0 deletions test/compliance/cases/language/enum_backed_user_method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Language: backed enum instance method (#3390)
--FILE--
<?php
enum Status: string {
case Active = 'active';
public function label(): string {
return $this->name;
}
}
echo Status::Active->label();
echo "\n";
echo Status::Active;
--EXPECT--
Active
active
17 changes: 17 additions & 0 deletions test/compliance/cases/language/enum_user_method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Language: unit enum instance method and $this->name (#3390)
--FILE--
<?php
enum Status {
case Ok;
case Err;
public function label(): string {
return $this->name;
}
}
echo Status::Ok->label();
echo "\n";
echo Status::Err->label();
--EXPECT--
Ok
Err