Skip to content

Commit

Permalink
Use implicitThrows to only look for explicit throw points in too-wi…
Browse files Browse the repository at this point in the history
…de `@throws` rules when set to `false`
  • Loading branch information
ondrejmirtes committed Oct 1, 2024
1 parent 4b974a4 commit a0e688c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ services:

-
class: PHPStan\Rules\Exceptions\TooWideThrowTypeCheck
arguments:
implicitThrows: %exceptions.implicitThrows%

-
class: PHPStan\Rules\FunctionCallParametersCheck
Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Exceptions/TooWideThrowTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
final class TooWideThrowTypeCheck
{

public function __construct(private bool $implicitThrows)
{
}

/**
* @param ThrowPoint[] $throwPoints
* @return string[]
Expand All @@ -23,8 +27,8 @@ public function check(Type $throwType, array $throwPoints): array
return [];
}

$throwPointType = TypeCombinator::union(...array_map(static function (ThrowPoint $throwPoint): Type {
if (!$throwPoint->isExplicit()) {
$throwPointType = TypeCombinator::union(...array_map(function (ThrowPoint $throwPoint): Type {
if (!$this->implicitThrows && !$throwPoint->isExplicit()) {
return new NeverType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
class TooWideFunctionThrowTypeRuleTest extends RuleTestCase
{

private bool $implicitThrows = true;

protected function getRule(): Rule
{
return new TooWideFunctionThrowTypeRule(new TooWideThrowTypeCheck());
return new TooWideFunctionThrowTypeRule(new TooWideThrowTypeCheck($this->implicitThrows));
}

public function testRule(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
class TooWideMethodThrowTypeRuleTest extends RuleTestCase
{

private bool $implicitThrows = true;

protected function getRule(): Rule
{
return new TooWideMethodThrowTypeRule(self::getContainer()->getByType(FileTypeMapper::class), new TooWideThrowTypeCheck());
return new TooWideMethodThrowTypeRule(self::getContainer()->getByType(FileTypeMapper::class), new TooWideThrowTypeCheck($this->implicitThrows));
}

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

public static function dataRuleLookOnlyForExplicitThrowPoints(): iterable
{
yield [
true,
[],
];
yield [
false,
[
[
'Method TooWideThrowsExplicit\Foo::doFoo() has Exception in PHPDoc @throws tag but it\'s not thrown.',
11,
],
],
];
}

/**
* @dataProvider dataRuleLookOnlyForExplicitThrowPoints
* @param list<array{0: string, 1: int, 2?: string|null}> $errors
*/
public function testRuleLookOnlyForExplicitThrowPoints(bool $implicitThrows, array $errors): void
{
$this->implicitThrows = $implicitThrows;
$this->analyse([__DIR__ . '/data/too-wide-throws-explicit.php'], $errors);
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/too-wide-throws-explicit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace TooWideThrowsExplicit;

class Foo
{

/**
* @throws \Exception
*/
public function doFoo(): void
{
$a = 1 + 1;
$this->doBar();
}

public function doBar(): void
{

}

}

0 comments on commit a0e688c

Please sign in to comment.