PHP 8.0.0 adds the \Stringable interface, which assures the existence of the __toString magic method on any class that implements it. If MarkupInterface were to extend it, this would mark all Markup objects as being safely castable to string, allowing Drupal-independent code to accept/return it anywhere string|\Stringable is type-hinted.

Arguments for making this change in a D9 minor version:

- Even though Drupal 9 does not yet require PHP 8, we already have access to \Stringable via the symfony/polyfill-php80 package
- \Stringable is exactly equivalent to MarkupInterface in that both only contain the __toString() method, so extending it doesn't alter any contracts or break BC.

CommentFileSizeAuthor
#2 3254863-2.patch496 bytescburschka

Comments

cburschka created an issue. See original summary.

cburschka’s picture

Status: Active » Needs review
StatusFileSize
new496 bytes

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

This seems valid and correct. Will let committers decide.

catch’s picture

So this is good, but there ought to be some places we can change type hints from string|MarkupInterface to string|Stringable in core too? I think we should open a plan issue to discuss. That doesn't detract from adding the interface to MarkupInterface itself so it can be passed to methods that already type hint string|Stringable.

quietone’s picture

Issue tags: +Needs followup

Adding tag for #6

  • catch committed 17aefffe on 10.1.x
    Issue #3254863 by cburschka: MarkupInterface should extend \Stringable
    
catch’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs followup
wim leers’s picture

🙏 Yay! This has gotten in the way for me before — this would've made that painless instead of painful 😊

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.