Skip to content

Commit 9be18df

Browse files
lookymanondrejmirtes
authored andcommitted
Improve memoizing reflectors
1 parent afb9975 commit 9be18df

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/Reflection/BetterReflection/Reflector/MemoizingClassReflector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Roave\BetterReflection\Reflection\Reflection;
66
use Roave\BetterReflection\Reflector\ClassReflector;
7+
use Throwable;
78

89
final class MemoizingClassReflector extends ClassReflector
910
{
1011

11-
/** @var array<string, \Roave\BetterReflection\Reflection\ReflectionClass> */
12+
/** @var array<string, \Roave\BetterReflection\Reflection\ReflectionClass|\Throwable> */
1213
private $reflections = [];
1314

1415
/**
@@ -21,9 +22,18 @@ final class MemoizingClassReflector extends ClassReflector
2122
public function reflect(string $className): Reflection
2223
{
2324
if (isset($this->reflections[$className])) {
25+
if ($this->reflections[$className] instanceof Throwable) {
26+
throw $this->reflections[$className];
27+
}
2428
return $this->reflections[$className];
2529
}
26-
return $this->reflections[$className] = parent::reflect($className);
30+
31+
try {
32+
return $this->reflections[$className] = parent::reflect($className);
33+
} catch (Throwable $e) {
34+
$this->reflections[$className] = $e;
35+
throw $e;
36+
}
2737
}
2838

2939
}

src/Reflection/BetterReflection/Reflector/MemoizingConstantReflector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Roave\BetterReflection\Reflection\Reflection;
66
use Roave\BetterReflection\Reflector\ConstantReflector;
7+
use Throwable;
78

89
final class MemoizingConstantReflector extends ConstantReflector
910
{
1011

11-
/** @var array<string, \Roave\BetterReflection\Reflection\ReflectionConstant> */
12+
/** @var array<string, \Roave\BetterReflection\Reflection\ReflectionConstant|\Throwable> */
1213
private $reflections = [];
1314

1415
/**
@@ -21,9 +22,18 @@ final class MemoizingConstantReflector extends ConstantReflector
2122
public function reflect(string $constantName): Reflection
2223
{
2324
if (isset($this->reflections[$constantName])) {
25+
if ($this->reflections[$constantName] instanceof Throwable) {
26+
throw $this->reflections[$constantName];
27+
}
2428
return $this->reflections[$constantName];
2529
}
26-
return $this->reflections[$constantName] = parent::reflect($constantName);
30+
31+
try {
32+
return $this->reflections[$constantName] = parent::reflect($constantName);
33+
} catch (Throwable $e) {
34+
$this->reflections[$constantName] = $e;
35+
throw $e;
36+
}
2737
}
2838

2939
}

src/Reflection/BetterReflection/Reflector/MemoizingFunctionReflector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Roave\BetterReflection\Reflection\Reflection;
66
use Roave\BetterReflection\Reflector\FunctionReflector;
7+
use Throwable;
78

89
final class MemoizingFunctionReflector extends FunctionReflector
910
{
1011

11-
/** @var array<string, \Roave\BetterReflection\Reflection\ReflectionFunction> */
12+
/** @var array<string, \Roave\BetterReflection\Reflection\ReflectionFunction|\Throwable> */
1213
private $reflections = [];
1314

1415
/**
@@ -21,9 +22,18 @@ final class MemoizingFunctionReflector extends FunctionReflector
2122
public function reflect(string $functionName): Reflection
2223
{
2324
if (isset($this->reflections[$functionName])) {
25+
if ($this->reflections[$functionName] instanceof Throwable) {
26+
throw $this->reflections[$functionName];
27+
}
2428
return $this->reflections[$functionName];
2529
}
26-
return $this->reflections[$functionName] = parent::reflect($functionName);
30+
31+
try {
32+
return $this->reflections[$functionName] = parent::reflect($functionName);
33+
} catch (Throwable $e) {
34+
$this->reflections[$functionName] = $e;
35+
throw $e;
36+
}
2737
}
2838

2939
}

0 commit comments

Comments
 (0)