Skip to content

Commit

Permalink
Reflection: indicate final properties in string output
Browse files Browse the repository at this point in the history
Add "final" to the result of `_property_string()` when outputting the string
representation of a `ReflectionClass` or `ReflectionProperty` instance
  • Loading branch information
DanielEScherzer committed Feb 16, 2025
1 parent 18df1e4 commit 438de46
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,9 @@ static void _property_string(smart_str *str, zend_property_info *prop, const cha
if (!prop) {
smart_str_append_printf(str, "<dynamic> public $%s", prop_name);
} else {
if (prop->flags & ZEND_ACC_FINAL) {
smart_str_appends(str, "final ");
}
/* These are mutually exclusive */
switch (prop->flags & ZEND_ACC_PPP_MASK) {
case ZEND_ACC_PUBLIC:
Expand Down
2 changes: 1 addition & 1 deletion ext/reflection/tests/asymmetric_visibility_flags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool(true)
bool(false)
bool(true)
bool(false)
Property [ public private(set) int $bar ]
Property [ final public private(set) int $bar ]
bool(false)
bool(true)
bool(false)
Expand Down
42 changes: 42 additions & 0 deletions ext/reflection/tests/final_property_indicated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Output of properties indicates if they are final
--FILE--
<?php

class Demo {
final public $a;
public $b;
}

$class = new ReflectionClass( Demo::class );
echo $class;

$propA = new ReflectionProperty( Demo::class, 'a' );
echo $propA;

$propB = new ReflectionProperty( Demo::class, 'b' );
echo $propB;
?>
--EXPECTF--
Class [ <user> class Demo ] {
@@ %s %d-%d

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [0] {
}

- Properties [2] {
Property [ final public $a = NULL ]
Property [ public $b = NULL ]
}

- Methods [0] {
}
}
Property [ final public $a = NULL ]
Property [ public $b = NULL ]

0 comments on commit 438de46

Please sign in to comment.