From 47541e59572ab9af20ddcb577b3c3af354625436 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 2 Apr 2024 10:24:12 +0200 Subject: [PATCH] ForbidNotNormalizedTypeRule: check even multi-catch statements --- src/Rule/ForbidNotNormalizedTypeRule.php | 14 ++++++++++++++ .../Rule/data/ForbidNotNormalizedTypeRule/code.php | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Rule/ForbidNotNormalizedTypeRule.php b/src/Rule/ForbidNotNormalizedTypeRule.php index ace2ebd..750836b 100644 --- a/src/Rule/ForbidNotNormalizedTypeRule.php +++ b/src/Rule/ForbidNotNormalizedTypeRule.php @@ -11,6 +11,7 @@ use PhpParser\Node\IntersectionType; use PhpParser\Node\NullableType; use PhpParser\Node\Param; +use PhpParser\Node\Stmt\Catch_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Property; @@ -103,9 +104,22 @@ public function processNode( ); } + if ($node instanceof Catch_) { + return $this->checkCatchNativeType($node, $scope); + } + return $this->checkInlineVarDoc($node, $scope); } + /** + * @return list + */ + private function checkCatchNativeType(Catch_ $node, Scope $scope): array + { + $multiTypeNode = new UnionType($node->types, $node->getAttributes()); + return $this->processMultiTypePhpParserNode($multiTypeNode, $scope, 'catch statement'); + } + /** * @return list */ diff --git a/tests/Rule/data/ForbidNotNormalizedTypeRule/code.php b/tests/Rule/data/ForbidNotNormalizedTypeRule/code.php index 33145c2..92fdd16 100644 --- a/tests/Rule/data/ForbidNotNormalizedTypeRule/code.php +++ b/tests/Rule/data/ForbidNotNormalizedTypeRule/code.php @@ -160,4 +160,13 @@ public function testNativeUnions( { } + public function testCatch() + { + try { + + } catch (InterfaceImplementor|MyInterface $k) { // error: Found non-normalized type \ForbidNotNormalizedTypeRule\InterfaceImplementor|\ForbidNotNormalizedTypeRule\MyInterface for catch statement: \ForbidNotNormalizedTypeRule\InterfaceImplementor is a subtype of \ForbidNotNormalizedTypeRule\MyInterface. + + } + } + }