Skip to content

Commit 7769c17

Browse files
authored
ForbidNotNormalizedTypeRule: check even multi-catch statements (#231)
1 parent 32002c4 commit 7769c17

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Rule/ForbidNotNormalizedTypeRule.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\IntersectionType;
1212
use PhpParser\Node\NullableType;
1313
use PhpParser\Node\Param;
14+
use PhpParser\Node\Stmt\Catch_;
1415
use PhpParser\Node\Stmt\ClassMethod;
1516
use PhpParser\Node\Stmt\Function_;
1617
use PhpParser\Node\Stmt\Property;
@@ -103,9 +104,22 @@ public function processNode(
103104
);
104105
}
105106

107+
if ($node instanceof Catch_) {
108+
return $this->checkCatchNativeType($node, $scope);
109+
}
110+
106111
return $this->checkInlineVarDoc($node, $scope);
107112
}
108113

114+
/**
115+
* @return list<RuleError>
116+
*/
117+
private function checkCatchNativeType(Catch_ $node, Scope $scope): array
118+
{
119+
$multiTypeNode = new UnionType($node->types, $node->getAttributes());
120+
return $this->processMultiTypePhpParserNode($multiTypeNode, $scope, 'catch statement');
121+
}
122+
109123
/**
110124
* @return list<RuleError>
111125
*/

tests/Rule/data/ForbidNotNormalizedTypeRule/code.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,13 @@ public function testNativeUnions(
160160
{
161161
}
162162

163+
public function testCatch()
164+
{
165+
try {
166+
167+
} catch (InterfaceImplementor|MyInterface $k) { // error: Found non-normalized type \ForbidNotNormalizedTypeRule\InterfaceImplementor|\ForbidNotNormalizedTypeRule\MyInterface for catch statement: \ForbidNotNormalizedTypeRule\InterfaceImplementor is a subtype of \ForbidNotNormalizedTypeRule\MyInterface.
168+
169+
}
170+
}
171+
163172
}

0 commit comments

Comments
 (0)