diff --git a/conf/config.level0.neon b/conf/config.level0.neon index ead65da2ab..bc714eb60b 100644 --- a/conf/config.level0.neon +++ b/conf/config.level0.neon @@ -11,7 +11,6 @@ rules: - PHPStan\Rules\Classes\InstantiationRule - PHPStan\Rules\Classes\RequireParentConstructCallRule - PHPStan\Rules\Classes\UnusedConstructorParametersRule - - PHPStan\Rules\Exceptions\CaughtExceptionExistenceRule - PHPStan\Rules\Functions\CallToFunctionParametersRule - PHPStan\Rules\Functions\CallToNonExistentFunctionRule - PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule @@ -37,6 +36,13 @@ services: arguments: checkClassCaseSensitivity: %checkClassCaseSensitivity% + - + class: PHPStan\Rules\Exceptions\CaughtExceptionExistenceRule + tags: + - phpstan.rules.rule + arguments: + checkClassCaseSensitivity: %checkClassCaseSensitivity% + - class: PHPStan\Rules\Properties\ExistingClassesInPropertiesRule tags: diff --git a/src/Rules/Exceptions/CaughtExceptionExistenceRule.php b/src/Rules/Exceptions/CaughtExceptionExistenceRule.php index bad6cfa574..c5eca5df8e 100644 --- a/src/Rules/Exceptions/CaughtExceptionExistenceRule.php +++ b/src/Rules/Exceptions/CaughtExceptionExistenceRule.php @@ -6,6 +6,7 @@ use PhpParser\Node\Stmt\Catch_; use PHPStan\Analyser\Scope; use PHPStan\Broker\Broker; +use PHPStan\Rules\ClassCaseSensitivityCheck; class CaughtExceptionExistenceRule implements \PHPStan\Rules\Rule { @@ -15,9 +16,25 @@ class CaughtExceptionExistenceRule implements \PHPStan\Rules\Rule */ private $broker; - public function __construct(Broker $broker) + /** + * @var \PHPStan\Rules\ClassCaseSensitivityCheck + */ + private $classCaseSensitivityCheck; + + /** + * @var bool + */ + private $checkClassCaseSensitivity; + + public function __construct( + Broker $broker, + ClassCaseSensitivityCheck $classCaseSensitivityCheck, + bool $checkClassCaseSensitivity + ) { $this->broker = $broker; + $this->classCaseSensitivityCheck = $classCaseSensitivityCheck; + $this->checkClassCaseSensitivity = $checkClassCaseSensitivity; } public function getNodeType(): string @@ -51,6 +68,13 @@ public function processNode(Node $node, Scope $scope): array if (!$classReflection->isInterface() && !$classReflection->getNativeReflection()->implementsInterface(\Throwable::class)) { $errors[] = sprintf('Caught class %s is not an exception.', $classReflection->getDisplayName()); } + + if ($this->checkClassCaseSensitivity) { + $errors = array_merge( + $errors, + $this->classCaseSensitivityCheck->checkClassNames([$class]) + ); + } } return $errors; diff --git a/tests/PHPStan/Rules/Exceptions/CaughtExceptionExistenceRuleTest.php b/tests/PHPStan/Rules/Exceptions/CaughtExceptionExistenceRuleTest.php index 7abe81f0ed..13f6a032e0 100644 --- a/tests/PHPStan/Rules/Exceptions/CaughtExceptionExistenceRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/CaughtExceptionExistenceRuleTest.php @@ -2,13 +2,18 @@ namespace PHPStan\Rules\Exceptions; +use PHPStan\Rules\ClassCaseSensitivityCheck; + class CaughtExceptionExistenceRuleTest extends \PHPStan\Testing\RuleTestCase { protected function getRule(): \PHPStan\Rules\Rule { + $broker = $this->createBroker(); return new CaughtExceptionExistenceRule( - $this->createBroker() + $broker, + new ClassCaseSensitivityCheck($broker), + true ); } @@ -23,6 +28,10 @@ public function testCheckCaughtException(): void 'Caught class FooCatchException not found.', 29, ], + [ + 'Class TestCatch\MyCatchException referenced with incorrect case: TestCatch\MyCatchEXCEPTION.', + 41, + ], ]); } diff --git a/tests/PHPStan/Rules/Exceptions/data/catch.php b/tests/PHPStan/Rules/Exceptions/data/catch.php index ebe7341caa..82720fb9da 100644 --- a/tests/PHPStan/Rules/Exceptions/data/catch.php +++ b/tests/PHPStan/Rules/Exceptions/data/catch.php @@ -35,3 +35,9 @@ class MyCatchException extends \Exception } catch (\TypeError $e) { } + +try { + +} catch (\TestCatch\MyCatchEXCEPTION $e) { + +}