Skip to content

Commit

Permalink
Fix CatchWithUnthrownExceptionRule for new $expr()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 5, 2021
1 parent 566b44b commit 8463afd
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2384,10 +2384,22 @@ static function () use ($expr, $rightResult): MutatingScope {
$hasYield = false;
$throwPoints = [];
if ($expr->class instanceof Expr) {
$objectClasses = TypeUtils::getDirectClassNames($scope->getType($expr));
if (count($objectClasses) === 1) {
$objectExprResult = $this->processExprNode(new New_(new Name($objectClasses[0])), $scope, static function (): void {
}, $context->enterDeep());
$additionalThrowPoints = $objectExprResult->getThrowPoints();
} else {
$additionalThrowPoints = [ThrowPoint::createImplicit($scope)];
}

$result = $this->processExprNode($expr->class, $scope, $nodeCallback, $context->enterDeep());
$scope = $result->getScope();
$hasYield = $result->hasYield();
$throwPoints = $result->getThrowPoints();
foreach ($additionalThrowPoints as $throwPoint) {
$throwPoints[] = $throwPoint;
}
} elseif ($expr->class instanceof Class_) {
$this->reflectionProvider->getAnonymousClassReflection($expr->class, $scope); // populates $expr->class->name
$this->processStmtNode($expr->class, $scope, $nodeCallback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ public function testRule(): void
]);
}

public function testBug4806(): void
{
$this->analyse([__DIR__ . '/data/bug-4806.php'], [
[
'Dead catch - ArgumentCountError is never thrown in the try block.',
65,
],
[
'Dead catch - Throwable is never thrown in the try block.',
119,
],
]);
}

}
124 changes: 124 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-4806.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Bug4806;

class NeverThrows
{
/**
* @throws void
*/
final public function __construct()
{
}
}

class HasNoConstructor
{

}

class MayThrowArgumentCountError
{
/**
* @throws \ArgumentCountError
*/
public function __construct()
{
throw new \ArgumentCountError();
}
}

class ImplicitThrow
{

public function __construct()
{

}

}

class Foo
{

/**
* @param class-string $class
*/
function createNotSpecified(string $class): object
{
try {
$object = new $class();
} catch (\ArgumentCountError $error) {

}

return $object;
}

/**
* @param class-string<NeverThrows> $class
*/
function createNeverThrows(string $class): object
{
try {
$object = new $class();
} catch (\ArgumentCountError $throwable) {

}

return $object;
}

/**
* @param class-string<MayThrowArgumentCountError> $class
*/
function createMayThrowArgumentCountError(string $class): object
{
try {
$object = new $class();
} catch (\ArgumentCountError $error) {

}

return $object;
}

/**
* @param class-string<MayThrowArgumentCountError> $class
*/
function createMayThrowArgumentCountErrorB(string $class): object
{
try {
$object = new $class();
} catch (\Throwable $throwable) {

}

return $object;
}

/**
* @param class-string<ImplicitThrow> $class
*/
function implicitThrow(string $class): void
{
try {
$object = new $class();
} catch (\Throwable $throwable) {

}
}

/**
* @param class-string<HasNoConstructor> $class
*/
function hasNoConstructor(string $class): void
{
try {
$object = new $class();
} catch (\Throwable $throwable) {

}
}

}

0 comments on commit 8463afd

Please sign in to comment.