Skip to content

Option for reporting all unused catch statements #13

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
Jul 25, 2018
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ includes:

parameters:
exceptionRules:
reportUnusedCatchesOfUncheckedExceptions: false
checkedExceptions:
- RuntimeException
```
Expand Down
4 changes: 3 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
exceptionRules:
reportMaybes: true
reportUnusedCatchesOfUncheckedExceptions: false
checkedExceptions: []
uncheckedExceptions: []
methodThrowTypeDeclarations: []
Expand All @@ -22,6 +22,8 @@ services:

-
class: Pepakriz\PHPStanExceptionRules\Rules\ThrowsPhpDocRule
arguments:
reportUnusedCatchesOfUncheckedExceptions: %exceptionRules.reportUnusedCatchesOfUncheckedExceptions%
tags: [phpstan.rules.rule]

-
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parameters:
- %rootDir%/../../../tests/*/data/*

exceptionRules:
reportUnusedCatchesOfUncheckedExceptions: true
uncheckedExceptions:
- LogicException
- PHPStan\ShouldNotHappenException
Expand Down
23 changes: 18 additions & 5 deletions src/Rules/ThrowsPhpDocRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,23 @@ class ThrowsPhpDocRule implements Rule
*/
private $throwsScope;

/**
* @var bool
*/
private $reportUnusedCatchesOfUncheckedExceptions;

public function __construct(
CheckedExceptionService $checkedExceptionService,
DynamicThrowTypeService $dynamicThrowTypeService,
Broker $broker
Broker $broker,
bool $reportUnusedCatchesOfUncheckedExceptions
)
{
$this->checkedExceptionService = $checkedExceptionService;
$this->dynamicThrowTypeService = $dynamicThrowTypeService;
$this->broker = $broker;
$this->throwsScope = new ThrowsScope();
$this->reportUnusedCatchesOfUncheckedExceptions = $reportUnusedCatchesOfUncheckedExceptions;
}

public function getNodeType(): string
Expand Down Expand Up @@ -391,13 +398,19 @@ private function processCatch(Catch_ $node): array
}
}

$caughtExceptions = $this->checkedExceptionService->filterCheckedExceptions($caughtExceptions);
if (count($caughtExceptions) > 0) {
$exceptionClass = $type->toString();
if (
!$this->reportUnusedCatchesOfUncheckedExceptions
&& !$this->checkedExceptionService->isCheckedException($exceptionClass)
) {
continue;
}

$exceptionClass = $type->toString();
if (!$this->checkedExceptionService->isCheckedException($exceptionClass)) {
if (!$this->reportUnusedCatchesOfUncheckedExceptions) {
$caughtExceptions = $this->checkedExceptionService->filterCheckedExceptions($caughtExceptions);
}

if (count($caughtExceptions) > 0) {
continue;
}

Expand Down
14 changes: 13 additions & 1 deletion tests/src/Rules/ThrowsPhpDocRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
class ThrowsPhpDocRuleTest extends RuleTestCase
{

/**
* @var bool
*/
private $reportUnusedCatchesOfUncheckedExceptions = false;

protected function getRule(): Rule
{
$throwsRule = new ThrowsPhpDocRule(
Expand All @@ -31,7 +36,8 @@ protected function getRule(): Rule
], [
new DynamicFunctionExtension(),
]),
$this->createBroker()
$this->createBroker(),
$this->reportUnusedCatchesOfUncheckedExceptions
);

return $throwsRule;
Expand All @@ -57,6 +63,12 @@ public function testUnusedCatches(): void
$this->analyse(__DIR__ . '/data/unused-catches.php');
}

public function testAllUnusedCatches(): void
{
$this->reportUnusedCatchesOfUncheckedExceptions = true;
$this->analyse(__DIR__ . '/data/unused-catches-all.php');
}

public function testIterators(): void
{
$this->analyse(__DIR__ . '/data/iterators.php');
Expand Down
66 changes: 66 additions & 0 deletions tests/src/Rules/data/unused-catches-all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types = 1);

namespace Pepakriz\PHPStanExceptionRules\Rules\UnusedCatchesAll;

use LogicException;
use RuntimeException;

class FooException extends RuntimeException
{

}

class UnusedCatches
{

public function nestedUnusedCatch(): void
{
try {
try {
throw new FooException();
} catch (LogicException $e) { // error: LogicException is never thrown in the corresponding try block

} catch (RuntimeException $e) {

}
} catch (FooException $e) { // error: Pepakriz\PHPStanExceptionRules\Rules\UnusedCatchesAll\FooException is never thrown in the corresponding try block

}
}

public function correctCatchMethodCall(): void
{
try {
$this->someVoidMethod();
} catch (LogicException $e) { // error: LogicException is never thrown in the corresponding try block

} catch (RuntimeException $e) { // error: RuntimeException is never thrown in the corresponding try block

}
}

public function correctCatchMethodCallWithThrows(): void
{
try {
$this->throwLogic();
} catch (LogicException $e) {

} catch (RuntimeException $e) { // error: RuntimeException is never thrown in the corresponding try block

}
}

private function someVoidMethod(): void
{
}

/**
* @throws LogicException
*/
private function throwLogic(): void // error: Unused @throws LogicException annotation
{
throw new LogicException();
}

}

34 changes: 34 additions & 0 deletions tests/src/Rules/data/unused-catches.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,39 @@ public function nestedUnusedCatch(): void
}
}

public function correctCatchMethodCall(): void
{
try {
$this->someVoidMethod();
} catch (LogicException $e) {

} catch (RuntimeException $e) { // error: RuntimeException is never thrown in the corresponding try block

}
}

public function correctCatchMethodCallWithThrows(): void
{
try {
$this->throwLogic();
} catch (LogicException $e) {

} catch (RuntimeException $e) { // error: RuntimeException is never thrown in the corresponding try block

}
}

private function someVoidMethod(): void
{
}

/**
* @throws LogicException
*/
private function throwLogic(): void // error: Unused @throws LogicException annotation
{
throw new LogicException();
}

}