Skip to content
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This extension provides following rules and features:
* Unnecessary `@throws` annotation detection ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/unused-throws.php))
* Useless `@throws` annotation detection ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/useless-throws.php))
* Optionally ignore descriptive `@throws` annotations ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/unused-descriptive-throws.php))
* Optionally allows unused `@throws` annotations in implementations ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/intentionally-unused-throws.php))
* Optionally allows unused `@throws` annotations in subtypes ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/intentionally-unused-throws.php))
* `@throws` annotation variance validation ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/throws-inheritance.php))
* [Dynamic throw types based on arguments](#extensibility)
* Unreachable catch statements
Expand Down Expand Up @@ -49,9 +49,9 @@ includes:
parameters:
exceptionRules:
reportUnusedCatchesOfUncheckedExceptions: false
reportUnusedCheckedThrowsInSubtypes: false
reportCheckedThrowsInGlobalScope: false
ignoreDescriptiveUncheckedExceptions: false
allowUnusedThrowsInImplementation: false
checkedExceptions:
- RuntimeException
```
Expand Down
6 changes: 3 additions & 3 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
parameters:
exceptionRules:
reportUnusedCatchesOfUncheckedExceptions: false
reportUnusedCheckedThrowsInSubtypes: false
reportCheckedThrowsInGlobalScope: true
ignoreDescriptiveUncheckedExceptions: false
allowUnusedThrowsInImplementation: false
checkedExceptions: []
uncheckedExceptions: []
methodThrowTypeDeclarations: []
Expand All @@ -13,9 +13,9 @@ parameters:
parametersSchema:
exceptionRules: structure([
reportUnusedCatchesOfUncheckedExceptions: bool()
reportUnusedCheckedThrowsInSubtypes: bool()
reportCheckedThrowsInGlobalScope: bool()
ignoreDescriptiveUncheckedExceptions: bool()
allowUnusedThrowsInImplementation: bool()
checkedExceptions: listOf(string())
uncheckedExceptions: listOf(string())
methodThrowTypeDeclarations: arrayOf(arrayOf(listOf(string())))
Expand Down Expand Up @@ -73,9 +73,9 @@ services:
class: Pepakriz\PHPStanExceptionRules\Rules\ThrowsPhpDocRule
arguments:
reportUnusedCatchesOfUncheckedExceptions: %exceptionRules.reportUnusedCatchesOfUncheckedExceptions%
reportUnusedCheckedThrowsInSubtypes: %exceptionRules.reportUnusedCheckedThrowsInSubtypes%
reportCheckedThrowsInGlobalScope: %exceptionRules.reportCheckedThrowsInGlobalScope%
ignoreDescriptiveUncheckedExceptions: %exceptionRules.ignoreDescriptiveUncheckedExceptions%
allowUnusedThrowsInImplementation: %exceptionRules.allowUnusedThrowsInImplementation%
methodWhitelist: %exceptionRules.methodWhitelist%
tags: [phpstan.rules.rule]

Expand Down
8 changes: 4 additions & 4 deletions src/Rules/ThrowsPhpDocRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class ThrowsPhpDocRule implements Rule
/**
* @var bool
*/
private $allowUnusedThrowsInImplementation;
private $reportUnusedCheckedThrowsInSubtypes;

/** @var string[] */
private $methodWhitelist;
Expand All @@ -129,9 +129,9 @@ public function __construct(
ThrowsAnnotationReader $throwsAnnotationReader,
Broker $broker,
bool $reportUnusedCatchesOfUncheckedExceptions,
bool $reportUnusedCheckedThrowsInSubtypes,
bool $reportCheckedThrowsInGlobalScope,
bool $ignoreDescriptiveUncheckedExceptions,
bool $allowUnusedThrowsInImplementation,
array $methodWhitelist
)
{
Expand All @@ -144,7 +144,7 @@ public function __construct(
$this->reportUnusedCatchesOfUncheckedExceptions = $reportUnusedCatchesOfUncheckedExceptions;
$this->reportCheckedThrowsInGlobalScope = $reportCheckedThrowsInGlobalScope;
$this->ignoreDescriptiveUncheckedExceptions = $ignoreDescriptiveUncheckedExceptions;
$this->allowUnusedThrowsInImplementation = $allowUnusedThrowsInImplementation;
$this->reportUnusedCheckedThrowsInSubtypes = $reportUnusedCheckedThrowsInSubtypes;
$this->methodWhitelist = $methodWhitelist;
}

Expand Down Expand Up @@ -559,7 +559,7 @@ private function filterUnusedExceptions(array $declaredThrows, array $usedThrows
return $unusedThrows;
}

if ($this->allowUnusedThrowsInImplementation && $functionReflection instanceof MethodReflection) {
if (!$this->reportUnusedCheckedThrowsInSubtypes && $functionReflection instanceof MethodReflection) {
$declaringClass = $functionReflection->getDeclaringClass();
$nativeClassReflection = $declaringClass->getNativeReflection();
$nativeMethodReflection = $nativeClassReflection->getMethod($functionReflection->getName());
Expand Down
10 changes: 5 additions & 5 deletions tests/src/Rules/ThrowsPhpDocRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ class ThrowsPhpDocRuleTest extends RuleTestCase
/**
* @var bool
*/
private $ignoreDescriptiveUncheckedExceptions = false;
private $reportUnusedCheckedThrowsInSubtypes = true;

/**
* @var bool
*/
private $reportCheckedThrowsInGlobalScope = false;
private $ignoreDescriptiveUncheckedExceptions = false;

/**
* @var bool
*/
private $allowUnusedThrowsInImplementation = false;
private $reportCheckedThrowsInGlobalScope = false;

/**
* @var array<string, string>
Expand Down Expand Up @@ -85,9 +85,9 @@ protected function getRule(): Rule
$this->createThrowsAnnotationReader(),
$this->createBroker(),
$this->reportUnusedCatchesOfUncheckedExceptions,
$this->reportUnusedCheckedThrowsInSubtypes,
$this->reportCheckedThrowsInGlobalScope,
$this->ignoreDescriptiveUncheckedExceptions,
$this->allowUnusedThrowsInImplementation,
$this->methodWhitelist
);
}
Expand Down Expand Up @@ -182,7 +182,7 @@ public function testMethodWhitelist(): void

public function testIntentionallyUnusedThrows(): void
{
$this->allowUnusedThrowsInImplementation = true;
$this->reportUnusedCheckedThrowsInSubtypes = false;
$this->analyse(__DIR__ . '/data/intentionally-unused-throws.php');
}

Expand Down