├── ChangeLog.md ├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src └── Wizard.php /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to `sebastianbergmann/code-unit-reverse-lookup` are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. 4 | 5 | ## [4.0.1] - 2024-07-03 6 | 7 | ### Changed 8 | 9 | * This project now uses PHPStan instead of Psalm for static analysis 10 | 11 | ## [4.0.0] - 2024-02-02 12 | 13 | ### Removed 14 | 15 | * This component is no longer supported on PHP 8.1 16 | 17 | ## [3.0.0] - 2023-02-03 18 | 19 | ### Removed 20 | 21 | * This component is no longer supported on PHP 7.3, PHP 7.4, and PHP 8.0 22 | 23 | ## [2.0.3] - 2020-09-28 24 | 25 | ### Changed 26 | 27 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 28 | 29 | ## [2.0.2] - 2020-06-26 30 | 31 | ### Added 32 | 33 | * This component is now supported on PHP 8 34 | 35 | ## [2.0.1] - 2020-06-15 36 | 37 | ### Changed 38 | 39 | * Tests etc. are now ignored for archive exports 40 | 41 | ## 2.0.0 - 2020-02-07 42 | 43 | ### Removed 44 | 45 | * This component is no longer supported on PHP 5.6, PHP 7.0, PHP 7.1, and PHP 7.2 46 | 47 | ## 1.0.0 - 2016-02-13 48 | 49 | ### Added 50 | 51 | * Initial release 52 | 53 | [4.0.1]: https://github.com/sebastianbergmann/code-unit-reverse-lookup/compare/4.0.0...4.0.1 54 | [4.0.0]: https://github.com/sebastianbergmann/code-unit-reverse-lookup/compare/3.0...4.0.0 55 | [3.0.0]: https://github.com/sebastianbergmann/code-unit-reverse-lookup/compare/2.0.3...3.0.0 56 | [2.0.3]: https://github.com/sebastianbergmann/code-unit-reverse-lookup/compare/2.0.2...2.0.3 57 | [2.0.2]: https://github.com/sebastianbergmann/code-unit-reverse-lookup/compare/2.0.1...2.0.2 58 | [2.0.1]: https://github.com/sebastianbergmann/code-unit-reverse-lookup/compare/2.0.0...2.0.1 59 | [2.0.0]: https://github.com/sebastianbergmann/code-unit-reverse-lookup/compare/1.0.0...2.0.0 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2016-2025, Sebastian Bergmann 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![No Maintenance Intended](https://unmaintained.tech/badge.svg)](https://unmaintained.tech/) 2 | [![Latest Stable Version](https://poser.pugx.org/sebastian/code-unit-reverse-lookup/v)](https://packagist.org/packages/sebastian/code-unit-reverse-lookup) 3 | [![CI Status](https://github.com/sebastianbergmann/code-unit-reverse-lookup/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/code-unit-reverse-lookup/actions) 4 | [![codecov](https://codecov.io/gh/sebastianbergmann/code-unit-reverse-lookup/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/code-unit-reverse-lookup) 5 | 6 | # sebastian/code-unit-reverse-lookup 7 | 8 | Looks up which function or method a line of code belongs to. 9 | 10 | ## Installation 11 | 12 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 13 | 14 | ``` 15 | composer require sebastian/code-unit-reverse-lookup 16 | ``` 17 | 18 | If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: 19 | 20 | ``` 21 | composer require --dev sebastian/code-unit-reverse-lookup 22 | ``` 23 | 24 | Please note that this is now a [low maintenance project](https://github.com/sebastianbergmann/code-unit-reverse-lookup/blob/main/.github/CONTRIBUTING.md#low-maintenance-project). 25 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure. 4 | 5 | **Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** 6 | 7 | Instead, please email `sebastian@phpunit.de`. 8 | 9 | Please include as much of the information listed below as you can to help us better understand and resolve the issue: 10 | 11 | * The type of issue 12 | * Full paths of source file(s) related to the manifestation of the issue 13 | * The location of the affected source code (tag/branch/commit or direct URL) 14 | * Any special configuration required to reproduce the issue 15 | * Step-by-step instructions to reproduce the issue 16 | * Proof-of-concept or exploit code (if possible) 17 | * Impact of the issue, including how an attacker might exploit the issue 18 | 19 | This information will help us triage your report more quickly. 20 | 21 | ## Web Context 22 | 23 | The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit. 24 | 25 | The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes. 26 | 27 | If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context. 28 | 29 | Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes. 30 | 31 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/code-unit-reverse-lookup", 3 | "description": "Looks up which function or method a line of code belongs to", 4 | "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", 5 | "license": "BSD-3-Clause", 6 | "authors": [ 7 | { 8 | "name": "Sebastian Bergmann", 9 | "email": "sebastian@phpunit.de" 10 | } 11 | ], 12 | "support": { 13 | "issues": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/issues", 14 | "security": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/security/policy" 15 | }, 16 | "config": { 17 | "platform": { 18 | "php": "8.2.0" 19 | }, 20 | "optimize-autoloader": true, 21 | "sort-packages": true 22 | }, 23 | "prefer-stable": true, 24 | "require": { 25 | "php": ">=8.2" 26 | }, 27 | "require-dev": { 28 | "phpunit/phpunit": "^11.3" 29 | }, 30 | "autoload": { 31 | "classmap": [ 32 | "src/" 33 | ] 34 | }, 35 | "extra": { 36 | "branch-alias": { 37 | "dev-main": "4.0-dev" 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Wizard.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\CodeUnitReverseLookup; 11 | 12 | use function array_merge; 13 | use function assert; 14 | use function class_exists; 15 | use function get_declared_classes; 16 | use function get_declared_traits; 17 | use function get_defined_functions; 18 | use function is_int; 19 | use function is_string; 20 | use function range; 21 | use function trait_exists; 22 | use ReflectionClass; 23 | use ReflectionFunction; 24 | use ReflectionMethod; 25 | 26 | final class Wizard 27 | { 28 | /** 29 | * @var array> 30 | */ 31 | private array $lookupTable = []; 32 | 33 | /** 34 | * @var array 35 | */ 36 | private array $processedClasses = []; 37 | 38 | /** 39 | * @var array 40 | */ 41 | private array $processedFunctions = []; 42 | 43 | public function lookup(string $filename, int $lineNumber): string 44 | { 45 | if (!isset($this->lookupTable[$filename][$lineNumber])) { 46 | $this->updateLookupTable(); 47 | } 48 | 49 | if (isset($this->lookupTable[$filename][$lineNumber])) { 50 | return $this->lookupTable[$filename][$lineNumber]; 51 | } 52 | 53 | return $filename . ':' . $lineNumber; 54 | } 55 | 56 | private function updateLookupTable(): void 57 | { 58 | $this->processClassesAndTraits(); 59 | $this->processFunctions(); 60 | } 61 | 62 | private function processClassesAndTraits(): void 63 | { 64 | $classes = get_declared_classes(); 65 | $traits = get_declared_traits(); 66 | 67 | foreach (array_merge($classes, $traits) as $classOrTrait) { 68 | assert(class_exists($classOrTrait) || trait_exists($classOrTrait)); 69 | 70 | if (isset($this->processedClasses[$classOrTrait])) { 71 | continue; 72 | } 73 | 74 | foreach ((new ReflectionClass($classOrTrait))->getMethods() as $method) { 75 | $this->processFunctionOrMethod($method); 76 | } 77 | 78 | $this->processedClasses[$classOrTrait] = true; 79 | } 80 | } 81 | 82 | private function processFunctions(): void 83 | { 84 | foreach (get_defined_functions()['user'] as $function) { 85 | if (isset($this->processedFunctions[$function])) { 86 | continue; 87 | } 88 | 89 | $this->processFunctionOrMethod(new ReflectionFunction($function)); 90 | 91 | $this->processedFunctions[$function] = true; 92 | } 93 | } 94 | 95 | private function processFunctionOrMethod(ReflectionFunction|ReflectionMethod $functionOrMethod): void 96 | { 97 | if ($functionOrMethod->isInternal()) { 98 | return; 99 | } 100 | 101 | $name = $functionOrMethod->getName(); 102 | 103 | if ($functionOrMethod instanceof ReflectionMethod) { 104 | $name = $functionOrMethod->getDeclaringClass()->getName() . '::' . $name; 105 | } 106 | 107 | $fileName = $functionOrMethod->getFileName(); 108 | 109 | assert(is_string($fileName)); 110 | 111 | if (!isset($this->lookupTable[$fileName])) { 112 | $this->lookupTable[$fileName] = []; 113 | } 114 | 115 | $startLine = $functionOrMethod->getStartLine(); 116 | $endLine = $functionOrMethod->getEndLine(); 117 | 118 | assert(is_int($startLine)); 119 | assert(is_int($endLine)); 120 | assert($endLine >= $startLine); 121 | 122 | foreach (range($startLine, $endLine) as $line) { 123 | $this->lookupTable[$fileName][$line] = $name; 124 | } 125 | } 126 | } 127 | --------------------------------------------------------------------------------