Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
CRAP
80.00% covered (warning)
80.00%
4 / 5
ToStringTrait
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
4.13
80.00% covered (warning)
80.00%
4 / 5
 __toString
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 _die
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 render
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
0 / 0
<?php
/**
 * @file
 * Contains \Drupal\Component\Utility\ToStringTrait.
 */
namespace Drupal\Component\Utility;
/**
 * Wraps __toString in a trait to avoid some fatals.
 */
trait ToStringTrait {
  /**
   * Implements the magic __toString() method.
   */
  public function __toString() {
    try {
      return (string) $this->render();
    }
    catch (\Exception $e) {
      // User errors in __toString() methods are considered fatal in the Drupal
      // error handler.
      trigger_error(get_class($e) . ' thrown while calling __toString on a ' . get_class($this) . ' object in ' . $e->getFile() . ' on line ' . $e->getLine() . ': ' . $e->getMessage(), E_USER_ERROR);
      // In case we are using another error handler that did not fatal on the
      // E_USER_ERROR, we terminate execution. However, for test purposes allow
      // a return value.
      return $this->_die();
    }
  }
  /**
   * For test purposes, wrap die() in an overridable method.
   */
  protected function _die() {
    die();
  }
  /**
   * Renders the object as a string.
   *
   * @return string|object
   *   The rendered string or an object implementing __toString().
   */
  abstract public function render();
}