Wizard.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. use function array_merge;
  12. use function assert;
  13. use function get_declared_classes;
  14. use function get_declared_traits;
  15. use function get_defined_functions;
  16. use function is_array;
  17. use function range;
  18. use ReflectionClass;
  19. use ReflectionFunction;
  20. use ReflectionFunctionAbstract;
  21. use ReflectionMethod;
  22. class Wizard
  23. {
  24. /**
  25. * @psalm-var array<string,array<int,string>>
  26. */
  27. private array $lookupTable = [];
  28. /**
  29. * @psalm-var array<class-string,true>
  30. */
  31. private array $processedClasses = [];
  32. /**
  33. * @psalm-var array<string,true>
  34. */
  35. private array $processedFunctions = [];
  36. public function lookup(string $filename, int $lineNumber): string
  37. {
  38. if (!isset($this->lookupTable[$filename][$lineNumber])) {
  39. $this->updateLookupTable();
  40. }
  41. if (isset($this->lookupTable[$filename][$lineNumber])) {
  42. return $this->lookupTable[$filename][$lineNumber];
  43. }
  44. return $filename . ':' . $lineNumber;
  45. }
  46. private function updateLookupTable(): void
  47. {
  48. $this->processClassesAndTraits();
  49. $this->processFunctions();
  50. }
  51. private function processClassesAndTraits(): void
  52. {
  53. $classes = get_declared_classes();
  54. $traits = get_declared_traits();
  55. /* @noinspection PhpConditionAlreadyCheckedInspection */
  56. assert(is_array($traits));
  57. foreach (array_merge($classes, $traits) as $classOrTrait) {
  58. if (isset($this->processedClasses[$classOrTrait])) {
  59. continue;
  60. }
  61. foreach ((new ReflectionClass($classOrTrait))->getMethods() as $method) {
  62. $this->processFunctionOrMethod($method);
  63. }
  64. $this->processedClasses[$classOrTrait] = true;
  65. }
  66. }
  67. private function processFunctions(): void
  68. {
  69. foreach (get_defined_functions()['user'] as $function) {
  70. if (isset($this->processedFunctions[$function])) {
  71. continue;
  72. }
  73. $this->processFunctionOrMethod(new ReflectionFunction($function));
  74. $this->processedFunctions[$function] = true;
  75. }
  76. }
  77. private function processFunctionOrMethod(ReflectionFunctionAbstract $functionOrMethod): void
  78. {
  79. if ($functionOrMethod->isInternal()) {
  80. return;
  81. }
  82. $name = $functionOrMethod->getName();
  83. if ($functionOrMethod instanceof ReflectionMethod) {
  84. $name = $functionOrMethod->getDeclaringClass()->getName() . '::' . $name;
  85. }
  86. if (!isset($this->lookupTable[$functionOrMethod->getFileName()])) {
  87. $this->lookupTable[$functionOrMethod->getFileName()] = [];
  88. }
  89. foreach (range($functionOrMethod->getStartLine(), $functionOrMethod->getEndLine()) as $line) {
  90. $this->lookupTable[$functionOrMethod->getFileName()][$line] = $name;
  91. }
  92. }
  93. }