Skip to content

Consider default exceptions as possibly thrown exceptions #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/Rules/ThrowsPhpDocRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Iterator;
use IteratorAggregate;
use Pepakriz\PHPStanExceptionRules\CheckedExceptionService;
use Pepakriz\PHPStanExceptionRules\DefaultThrowTypeService;
use Pepakriz\PHPStanExceptionRules\DynamicThrowTypeService;
use Pepakriz\PHPStanExceptionRules\Node\FunctionEnd;
use Pepakriz\PHPStanExceptionRules\Node\TryCatchTryEnd;
use Pepakriz\PHPStanExceptionRules\ThrowsAnnotationReader;
use Pepakriz\PHPStanExceptionRules\UnsupportedClassException;
use Pepakriz\PHPStanExceptionRules\UnsupportedFunctionException;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -65,6 +68,11 @@ class ThrowsPhpDocRule implements Rule
*/
private $dynamicThrowTypeService;

/**
* @var DefaultThrowTypeService
*/
private $defaultThrowTypeService;

/**
* @var ThrowsAnnotationReader
*/
Expand Down Expand Up @@ -93,6 +101,7 @@ class ThrowsPhpDocRule implements Rule
public function __construct(
CheckedExceptionService $checkedExceptionService,
DynamicThrowTypeService $dynamicThrowTypeService,
DefaultThrowTypeService $defaultThrowTypeService,
ThrowsAnnotationReader $throwsAnnotationReader,
Broker $broker,
bool $reportUnusedCatchesOfUncheckedExceptions,
Expand All @@ -101,6 +110,7 @@ public function __construct(
{
$this->checkedExceptionService = $checkedExceptionService;
$this->dynamicThrowTypeService = $dynamicThrowTypeService;
$this->defaultThrowTypeService = $defaultThrowTypeService;
$this->throwsAnnotationReader = $throwsAnnotationReader;
$this->broker = $broker;
$this->throwsScope = new ThrowsScope();
Expand Down Expand Up @@ -433,26 +443,41 @@ private function filterUnusedExceptions(array $declaredThrows, array $usedThrows
$checkedThrowsAnnotations = $this->checkedExceptionService->filterCheckedExceptions($usedThrowsAnnotations);
$unusedThrows = array_diff($declaredThrows, $checkedThrowsAnnotations);

if (!$this->ignoreDescriptiveUncheckedExceptions) {
return $unusedThrows;
}

$functionReflection = $scope->getFunction();
if ($functionReflection === null) {
return $unusedThrows;
}

try {
if ($functionReflection instanceof MethodReflection) {
$defaultThrowsType = $functionReflection->getName() === '__construct' ?
$this->defaultThrowTypeService->getConstructorThrowType($functionReflection) :
$this->defaultThrowTypeService->getMethodThrowType($functionReflection);
} else {
$defaultThrowsType = $this->defaultThrowTypeService->getFunctionThrowType($functionReflection);
}
} catch (UnsupportedClassException | UnsupportedFunctionException $exception) {
$defaultThrowsType = new VoidType();
}

$unusedThrows = array_diff($unusedThrows, TypeUtils::getDirectClassNames($defaultThrowsType));

try {
if ($functionReflection instanceof MethodReflection) {
$nativeClassReflection = $functionReflection->getDeclaringClass()->getNativeReflection();
$nativeFunctionReflection = $nativeClassReflection->getMethod($functionReflection->getName());

} else {
$nativeFunctionReflection = new ReflectionFunction($functionReflection->getName());
}
} catch (ReflectionException $exception) {
return $unusedThrows;
}

if (!$this->ignoreDescriptiveUncheckedExceptions) {
return $unusedThrows;
}

$throwsAnnotations = $this->throwsAnnotationReader->read($nativeFunctionReflection);

return array_filter($unusedThrows, static function (string $type) use ($throwsAnnotations, $usedThrowsAnnotations): bool {
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Rules/PhpInternalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Pepakriz\PHPStanExceptionRules\Rules;

use Pepakriz\PHPStanExceptionRules\CheckedExceptionService;
use Pepakriz\PHPStanExceptionRules\DefaultThrowTypeService;
use Pepakriz\PHPStanExceptionRules\DynamicThrowTypeService;
use Pepakriz\PHPStanExceptionRules\Extension\DateTimeExtension;
use Pepakriz\PHPStanExceptionRules\Extension\JsonEncodeDecodeExtension;
Expand Down Expand Up @@ -36,6 +37,7 @@ protected function getRule(): Rule
$jsonEncodeDecodeExtension,
]
),
new DefaultThrowTypeService([], []),
$this->createThrowsAnnotationReader(),
$this->createBroker(),
true,
Expand Down
41 changes: 32 additions & 9 deletions tests/src/Rules/ThrowsPhpDocRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Pepakriz\PHPStanExceptionRules\DynamicThrowTypeService;
use Pepakriz\PHPStanExceptionRules\Rules\Data\CheckedException;
use Pepakriz\PHPStanExceptionRules\Rules\DynamicExtension\DynamicExtension;
use Pepakriz\PHPStanExceptionRules\Rules\UnusedCatches\FooException;
use Pepakriz\PHPStanExceptionRules\Rules\UnusedCatches\UnusedCatches;
use Pepakriz\PHPStanExceptionRules\RuleTestCase;
use PharData;
use PHPStan\Rules\Rule;
Expand All @@ -27,19 +29,26 @@ class ThrowsPhpDocRuleTest extends RuleTestCase
*/
private $ignoreDescriptiveUncheckedExceptions = false;

/**
* @var mixed[]
*/
private $methodThrowTypes = [];

/**
* @var mixed[]
*/
private $functionThrowTypes = [];

protected function getRule(): Rule
{
$defaultThrowTypeService = new DefaultThrowTypeService(
$this->methodThrowTypes,
$this->functionThrowTypes
);

$extensions = [
new DynamicExtension(),
new DefaultThrowTypeExtension(
new DefaultThrowTypeService([
PharData::class => [
'extractTo' => [
RuntimeException::class,
],
],
], [])
),
new DefaultThrowTypeExtension($defaultThrowTypeService),
];

return new ThrowsPhpDocRule(
Expand All @@ -56,6 +65,7 @@ protected function getRule(): Rule
$extensions,
$extensions
),
$defaultThrowTypeService,
$this->createThrowsAnnotationReader(),
$this->createBroker(),
$this->reportUnusedCatchesOfUncheckedExceptions,
Expand All @@ -80,6 +90,19 @@ public function testUnusedThrows(): void

public function testUnusedCatches(): void
{
$this->methodThrowTypes = [
PharData::class => [
'extractTo' => [
RuntimeException::class,
],
],
UnusedCatches::class => [
'methodWithDefaultThrowType' => [
FooException::class,
],
],
];

$this->analyse(__DIR__ . '/data/unused-catches.php');
}

Expand Down
9 changes: 8 additions & 1 deletion tests/src/Rules/data/unused-catches.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,12 @@ private function dynamicThrowType(): void
}
}

}
/**
* @throws FooException
*/
private function methodWithDefaultThrowType(callable $callable): void
{
$callable();
}

}