Skip to content

Commit

Permalink
Fixed bug #81457
Browse files Browse the repository at this point in the history
When Reflection internally instantiates a ReflectionClass, it
should create a more specific ReflectionEnum instance if the
class is actually an enum.
  • Loading branch information
nikic committed Sep 20, 2021
1 parent 55582a2 commit ea11e79
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -2,6 +2,10 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.1.0RC3

- Reflection:
. Fixed bug #81457 (Enum: ReflectionMethod->getDeclaringClass() return a
ReflectionClass). (Nikita)

- XML:
. Fixed bug #70962 (XML_OPTION_SKIP_WHITE strips embedded whitespace).
(Aliaksandr Bystry, cmb)
Expand Down
18 changes: 4 additions & 14 deletions ext/reflection/php_reflection.c
Expand Up @@ -1231,7 +1231,9 @@ PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
{
reflection_object *intern;

reflection_instantiate(reflection_class_ptr, object);
zend_class_entry *reflection_ce =
ce->ce_flags & ZEND_ACC_ENUM ? reflection_enum_ptr : reflection_class_ptr;
reflection_instantiate(reflection_ce, object);
intern = Z_REFLECTION_P(object);
intern->ptr = ce;
intern->ref_type = REF_TYPE_OTHER;
Expand All @@ -1240,18 +1242,6 @@ PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
}
/* }}} */

static void zend_reflection_enum_factory(zend_class_entry *ce, zval *object)
{
reflection_object *intern;

reflection_instantiate(reflection_enum_ptr, object);
intern = Z_REFLECTION_P(object);
intern->ptr = ce;
intern->ref_type = REF_TYPE_OTHER;
intern->ce = ce;
ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
}

/* {{{ reflection_extension_factory */
static void reflection_extension_factory(zval *object, const char *name_str)
{
Expand Down Expand Up @@ -6849,7 +6839,7 @@ ZEND_METHOD(ReflectionEnumUnitCase, getEnum)
}
GET_REFLECTION_OBJECT_PTR(ref);

zend_reflection_enum_factory(ref->ce, return_value);
zend_reflection_class_factory(ref->ce, return_value);
}

ZEND_METHOD(ReflectionEnumBackedCase, __construct)
Expand Down
26 changes: 26 additions & 0 deletions ext/reflection/tests/bug81457.phpt
@@ -0,0 +1,26 @@
--TEST--
Bug #81457: Enum ReflectionMethod->getDeclaringClass() return a ReflectionClass
--FILE--
<?php

enum testEnum {
case A;
case B;

public function foo () {}
}

$re = new ReflectionEnum(testEnum::class);
$me = $re->getMethod('foo');

echo $me->getDeclaringClass()::class, "\n";

$rc = new ReflectionClass(testEnum::class);
$mc = $re->getMethod('foo');

echo $mc->getDeclaringClass()::class, "\n";

?>
--EXPECT--
ReflectionEnum
ReflectionEnum

0 comments on commit ea11e79

Please sign in to comment.