Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save devnix/11932a649c40541c5cde7cd9ba4c2933 to your computer and use it in GitHub Desktop.

Select an option

Save devnix/11932a649c40541c5cde7cd9ba4c2933 to your computer and use it in GitHub Desktop.

Revisions

  1. @Thinkscape Thinkscape revised this gist Mar 6, 2015. 1 changed file with 16 additions and 9 deletions.
    25 changes: 16 additions & 9 deletions flattenExceptionBacktrace.php
    Original file line number Diff line number Diff line change
    @@ -3,18 +3,25 @@ function flattenExceptionBacktrace(\Exception $exception) {
    $traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace');
    $traceProperty->setAccessible(true);

    $flatten = function(&$value, $key) {
    if ($value instanceof \Closure) {
    $closureReflection = new \ReflectionFunction($value);
    $value = sprintf(
    '(Closure at %s:%s)',
    $closureReflection->getFileName(),
    $closureReflection->getStartLine()
    );
    } elseif (is_object($value)) {
    $value = sprintf('object(%s)', get_class($value));
    } elseif (is_resource($value)) {
    $value = sprintf('resource(%s)', get_resource_type($value));
    }
    };

    do {
    $trace = $traceProperty->getValue($exception);
    foreach($trace as &$call) {
    foreach($call['args'] as $key => $value) {
    if ($value instanceof \Closure) {
    $call['args'][$key] = '(Closure)';
    } elseif (is_object($value) && !method_exists($value, '__toString')) {
    $call['args'][$key] = sprintf('object(%s)', get_class($value));
    } elseif (is_resource($value)) {
    $call['args'][$key] = sprintf('resource(%s)', get_resource_type($value));
    }
    }
    array_walk_recursive($call['args'], $flatten);
    }
    $traceProperty->setValue($exception, $trace);
    } while($exception = $exception->getPrevious());
  2. @Thinkscape Thinkscape renamed this gist Mar 6, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion flattenExceptionBacktrace → flattenExceptionBacktrace.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    protected static function flattenExceptionBacktrace(Exception $exception) {
    <?php
    function flattenExceptionBacktrace(\Exception $exception) {
    $traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace');
    $traceProperty->setAccessible(true);

  3. @Thinkscape Thinkscape created this gist Mar 6, 2015.
    22 changes: 22 additions & 0 deletions flattenExceptionBacktrace
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    protected static function flattenExceptionBacktrace(Exception $exception) {
    $traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace');
    $traceProperty->setAccessible(true);

    do {
    $trace = $traceProperty->getValue($exception);
    foreach($trace as &$call) {
    foreach($call['args'] as $key => $value) {
    if ($value instanceof \Closure) {
    $call['args'][$key] = '(Closure)';
    } elseif (is_object($value) && !method_exists($value, '__toString')) {
    $call['args'][$key] = sprintf('object(%s)', get_class($value));
    } elseif (is_resource($value)) {
    $call['args'][$key] = sprintf('resource(%s)', get_resource_type($value));
    }
    }
    }
    $traceProperty->setValue($exception, $trace);
    } while($exception = $exception->getPrevious());

    $traceProperty->setAccessible(false);
    }