diff --git a/build/phpstan.neon b/build/phpstan.neon index b285f56e12..b8979d63b7 100644 --- a/build/phpstan.neon +++ b/build/phpstan.neon @@ -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' diff --git a/src/Rules/Exceptions/DefaultExceptionTypeResolver.php b/src/Rules/Exceptions/DefaultExceptionTypeResolver.php index f428436b48..40b5a4095d 100644 --- a/src/Rules/Exceptions/DefaultExceptionTypeResolver.php +++ b/src/Rules/Exceptions/DefaultExceptionTypeResolver.php @@ -5,6 +5,7 @@ use Nette\Utils\Strings; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ReflectionProvider; +use PHPStan\ShouldNotHappenException; use function count; /** @@ -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.'); + } + } + foreach ($this->uncheckedExceptionClasses as $uncheckedExceptionClass) { + if (!$this->reflectionProvider->hasClass($uncheckedExceptionClass)) { + throw new ShouldNotHappenException('Class ' . $uncheckedExceptionClass . ' used in \'uncheckedExceptionClasses\' does not exist.'); + } + } } public function isCheckedException(string $className, Scope $scope): bool diff --git a/tests/PHPStan/Rules/Exceptions/ThrowsVoidFunctionWithExplicitThrowPointRuleTest.php b/tests/PHPStan/Rules/Exceptions/ThrowsVoidFunctionWithExplicitThrowPointRuleTest.php index ff6c4416a6..4a50e673da 100644 --- a/tests/PHPStan/Rules/Exceptions/ThrowsVoidFunctionWithExplicitThrowPointRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/ThrowsVoidFunctionWithExplicitThrowPointRuleTest.php @@ -4,6 +4,7 @@ use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; +use ThrowsVoidFunction\DifferentException; use ThrowsVoidFunction\MyException; /** @@ -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.', @@ -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.', diff --git a/tests/PHPStan/Rules/Exceptions/ThrowsVoidMethodWithExplicitThrowPointRuleTest.php b/tests/PHPStan/Rules/Exceptions/ThrowsVoidMethodWithExplicitThrowPointRuleTest.php index 5a2dcb0429..059bcb0bba 100644 --- a/tests/PHPStan/Rules/Exceptions/ThrowsVoidMethodWithExplicitThrowPointRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/ThrowsVoidMethodWithExplicitThrowPointRuleTest.php @@ -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; @@ -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.', @@ -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.', diff --git a/tests/PHPStan/Rules/Exceptions/ThrowsVoidPropertyHookWithExplicitThrowPointRuleTest.php b/tests/PHPStan/Rules/Exceptions/ThrowsVoidPropertyHookWithExplicitThrowPointRuleTest.php index 6072db088b..757e47c6ca 100644 --- a/tests/PHPStan/Rules/Exceptions/ThrowsVoidPropertyHookWithExplicitThrowPointRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/ThrowsVoidPropertyHookWithExplicitThrowPointRuleTest.php @@ -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.', @@ -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.', diff --git a/tests/PHPStan/Rules/Exceptions/data/throws-void-function.php b/tests/PHPStan/Rules/Exceptions/data/throws-void-function.php index d5a407ec13..c9e0e566a7 100644 --- a/tests/PHPStan/Rules/Exceptions/data/throws-void-function.php +++ b/tests/PHPStan/Rules/Exceptions/data/throws-void-function.php @@ -2,10 +2,10 @@ namespace ThrowsVoidFunction; -class MyException extends \Exception -{ +class MyException extends \Exception {} + +class DifferentException extends \Exception {} -} /** * @throws void diff --git a/tests/PHPStan/Rules/Exceptions/data/throws-void-method.php b/tests/PHPStan/Rules/Exceptions/data/throws-void-method.php index fec5c30a9a..084168dce1 100644 --- a/tests/PHPStan/Rules/Exceptions/data/throws-void-method.php +++ b/tests/PHPStan/Rules/Exceptions/data/throws-void-method.php @@ -2,10 +2,10 @@ namespace ThrowsVoidMethod; -class MyException extends \Exception -{ +class MyException extends \Exception {} + +class DifferentException extends \Exception {} -} class Foo { diff --git a/tests/PHPStan/Rules/Exceptions/data/throws-void-property-hook.php b/tests/PHPStan/Rules/Exceptions/data/throws-void-property-hook.php index 08e3f10940..e7abb0976f 100644 --- a/tests/PHPStan/Rules/Exceptions/data/throws-void-property-hook.php +++ b/tests/PHPStan/Rules/Exceptions/data/throws-void-property-hook.php @@ -2,10 +2,10 @@ namespace ThrowsVoidPropertyHook; -class MyException extends \Exception -{ +class MyException extends \Exception {} + +class DifferentException extends \Exception {} -} class Foo {