{"id":19324868,"url":"https://github.com/spatie/backtrace","last_synced_at":"2025-05-14T04:08:10.845Z","repository":{"id":42447027,"uuid":"314528792","full_name":"spatie/backtrace","owner":"spatie","description":"A better backtrace","archived":false,"fork":false,"pushed_at":"2025-05-12T20:44:21.000Z","size":147,"stargazers_count":387,"open_issues_count":0,"forks_count":27,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-12T21:42:53.140Z","etag":null,"topics":["debugging","php"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2020-11-20T11:03:26.000Z","updated_at":"2025-05-12T20:44:17.000Z","dependencies_parsed_at":"2024-11-05T22:03:41.971Z","dependency_job_id":"1df2447b-db47-4301-93ff-15ec52a71872","html_url":"https://github.com/spatie/backtrace","commit_stats":{"total_commits":112,"total_committers":14,"mean_commits":8.0,"dds":0.6160714285714286,"last_synced_commit":"ecdad8b0c7e88709d71ead1ef36771007a2713cd"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":"spatie/package-skeleton-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fbacktrace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fbacktrace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fbacktrace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fbacktrace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/backtrace/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254046274,"owners_count":22005567,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["debugging","php"],"created_at":"2024-11-10T02:07:16.915Z","updated_at":"2025-05-14T04:08:05.807Z","avatar_url":"https://github.com/spatie.png","language":"PHP","readme":"# A better PHP backtrace\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/backtrace.svg?style=flat-square)](https://packagist.org/packages/spatie/backtrace)\n![Tests](https://github.com/spatie/backtrace/workflows/Tests/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/backtrace.svg?style=flat-square)](https://packagist.org/packages/spatie/backtrace)\n\nTo get the backtrace in PHP you can use the `debug_backtrace` function. By default, it can be hard to work with. The\nreported function name for a frame is skewed: it belongs to the previous frame. Also, options need to be passed using a bitmask.\n\nThis package provides a better way than `debug_backtrace` to work with a back trace. Here's an example:\n\n```php\n// returns an array with `Spatie\\Backtrace\\Frame` instances\n$frames = Spatie\\Backtrace\\Backtrace::create()-\u003eframes(); \n\n$firstFrame = $frames[0];\n\n$firstFrame-\u003efile; // returns the file name\n$firstFrame-\u003elineNumber; // returns the line number\n$firstFrame-\u003eclass; // returns the class name\n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/backtrace.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/backtrace)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can\nsupport us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.\nYou'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards\non [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/backtrace\n```\n\n## Usage\n\nThis is how you can create a backtrace instance:\n\n```php\n$backtrace = Spatie\\Backtrace\\Backtrace::create();\n```\n\n### Getting the frames\n\nTo get all the frames you can call `frames`.\n\n```php\n$frames = $backtrace-\u003eframes(); // contains an array with `Spatie\\Backtrace\\Frame` instances\n```\n\nA `Spatie\\Backtrace\\Frame` has these properties:\n\n- `file`: the name of the file\n- `lineNumber`: the line number\n- `arguments`: the arguments used for this frame. Will be `null` if `withArguments` was not used.\n- `class`: the class name for this frame. Will be `null` if the frame concerns a function.\n- `method`: the method used in this frame\n- `applicationFrame`: contains `true` is this frame belongs to your application, and `false` if it belongs to a file in\n  the vendor directory\n\n### Collecting arguments\n\nFor performance reasons, the frames of the back trace will not contain the arguments of the called functions. If you\nwant to add those use the `withArguments` method.\n\n```php\n$backtrace = Spatie\\Backtrace\\Backtrace::create()-\u003ewithArguments();\n```\n\n#### Reducing arguments\n\nFor viewing purposes, arguments can be reduced to a string:\n\n```php\n$backtrace = Spatie\\Backtrace\\Backtrace::create()-\u003ewithArguments()-\u003ereduceArguments();\n```\n\nBy default, some typical types will be reduced to a string. You can define your own reduction algorithm per type by implementing an `ArgumentReducer`:\n\n```php\nclass DateTimeWithOtherFormatArgumentReducer implements ArgumentReducer\n{\n    public function execute($argument): ReducedArgumentContract\n    {\n        if (! $argument instanceof DateTimeInterface) {\n            return UnReducedArgument::create();\n        }\n\n        return new ReducedArgument(\n            $argument-\u003eformat('d/m/y H:i'),\n            get_class($argument),\n        );\n    }\n}\n```\n\nThis is a copy of the built-in argument reducer for `DateTimeInterface` where we've updated the format. An `UnReducedArgument` object is returned when the argument is not of the expected type. A `ReducedArgument` object is returned with the reduced value of the argument and the original type of the argument.\n\nThe reducer can be used as such:\n\n```php\n$backtrace = Spatie\\Backtrace\\Backtrace::create()-\u003ewithArguments()-\u003ereduceArguments(\n    Spatie\\Backtrace\\Arguments\\ArgumentReducers::default([\n        new DateTimeWithOtherFormatArgumentReducer()\n    ])\n);\n```\n\nWhich will first execute the new reducer and then the default ones.\n\n### Setting the application path\n\nYou can use the `applicationPath` to pass the base path of your app. This value will be used to determine whether a\nframe is an application frame, or a vendor frame. Here's an example using a Laravel specific function.\n\n```php\n$backtrace = Spatie\\Backtrace\\Backtrace::create()-\u003eapplicationPath(base_path());\n```\n### Removing the application path from the file name\n\nYou can use `trimFilePaths` to remove the base path of your app from the file. This will only work if you use it in conjunction with the `applicationPath` method re above. Here's an example using a Laravel specific function. This will ensure the Frame has the trimmedFilePath property set.\n\n```php\n$backtrace = Backtrace::create()-\u003eapplicationPath(base_path())-\u003etrimFilePaths());\n```\n\n### Getting a certain part of a trace\n\nIf you only want to have the frames starting from a particular frame in the backtrace you can use\nthe `startingFromFrame` method:\n\n```php\nuse Spatie\\Backtrace\\Backtrace;\nuse Spatie\\Backtrace\\Frame;\n\n$frames = Backtrace::create()\n    -\u003estartingFromFrame(function (Frame $frame) {\n        return $frame-\u003eclass === MyClass::class;\n    })\n    -\u003eframes();\n```\n\nWith this code, all frames before the frame that concerns `MyClass` will have been filtered out.\n\nAlternatively, you can use the `offset` method, which will skip the given number of frames. In this example the first 2 frames will not end up in `$frames`.\n\n```php\n$frames = Spatie\\Backtrace\\Backtrace::create()\n    -\u003eoffset(2)\n    -\u003eframes();\n```\n\n### Limiting the number of frames\n\nTo only get a specific number of frames use the `limit` function. In this example, we'll only get the first two frames.\n\n```php\n$frames = Spatie\\Backtrace\\Backtrace::create()\n    -\u003elimit(2)\n    -\u003eframes();\n```\n\n###  Getting a backtrace for a throwable\n\nHere's how you can get a backtrace for a throwable.\n\n```php\n$frames = Spatie\\Backtrace\\Backtrace::createForThrowable($throwable)\n```\n\nBecause we will use the backtrace that is already available the throwable, the frames will always contain the arguments used.\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Freek Van de Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fbacktrace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fbacktrace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fbacktrace/lists"}