Skip to content

Commit 3e8ec5f

Browse files
committed
Catch more BetterReflection errors
1 parent cefbb1d commit 3e8ec5f

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/Analyser/FileAnalyser.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use PHPStan\Rules\Registry;
1616
use PHPStan\Rules\TipRuleError;
1717
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
18+
use Roave\BetterReflection\Reflection\Exception\NotAClassReflection;
19+
use Roave\BetterReflection\Reflection\Exception\NotAnInterfaceReflection;
1820
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
1921
use function array_fill_keys;
2022
use function array_key_exists;
@@ -90,8 +92,8 @@ public function analyseFile(
9092
} catch (IdentifierNotFound $e) {
9193
$fileErrors[] = new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, $node->getLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
9294
continue;
93-
} catch (UnableToCompileNode $e) {
94-
$fileErrors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
95+
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
96+
$fileErrors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getLine(), $e);
9597
continue;
9698
}
9799

@@ -178,7 +180,7 @@ public function analyseFile(
178180
// pass
179181
} catch (IdentifierNotFound $e) {
180182
// pass
181-
} catch (UnableToCompileNode $e) {
183+
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
182184
// pass
183185
}
184186
};
@@ -239,8 +241,8 @@ public function analyseFile(
239241
$fileErrors[] = new Error($e->getMessage(), $file, null, $e, null, null, $e->getTip());
240242
} catch (IdentifierNotFound $e) {
241243
$fileErrors[] = new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, null, $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
242-
} catch (UnableToCompileNode $e) {
243-
$fileErrors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, null, $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
244+
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
245+
$fileErrors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, null, $e);
244246
}
245247
} elseif (is_dir($file)) {
246248
$fileErrors[] = new Error(sprintf('File %s is a directory.', $file), $file, null, false);

src/Reflection/BetterReflection/BetterReflectionProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
2828
use Roave\BetterReflection\Reflection\Adapter\ReflectionClass;
2929
use Roave\BetterReflection\Reflection\Adapter\ReflectionFunction;
30+
use Roave\BetterReflection\Reflection\Exception\NotAClassReflection;
31+
use Roave\BetterReflection\Reflection\Exception\NotAnInterfaceReflection;
3032
use Roave\BetterReflection\Reflector\ClassReflector;
3133
use Roave\BetterReflection\Reflector\ConstantReflector;
3234
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
@@ -335,7 +337,7 @@ public function getConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): Glob
335337
$constantValue = $constantReflection->getValue();
336338
$constantValueType = ConstantTypeHelper::getTypeFromValue($constantValue);
337339
$fileName = $constantReflection->getFileName();
338-
} catch (UnableToCompileNode $e) {
340+
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
339341
$constantValueType = new MixedType();
340342
$fileName = null;
341343
}
@@ -355,7 +357,7 @@ public function resolveConstantName(\PhpParser\Node\Name $nameNode, ?Scope $scop
355357
return true;
356358
} catch (\Roave\BetterReflection\Reflector\Exception\IdentifierNotFound $e) {
357359
// pass
358-
} catch (UnableToCompileNode $e) {
360+
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
359361
// pass
360362
}
361363
return false;

0 commit comments

Comments
 (0)