Don't implement Stringable on traits

Traits do not support interfaces, so we should not implement
Stringable on them.

Also check the __toString() return type in the same way other
magic methods do, otherwise we would now miss the check in the
trait case.
This commit is contained in:
Nikita Popov 2021-11-05 09:42:58 +01:00
parent 7e67366a9b
commit d478ae73b1
6 changed files with 25 additions and 10 deletions

View file

@ -2487,7 +2487,8 @@ ZEND_API zend_result zend_do_link_class(zend_class_entry *ce, zend_string *lc_pa
/* Normally Stringable is added during compilation. However, if it is imported from a trait,
* we need to explicilty add the interface here. */
if (ce->__tostring && !zend_class_implements_interface(ce, zend_ce_stringable)) {
if (ce->__tostring && !(ce->ce_flags & ZEND_ACC_TRAIT)
&& !zend_class_implements_interface(ce, zend_ce_stringable)) {
ZEND_ASSERT(ce->__tostring->common.fn_flags & ZEND_ACC_TRAIT_CLONE);
ce->ce_flags |= ZEND_ACC_RESOLVED_INTERFACES;
ce->num_interfaces++;