Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Code Unit Reverse Lookup

Code Unit Reverse Lookup Laravel Package

sebastian/code-unit-reverse-lookup

Looks up which function or method a given line of code belongs to, a small utility from the PHPUnit ecosystem by Sebastian Bergmann. Install it via Composer as a project or dev dependency. Now a low maintenance project.

View on GitHub
Deep Wiki
Context7

Looks up which function or method a line of code belongs to

Frequently asked questions about Code Unit Reverse Lookup
What does sebastian/code-unit-reverse-lookup actually do?
It answers one question: given a file path and a line number, which function or method does that line belong to? The package is essentially a single class, SebastianBergmann\CodeUnitReverseLookup\Wizard, with a lookup($file, $line) method that reads the file, tokenizes it, and returns the name of the enclosing function or method. PHPUnit uses it through php-code-coverage to attribute covered lines to individual functions and methods, which is what makes per-method coverage reporting possible. There is no service provider, config file, facade, or any other Laravel integration surface.
How do I install sebastian/code-unit-reverse-lookup in a Laravel project?
Install it with Composer as a development dependency: composer require --dev sebastian/code-unit-reverse-lookup. There is nothing to register in config/app.php, no artisan command, and no publishing step, because it is a framework-agnostic library rather than a Laravel package. If you only want it because PHPUnit is already in your project, you probably do not need to install it explicitly at all.
Do I need to require it, or does PHPUnit already pull it in?
In most Laravel projects it is already present transitively: phpunit/phpunit depends on php-code-coverage, which depends on this package. Run composer why sebastian/code-unit-reverse-lookup before assuming it is missing, and avoid importing it from your own code unless you also declare it in composer.json. Relying on an undeclared package is fragile because a lockfile refresh can remove it without warning. Add it explicitly only if your own classes reference the Wizard.
Which Laravel and PHP versions is this package compatible with?
The package is framework-agnostic, so there is no Laravel version constraint to satisfy — it works anywhere Composer can load it, including Laravel 10 and 11. The only real compatibility variable is the PHP requirement declared in its composer.json against your runtime, so check that constraint rather than assuming it matches. Laravel 10 and 11 typically run on PHP 8.1 to 8.3, which is well within range for current releases, but projects pinned to older PHP versions should verify before installing.
How do I get the function or method name for a line of code with this package?
Instantiate the Wizard class and call lookup() with an absolute file path and an integer line number, for example (new Wizard())->lookup('/path/to/File.php', 42). The return value is a string identifying the enclosing function or method, and it comes back empty when no function or method encloses that line, such as at the top of a file or inside class-level property declarations. Because lookup() re-reads and tokenizes the file on every call, avoid calling it in a loop over thousands of lines without caching the results yourself.
Is it safe to use sebastian/code-unit-reverse-lookup in production?
It is safe in the sense that it makes no network calls, does no deserialization, and only reads local files, so the security exposure is negligible. The practical reason to keep it out of production is that it belongs in require-dev and has no business on a request path: each lookup() call reads and tokenizes a whole file. If you ever want it at runtime, for example to enrich exception logs with the enclosing method name, gate it to non-production or wrap it behind a cached index of file-to-method ranges.
Does the Wizard handle closures, traits, and anonymous classes correctly?
It is a small token-based implementation built for PHPUnit's coverage reporting needs, so treat its handling of edge cases like closure-bound methods, trait methods, arrow functions, and anonymous classes as best-effort rather than exhaustive. If you need precise attribution for those constructs, a parser-based approach is the safer foundation. Many teams keep a thin adapter in front of the Wizard precisely so the implementation can be swapped without touching call sites.
Is sebastian/code-unit-reverse-lookup abandoned or still maintained?
Neither abandoned nor actively developed: the repository carries a No Maintenance Intended badge and describes itself as a low-maintenance project. It is used by PHPUnit, so it keeps receiving the compatibility fixes required to keep the ecosystem working, but do not expect new features or a stable public API guarantee. Plan for it as a frozen dependency and pin a version if your own code calls it directly.
How should I test code that depends on this package?
Keep the vendor class behind a small adapter, something like App\Support\Source\MethodLocator, so the Wizard appears in exactly one place. That adapter can be swapped for a fake in tests and later reimplemented with nikic/php-parser or token_get_all without touching consumers. For the adapter's own tests, create fixture PHP files with known line numbers and assert the returned method names, including the empty-string case for lines outside any function.
What are the alternatives to sebastian/code-unit-reverse-lookup?
You can implement the same behaviour with PHP's built-in token_get_all(), which is roughly a day of work and removes the dependency entirely. If you need accurate handling of closures, arrow functions, traits, and anonymous classes, nikic/php-parser is the stronger foundation and is already common in Laravel-adjacent tooling. A final option is to do nothing and rely on the transitive copy that ships with PHPUnit, which is fine as long as your own code never references the class.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle
glavweb/extra-bundle
glavweb/api-bundle
glavweb/admin-bundle
adachsoft/api-version-bundle
elsayed85/laravel-api-exceptions
ouhssini/laravel-demo-mode
daworks/ncloud-outbound-mailer-driver
gp247/cms
ignibyte/aic-gates
nexa-merchant/blog
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
cgv/localized-resource