Skip to content

Commit

Permalink
ForbidNotNormalizedTypeRule: check even multi-catch statements (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Apr 2, 2024
1 parent 32002c4 commit 7769c17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Rule/ForbidNotNormalizedTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,9 +104,22 @@ public function processNode(
);
}

if ($node instanceof Catch_) {
return $this->checkCatchNativeType($node, $scope);
}

return $this->checkInlineVarDoc($node, $scope);
}

/**
* @return list<RuleError>
*/
private function checkCatchNativeType(Catch_ $node, Scope $scope): array
{
$multiTypeNode = new UnionType($node->types, $node->getAttributes());
return $this->processMultiTypePhpParserNode($multiTypeNode, $scope, 'catch statement');
}

/**
* @return list<RuleError>
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Rule/data/ForbidNotNormalizedTypeRule/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}
}

}

0 comments on commit 7769c17

Please sign in to comment.