Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. nh-mike revised this gist Apr 4, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion flattenExceptionBacktrace.php
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ function flattenExceptionBacktrace(\Throwable $exception) {
    array_walk_recursive($call['args'], $flatten);
    }
    $traceProperty->setValue($exception, $trace);
    } while($nextexception = $exception->getPrevious());
    } while($previousexception = $exception->getPrevious());

    $traceProperty->setAccessible(false);
    }
  2. nh-mike revised this gist Apr 4, 2019. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion flattenExceptionBacktrace.php
    Original file line number Diff line number Diff line change
    @@ -22,13 +22,18 @@ function flattenExceptionBacktrace(\Throwable $exception) {
    }
    };

    $previousexception = $exception;
    do {
    if ($previousexception === NULL) {
    break;
    }
    $exception = $previousexception;
    $trace = $traceProperty->getValue($exception);
    foreach($trace as &$call) {
    array_walk_recursive($call['args'], $flatten);
    }
    $traceProperty->setValue($exception, $trace);
    } while($exception = $exception->getPrevious());
    } while($nextexception = $exception->getPrevious());

    $traceProperty->setAccessible(false);
    }
  3. nh-mike revised this gist Mar 28, 2019. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions flattenExceptionBacktrace.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    <?php
    function flattenExceptionBacktrace(\Exception $exception) {
    $traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace');
    function flattenExceptionBacktrace(\Throwable $exception) {
    if ($exception instanceof \Exception) {
    $traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace');
    } else {
    $traceProperty = (new \ReflectionClass('Error'))->getProperty('trace');
    }
    $traceProperty->setAccessible(true);

    $flatten = function(&$value, $key) {
  4. @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());
  5. @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);

  6. @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);
    }