From 7da3130c09f5fe762a3a9b87bf61dde19128f204 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 29 Feb 2024 12:18:11 +0100 Subject: [PATCH] Catch more "There should be a node type provider" exceptions In #47 a try-catch was added to handle one case where `->getStatementsSource()->getNodeTypeProvider()` is being called and is throwing an exception because Psalm's FileAnalyzer doesn't populate the node type provider until after its NamespaceAnalyzer has already tried to analyze the whole file (see also #34). There are three other code paths making a similar call, at least one of which runs into the same problem. Let's add try-catches to all of them. --- Plugin.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Plugin.php b/Plugin.php index fbb0416..570d3ba 100644 --- a/Plugin.php +++ b/Plugin.php @@ -619,7 +619,11 @@ public static function afterEveryFunctionCallAnalysis( AfterEveryFunctionCallAna if ( ! $call_args[0]->value instanceof String_ ) { $statements_source = $event->getStatementsSource(); - $union = $statements_source->getNodeTypeProvider()->getType( $call_args[0]->value ); + try { + $union = $statements_source->getNodeTypeProvider()->getType( $call_args[0]->value ); + } catch (UnexpectedValueException $e) { + $union = null; + } if ( ! $union ) { $union = static::getTypeFromArg( $call_args[0]->value, $event->getContext(), $statements_source ); } @@ -821,7 +825,11 @@ public static function getFunctionParams( FunctionParamsProviderEvent $event ) : } if ( ! $call_args[0]->value instanceof String_ ) { - $union = $statements_source->getNodeTypeProvider()->getType( $call_args[0]->value ); + try { + $union = $statements_source->getNodeTypeProvider()->getType( $call_args[0]->value ); + } catch (UnexpectedValueException $e) { + $union = null; + } if ( ! $union ) { $union = static::getTypeFromArg( $call_args[0]->value, $event->getContext(), $statements_source ); } @@ -1112,7 +1120,11 @@ public static function getFunctionReturnType( FunctionReturnTypeProviderEvent $e // only apply_filters left to handle if ( ! $call_args[0]->value instanceof String_ ) { $statements_source = $event->getStatementsSource(); - $union = $statements_source->getNodeTypeProvider()->getType( $call_args[0]->value ); + try { + $union = $statements_source->getNodeTypeProvider()->getType( $call_args[0]->value ); + } catch (UnexpectedValueException $e) { + $union = null; + } if ( ! $union ) { $union = static::getTypeFromArg( $call_args[0]->value, $event->getContext(), $statements_source ); }