Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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: 0 additions & 1 deletion build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ parameters:
- 'PHPStan\ShouldNotHappenException'
- 'Symfony\Component\Console\Exception\InvalidArgumentException'
- 'PHPStan\BetterReflection\SourceLocator\Exception\InvalidFileLocation'
- 'PHPStan\BetterReflection\SourceLocator\Exception\InvalidArgumentException'
- 'Symfony\Component\Finder\Exception\DirectoryNotFoundException'
- 'InvalidArgumentException'
- 'PHPStan\DependencyInjection\ParameterNotFoundException'
Expand Down
11 changes: 11 additions & 0 deletions src/Rules/Exceptions/DefaultExceptionTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nette\Utils\Strings;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use function count;

/**
Expand All @@ -27,6 +28,16 @@ public function __construct(
private array $checkedExceptionClasses,
)
{
foreach ($this->checkedExceptionClasses as $checkedExceptionClass) {
if (!$this->reflectionProvider->hasClass($checkedExceptionClass)) {
throw new ShouldNotHappenException('Class ' . $checkedExceptionClass . ' used in \'checkedExceptionClasses\' does not exist.');
Copy link
Contributor Author

@janedbal janedbal May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the preferred way of config validation.

}
}
foreach ($this->uncheckedExceptionClasses as $uncheckedExceptionClass) {
if (!$this->reflectionProvider->hasClass($uncheckedExceptionClass)) {
throw new ShouldNotHappenException('Class ' . $uncheckedExceptionClass . ' used in \'uncheckedExceptionClasses\' does not exist.');
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want that behind featureFlag / bleedingEdge? I think this will be pretty rare.

}

public function isCheckedException(string $className, Scope $scope): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use ThrowsVoidFunction\DifferentException;
use ThrowsVoidFunction\MyException;

/**
Expand Down Expand Up @@ -38,7 +39,13 @@ public function dataRule(): array
],
[
false,
['DifferentException'],
[DifferentException::class],
[
[
'Function ThrowsVoidFunction\foo() throws exception ThrowsVoidFunction\MyException but the PHPDoc contains @throws void.',
15,
],
],
[
[
'Function ThrowsVoidFunction\foo() throws exception ThrowsVoidFunction\MyException but the PHPDoc contains @throws void.',
Expand All @@ -53,7 +60,7 @@ public function dataRule(): array
],
[
true,
['DifferentException'],
[DifferentException::class],
[
[
'Function ThrowsVoidFunction\foo() throws exception ThrowsVoidFunction\MyException but the PHPDoc contains @throws void.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use ThrowsVoidMethod\DifferentException;
use ThrowsVoidMethod\MyException;
use UnhandledMatchError;
use const PHP_VERSION_ID;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function dataRule(): array
],
[
false,
['DifferentException'],
[DifferentException::class],
[
[
'Method ThrowsVoidMethod\Foo::doFoo() throws exception ThrowsVoidMethod\MyException but the PHPDoc contains @throws void.',
Expand All @@ -55,7 +56,7 @@ public function dataRule(): array
],
[
true,
['DifferentException'],
[DifferentException::class],
[
[
'Method ThrowsVoidMethod\Foo::doFoo() throws exception ThrowsVoidMethod\MyException but the PHPDoc contains @throws void.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function dataRule(): array
],
[
false,
['DifferentException'],
['ThrowsVoidPropertyHook\\DifferentException'],
[
[
'Get hook for property ThrowsVoidPropertyHook\Foo::$i throws exception ThrowsVoidPropertyHook\MyException but the PHPDoc contains @throws void.',
Expand All @@ -57,7 +57,7 @@ public function dataRule(): array
],
[
true,
['DifferentException'],
['ThrowsVoidPropertyHook\\DifferentException'],
[
[
'Get hook for property ThrowsVoidPropertyHook\Foo::$i throws exception ThrowsVoidPropertyHook\MyException but the PHPDoc contains @throws void.',
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Exceptions/data/throws-void-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ThrowsVoidFunction;

class MyException extends \Exception
{
class MyException extends \Exception {}

class DifferentException extends \Exception {}

}

/**
* @throws void
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Exceptions/data/throws-void-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ThrowsVoidMethod;

class MyException extends \Exception
{
class MyException extends \Exception {}

class DifferentException extends \Exception {}

}

class Foo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ThrowsVoidPropertyHook;

class MyException extends \Exception
{
class MyException extends \Exception {}

class DifferentException extends \Exception {}

}

class Foo
{
Expand Down
Loading