Fixed XF\Phrase::__toString() does not always coerce output to string

Affected version
2.2.5

Xon

Well-known member
If XF\Phrase::allowHtml is true, and $options['fallback'] = \XF::Phrase(....) and $options['fallbackRaw'] = true then renderPhrase will return a non-string causing XF\Phrase::__toString() to fail with an "Method XF\Phrase::__toString() must return a string value" error.

Note; $allowHtml = false, then escapeString will coerce the output to a string.

I think a simple solution is to add an explicit strval call or (string) cast;
PHP:
public function __toString()
{
   try
   {
      return \strval($this->render());
 

Xon

Well-known member
Reproducing steps;

PHP:
$phrase = \XF::phrase('bad_phrase');
$phrase->fallback(\XF::phrase('fallback_phrase'), false);
$phrase = (string)$phrase;

$phrase = \XF::phrase('bad_phrase');
$phrase->fallback(\XF::phrase('fallback_phrase'), true);
$phrase = (string)$phrase;
The 2nd case will fail
 

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.6).

Change log:
Ensure phrases are properly returned as strings
There may be a delay before changes are rolled out to the XenForo Community.
 
Top