diff --git a/README.md b/README.md index cd4f6a3..c6b14ac 100644 --- a/README.md +++ b/README.md @@ -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 @@ -49,9 +49,9 @@ includes: parameters: exceptionRules: reportUnusedCatchesOfUncheckedExceptions: false + reportUnusedCheckedThrowsInSubtypes: false reportCheckedThrowsInGlobalScope: false ignoreDescriptiveUncheckedExceptions: false - allowUnusedThrowsInImplementation: false checkedExceptions: - RuntimeException ``` diff --git a/extension.neon b/extension.neon index a8e2abc..4a65875 100644 --- a/extension.neon +++ b/extension.neon @@ -1,9 +1,9 @@ parameters: exceptionRules: reportUnusedCatchesOfUncheckedExceptions: false + reportUnusedCheckedThrowsInSubtypes: false reportCheckedThrowsInGlobalScope: true ignoreDescriptiveUncheckedExceptions: false - allowUnusedThrowsInImplementation: false checkedExceptions: [] uncheckedExceptions: [] methodThrowTypeDeclarations: [] @@ -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()))) @@ -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] diff --git a/src/Rules/ThrowsPhpDocRule.php b/src/Rules/ThrowsPhpDocRule.php index 0482e93..6016195 100644 --- a/src/Rules/ThrowsPhpDocRule.php +++ b/src/Rules/ThrowsPhpDocRule.php @@ -114,7 +114,7 @@ class ThrowsPhpDocRule implements Rule /** * @var bool */ - private $allowUnusedThrowsInImplementation; + private $reportUnusedCheckedThrowsInSubtypes; /** @var string[] */ private $methodWhitelist; @@ -129,9 +129,9 @@ public function __construct( ThrowsAnnotationReader $throwsAnnotationReader, Broker $broker, bool $reportUnusedCatchesOfUncheckedExceptions, + bool $reportUnusedCheckedThrowsInSubtypes, bool $reportCheckedThrowsInGlobalScope, bool $ignoreDescriptiveUncheckedExceptions, - bool $allowUnusedThrowsInImplementation, array $methodWhitelist ) { @@ -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; } @@ -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()); diff --git a/tests/src/Rules/ThrowsPhpDocRuleTest.php b/tests/src/Rules/ThrowsPhpDocRuleTest.php index 8d4dc65..a1f8d52 100644 --- a/tests/src/Rules/ThrowsPhpDocRuleTest.php +++ b/tests/src/Rules/ThrowsPhpDocRuleTest.php @@ -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 @@ -85,9 +85,9 @@ protected function getRule(): Rule $this->createThrowsAnnotationReader(), $this->createBroker(), $this->reportUnusedCatchesOfUncheckedExceptions, + $this->reportUnusedCheckedThrowsInSubtypes, $this->reportCheckedThrowsInGlobalScope, $this->ignoreDescriptiveUncheckedExceptions, - $this->allowUnusedThrowsInImplementation, $this->methodWhitelist ); } @@ -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'); }