Verified Commit 671cd636 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3548957 Deprecate ToStringTrait

By: andypost
By: chi
By: dcam
By: amateescu
parent ff835241
Loading
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -331,12 +331,6 @@
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Component/Datetime/DateTimePlus.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Component\\\\Datetime\\\\DateTimePlus\\:\\:_die\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Component/Datetime/DateTimePlus.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Component\\\\Datetime\\\\DateTimePlus\\:\\:checkErrors\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
@@ -10147,12 +10141,6 @@
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/StreamWrapper/StreamWrapperManagerInterface.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\StringTranslation\\\\TranslatableMarkup\\:\\:_die\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\StringTranslation\\\\TranslationManager\\:\\:reset\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+1 −5
Original line number Diff line number Diff line
@@ -63,10 +63,6 @@ function _drupal_error_handler_real($error_level, $message, $filename, $line): v

    // We treat recoverable errors as fatal.
    $recoverable = $error_level == E_RECOVERABLE_ERROR;
    // As __toString() methods must not throw exceptions (recoverable errors)
    // in PHP, we allow them to trigger a fatal error by emitting a user error
    // using trigger_error().
    $to_string = $error_level == E_USER_ERROR && str_ends_with($caller['function'], '__toString()');
    _drupal_log_error([
      '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
      // The standard PHP error handler considers that the error messages
@@ -79,7 +75,7 @@ function _drupal_error_handler_real($error_level, $message, $filename, $line): v
      'backtrace' => $backtrace,
      '@backtrace_string' => (new \Exception())->getTraceAsString(),
      'exception' => NULL,
    ], $recoverable || $to_string);
    ], $recoverable);
  }
  // If the site is a test site then fail for user deprecations so they can be
  // caught by the deprecation error handler.
+11 −5
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\Component\Datetime;

use Drupal\Component\Utility\ToStringTrait;

/**
 * Wraps DateTime().
 *
@@ -37,9 +35,7 @@
 * @method int getTimestamp()
 * @method \DateTimeZone getTimezone()
 */
class DateTimePlus {

  use ToStringTrait;
class DateTimePlus implements \Stringable {

  const FORMAT = 'Y-m-d H:i:s';

@@ -682,4 +678,14 @@ public function getPhpDateTime() {
    return clone $this->dateTimeObject;
  }

  /**
   * Returns the string representation of this object.
   *
   * @return string
   *   The string representation.
   */
  public function __toString(): string {
    return (string) $this->render();
  }

}
+7 −0
Original line number Diff line number Diff line
@@ -2,8 +2,15 @@

namespace Drupal\Component\Utility;

@trigger_error('The ' . __NAMESPACE__ . '\ToStringTrait is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Implement the __toString() method directly, exception handling is no longer required. See https://www.drupal.org/node/3548961', E_USER_DEPRECATED);

/**
 * Wraps __toString in a trait to avoid some fatal errors.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Implement
 *   the __toString() method directly, exception handling is no longer required.
 *
 * @see https://www.drupal.org/node/3548961
 */
trait ToStringTrait {

+10 −3
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
namespace Drupal\Core\StringTranslation;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\ToStringTrait;

/**
 * Provides translatable markup class.
@@ -20,8 +19,6 @@
 */
class TranslatableMarkup extends FormattableMarkup {

  use ToStringTrait;

  /**
   * The translated markup without placeholder replacements.
   *
@@ -228,4 +225,14 @@ public function count(): int {
    return mb_strlen($this->render());
  }

  /**
   * Returns the string representation of this object.
   *
   * @return string
   *   The string representation.
   */
  public function __toString(): string {
    return (string) $this->render();
  }

}
Loading