Wizard.php 2.8 KB

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