Skip to content

Suggestion - debug_backtrace() replacements #25

Description

@langabi

Suggestion, for utility functions - I put them in Trace.php. Almost-dropin replacements for PHP debug_backtrace() and debug_print_backtrace(), useful for retrieving backtrace without having to throw a fatal exception. Happy to create as pull request, but quite possible I missed something vital in writing these.

    /**
     * Replacement for debug_print_backtrace() PHP function. Returns the current stack as a string
     * 
     * Call as `yield Trace::debug_print_backtrace();`
     */
    public static function debug_print_backtrace() : \Generator
    {
        /** @var Trace $trace */
        $trace = \class_exists(\Recoil\Dev\Instrumentation\Trace::class) ? yield \Recoil\Dev\Instrumentation\Trace::install() : null;
        $e = new Exception();
        $trace->updateStackTrace($e);
        return $e->getTraceAsString();
    }

    /**
     * Replacement for debug_backtrace() PHP function. Returns the current stack as an array
     * 
     * Call as `$backtrace = yield Trace::debug_backtrace();`
     */
    public static function debug_backtrace() : \Generator
    {
        /** @var Trace $trace */
        $trace = \class_exists(\Recoil\Dev\Instrumentation\Trace::class) ? yield \Recoil\Dev\Instrumentation\Trace::install() : null;
        $e = new Exception();
        $trace->updateStackTrace($e);
        return $e->getTrace();
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions