From b1ba365afdf55692066dd59de27b79bb4f06a463 Mon Sep 17 00:00:00 2001 From: AndrolGenhald Date: Sun, 20 Feb 2022 16:55:23 -0600 Subject: [PATCH] Fix issues from review. --- .../Analyzer/Statements/Block/TryAnalyzer.php | 1 - .../Analyzer/Statements/BreakAnalyzer.php | 2 - .../Internal/Analyzer/StatementsAnalyzer.php | 2 +- stubs/phpparser.phpstub | 62 ++++++++++++++----- tests/DocumentationTest.php | 1 - tests/TryCatchTest.php | 8 +-- tests/UnusedVariableTest.php | 10 +-- 7 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php index 32e84343d1c..1219051493b 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php @@ -257,7 +257,6 @@ private static function analyzeCatch( $fq_catch_classes = []; - assert(!empty($catch->types)); foreach ($catch->types as $catch_type_stmt) { $fq_catch_class = ClassLikeAnalyzer::getFQCLNFromNameObject( $catch_type_stmt, diff --git a/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php index 9758db1f9c0..e25b0d990a0 100644 --- a/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php @@ -5,7 +5,6 @@ use PhpParser; use Psalm\Context; use Psalm\Internal\Analyzer\ScopeAnalyzer; -use Psalm\Internal\Analyzer\StatementsAnalyzer; use Psalm\Type; use function end; @@ -16,7 +15,6 @@ class BreakAnalyzer { public static function analyze( - StatementsAnalyzer $_statements_analyzer, PhpParser\Node\Stmt\Break_ $stmt, Context $context ): void { diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index 5abdbf7e10a..4eeb5fb3bd0 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -567,7 +567,7 @@ private static function analyzeStatement( } elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) { SwitchAnalyzer::analyze($statements_analyzer, $stmt, $context); } elseif ($stmt instanceof PhpParser\Node\Stmt\Break_) { - BreakAnalyzer::analyze($statements_analyzer, $stmt, $context); + BreakAnalyzer::analyze($stmt, $context); } elseif ($stmt instanceof PhpParser\Node\Stmt\Continue_) { ContinueAnalyzer::analyze($statements_analyzer, $stmt, $context); } elseif ($stmt instanceof PhpParser\Node\Stmt\Static_) { diff --git a/stubs/phpparser.phpstub b/stubs/phpparser.phpstub index 834f8f62539..44bc02274ee 100644 --- a/stubs/phpparser.phpstub +++ b/stubs/phpparser.phpstub @@ -1,22 +1,54 @@ + */ + abstract public function getRawArgs(): array; -abstract class CallLike extends Expr { - /** - * @return list - */ - abstract public function getRawArgs(): array; + public function isFirstClassCallable(): bool {} - public function isFirstClassCallable(): bool {} + /** + * @psalm-pure + * @return list + */ + public function getArgs(): array{} + } +} + +namespace PhpParser\Node\Stmt { + use PhpParser\Node; + use PhpParser\Node\Expr; + + class Catch_ extends Node\Stmt + { + /** @var non-empty-list Types of exceptions to catch */ + public $types; + /** @var Expr\Variable|null Variable for exception */ + public $var; + /** @var list Statements */ + public $stmts; + + /** + * Constructs a catch node. + * + * @param non-empty-list $types Types of exceptions to catch + * @param Expr\Variable|null $var Variable for exception + * @param list $stmts Statements + * @param array $attributes Additional attributes + */ + public function __construct( + array $types, Expr\Variable $var = null, array $stmts = [], array $attributes = [] + ) {} - /** - * @psalm-pure - * @return list - */ - public function getArgs(): array{} + /** @return list */ + public function getSubNodeNames() : array {} + } } diff --git a/tests/DocumentationTest.php b/tests/DocumentationTest.php index 23f271133e1..23484bc70cc 100644 --- a/tests/DocumentationTest.php +++ b/tests/DocumentationTest.php @@ -65,7 +65,6 @@ class DocumentationTest extends TestCase * annotations that we don’t want documented */ private const INTENTIONALLY_UNDOCUMENTED_ANNOTATIONS = [ - '@psalm-check-type', // Used internally for testing try-catch-finally, not sure if we want to support it '@psalm-self-out', // Not documented as it's a legacy alias of @psalm-this-out '@psalm-variadic', ]; diff --git a/tests/TryCatchTest.php b/tests/TryCatchTest.php index ac027890693..a16a036303b 100644 --- a/tests/TryCatchTest.php +++ b/tests/TryCatchTest.php @@ -44,7 +44,7 @@ class CustomException extends Exception implements CustomThrowable {} 'code' => ' [ @@ -141,7 +141,7 @@ function test(): string { try { $var = test(); - } catch (Throwable $e) { + } catch (Exception $e) { return; } @@ -159,7 +159,7 @@ function test(): string { try { $var = test(); - } catch (Throwable $e) { + } catch (Exception $e) { $var = "bad"; } @@ -257,7 +257,7 @@ function example() : void { try { $str = "a"; - } catch (Throwable $e) { + } catch (Exception $e) { example(); } ord($str);', diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 1e77f0fcd1f..d0b09e303f8 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -444,7 +444,7 @@ function dangerous(): string { function callDangerous(): void { try { $s = dangerous(); - } catch (Throwable $e) { + } catch (Exception $e) { echo $e->getMessage(); $s = "hello"; } @@ -470,7 +470,7 @@ function callDangerous(): void { } else { try { $t = dangerous(); - } catch (Throwable $e) { + } catch (Exception $e) { echo $e->getMessage(); $t = "hello"; } @@ -500,7 +500,7 @@ function callDangerous(): void { } catch (E1 $e) { echo $e->getMessage(); $s = false; - } catch (Throwable $_) { + } catch (Exception $_) { return; } @@ -592,7 +592,7 @@ function main() : void { if (!$s) { echo "Failed to get string\n"; } - } catch (Throwable $_) { + } catch (Exception $_) { $s = "fallback"; } printf("s is %s\n", $s); @@ -2889,7 +2889,7 @@ function callDangerous(): void { } else { try { $t = dangerous(); - } catch (Throwable $e) { + } catch (Exception $e) { echo $e->getMessage(); $t = "hello"; }