Skip to content

Commit

Permalink
Support union exception types in catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 20, 2017
1 parent 22488d5 commit ba190f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1187,11 +1187,9 @@ public function enterForeach(Expr $iteratee, string $valueName, string $keyName
*/
public function enterCatch(array $classes, string $variableName): self
{
if (count($classes) === 1) {
$type = new ObjectType((string) $classes[0]);
} else {
$type = new MixedType();
}
$type = TypeCombinator::combine(...array_map(function (string $class): ObjectType {
return new ObjectType($class);
}, $classes));

return $this->assignVariable($variableName, $type, TrinaryLogic::createYes());
}
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,34 @@ public function dataAssignInIf(): array
];
}

public function dataUnionInCatch(): array
{
return [
[
'CatchUnion\BarException|CatchUnion\FooException',
'$e',
],
];
}

/**
* @requires PHP 7.1
* @dataProvider dataUnionInCatch
* @param string $description
* @param string $expression
*/
public function testUnionInCatch(
string $description,
string $expression
)
{
$this->assertTypes(
__DIR__ . '/data/catch-union.php',
$description,
$expression
);
}

/**
* @dataProvider dataAssignInIf
* @param \PHPStan\Analyser\Scope $scope
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/catch-union.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php // lint >= 7.1

namespace CatchUnion;

try {

} catch (FooException | BarException $e) {
die;
}

0 comments on commit ba190f0

Please sign in to comment.