Skip to content

Commit a0e688c

Browse files
committed
Use implicitThrows to only look for explicit throw points in too-wide @throws rules when set to false
1 parent 4b974a4 commit a0e688c

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

conf/config.neon

+2
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ services:
879879

880880
-
881881
class: PHPStan\Rules\Exceptions\TooWideThrowTypeCheck
882+
arguments:
883+
implicitThrows: %exceptions.implicitThrows%
882884

883885
-
884886
class: PHPStan\Rules\FunctionCallParametersCheck

src/Rules/Exceptions/TooWideThrowTypeCheck.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
final class TooWideThrowTypeCheck
1414
{
1515

16+
public function __construct(private bool $implicitThrows)
17+
{
18+
}
19+
1620
/**
1721
* @param ThrowPoint[] $throwPoints
1822
* @return string[]
@@ -23,8 +27,8 @@ public function check(Type $throwType, array $throwPoints): array
2327
return [];
2428
}
2529

26-
$throwPointType = TypeCombinator::union(...array_map(static function (ThrowPoint $throwPoint): Type {
27-
if (!$throwPoint->isExplicit()) {
30+
$throwPointType = TypeCombinator::union(...array_map(function (ThrowPoint $throwPoint): Type {
31+
if (!$this->implicitThrows && !$throwPoint->isExplicit()) {
2832
return new NeverType();
2933
}
3034

tests/PHPStan/Rules/Exceptions/TooWideFunctionThrowTypeRuleTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
class TooWideFunctionThrowTypeRuleTest extends RuleTestCase
1212
{
1313

14+
private bool $implicitThrows = true;
15+
1416
protected function getRule(): Rule
1517
{
16-
return new TooWideFunctionThrowTypeRule(new TooWideThrowTypeCheck());
18+
return new TooWideFunctionThrowTypeRule(new TooWideThrowTypeCheck($this->implicitThrows));
1719
}
1820

1921
public function testRule(): void

tests/PHPStan/Rules/Exceptions/TooWideMethodThrowTypeRuleTest.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
class TooWideMethodThrowTypeRuleTest extends RuleTestCase
1414
{
1515

16+
private bool $implicitThrows = true;
17+
1618
protected function getRule(): Rule
1719
{
18-
return new TooWideMethodThrowTypeRule(self::getContainer()->getByType(FileTypeMapper::class), new TooWideThrowTypeCheck());
20+
return new TooWideMethodThrowTypeRule(self::getContainer()->getByType(FileTypeMapper::class), new TooWideThrowTypeCheck($this->implicitThrows));
1921
}
2022

2123
public function testRule(): void
@@ -72,4 +74,31 @@ public function testFirstClassCallable(): void
7274
$this->analyse([__DIR__ . '/data/immediately-called-fcc.php'], []);
7375
}
7476

77+
public static function dataRuleLookOnlyForExplicitThrowPoints(): iterable
78+
{
79+
yield [
80+
true,
81+
[],
82+
];
83+
yield [
84+
false,
85+
[
86+
[
87+
'Method TooWideThrowsExplicit\Foo::doFoo() has Exception in PHPDoc @throws tag but it\'s not thrown.',
88+
11,
89+
],
90+
],
91+
];
92+
}
93+
94+
/**
95+
* @dataProvider dataRuleLookOnlyForExplicitThrowPoints
96+
* @param list<array{0: string, 1: int, 2?: string|null}> $errors
97+
*/
98+
public function testRuleLookOnlyForExplicitThrowPoints(bool $implicitThrows, array $errors): void
99+
{
100+
$this->implicitThrows = $implicitThrows;
101+
$this->analyse([__DIR__ . '/data/too-wide-throws-explicit.php'], $errors);
102+
}
103+
75104
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace TooWideThrowsExplicit;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @throws \Exception
10+
*/
11+
public function doFoo(): void
12+
{
13+
$a = 1 + 1;
14+
$this->doBar();
15+
}
16+
17+
public function doBar(): void
18+
{
19+
20+
}
21+
22+
}

0 commit comments

Comments
 (0)