| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of sebastian/code-unit-reverse-lookup.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
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 get_declared_classes;
|
|
|
15 |
use function get_declared_traits;
|
|
|
16 |
use function get_defined_functions;
|
|
|
17 |
use function is_array;
|
|
|
18 |
use function range;
|
|
|
19 |
use ReflectionClass;
|
|
|
20 |
use ReflectionFunction;
|
|
|
21 |
use ReflectionFunctionAbstract;
|
|
|
22 |
use ReflectionMethod;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* @since Class available since Release 1.0.0
|
|
|
26 |
*/
|
|
|
27 |
class Wizard
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
* @var array
|
|
|
31 |
*/
|
|
|
32 |
private $lookupTable = [];
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
private $processedClasses = [];
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @var array
|
|
|
41 |
*/
|
|
|
42 |
private $processedFunctions = [];
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* @param string $filename
|
|
|
46 |
* @param int $lineNumber
|
|
|
47 |
*
|
|
|
48 |
* @return string
|
|
|
49 |
*/
|
|
|
50 |
public function lookup($filename, $lineNumber)
|
|
|
51 |
{
|
|
|
52 |
if (!isset($this->lookupTable[$filename][$lineNumber])) {
|
|
|
53 |
$this->updateLookupTable();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
if (isset($this->lookupTable[$filename][$lineNumber])) {
|
|
|
57 |
return $this->lookupTable[$filename][$lineNumber];
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
return $filename . ':' . $lineNumber;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
private function updateLookupTable(): void
|
|
|
64 |
{
|
|
|
65 |
$this->processClassesAndTraits();
|
|
|
66 |
$this->processFunctions();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
private function processClassesAndTraits(): void
|
|
|
70 |
{
|
|
|
71 |
$classes = get_declared_classes();
|
|
|
72 |
$traits = get_declared_traits();
|
|
|
73 |
|
|
|
74 |
assert(is_array($classes));
|
|
|
75 |
assert(is_array($traits));
|
|
|
76 |
|
|
|
77 |
foreach (array_merge($classes, $traits) as $classOrTrait) {
|
|
|
78 |
if (isset($this->processedClasses[$classOrTrait])) {
|
|
|
79 |
continue;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$reflector = new ReflectionClass($classOrTrait);
|
|
|
83 |
|
|
|
84 |
foreach ($reflector->getMethods() as $method) {
|
|
|
85 |
$this->processFunctionOrMethod($method);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$this->processedClasses[$classOrTrait] = true;
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function processFunctions(): void
|
|
|
93 |
{
|
|
|
94 |
foreach (get_defined_functions()['user'] as $function) {
|
|
|
95 |
if (isset($this->processedFunctions[$function])) {
|
|
|
96 |
continue;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$this->processFunctionOrMethod(new ReflectionFunction($function));
|
|
|
100 |
|
|
|
101 |
$this->processedFunctions[$function] = true;
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
private function processFunctionOrMethod(ReflectionFunctionAbstract $functionOrMethod): void
|
|
|
106 |
{
|
|
|
107 |
if ($functionOrMethod->isInternal()) {
|
|
|
108 |
return;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$name = $functionOrMethod->getName();
|
|
|
112 |
|
|
|
113 |
if ($functionOrMethod instanceof ReflectionMethod) {
|
|
|
114 |
$name = $functionOrMethod->getDeclaringClass()->getName() . '::' . $name;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
if (!isset($this->lookupTable[$functionOrMethod->getFileName()])) {
|
|
|
118 |
$this->lookupTable[$functionOrMethod->getFileName()] = [];
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
foreach (range($functionOrMethod->getStartLine(), $functionOrMethod->getEndLine()) as $line) {
|
|
|
122 |
$this->lookupTable[$functionOrMethod->getFileName()][$line] = $name;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|