diff --git a/src/Broker/Broker.php b/src/Broker/Broker.php index e1fa72b7aa..ad6eca8593 100644 --- a/src/Broker/Broker.php +++ b/src/Broker/Broker.php @@ -349,11 +349,13 @@ public function getClassFromReflection(\ReflectionClass $reflectionClass, string public function hasClass(string $className): bool { + $className = trim($className, '\\'); if (isset($this->hasClassCache[$className])) { return $this->hasClassCache[$className]; } spl_autoload_register($autoloader = function (string $autoloadedClassName) use ($className): void { + $autoloadedClassName = trim($autoloadedClassName, '\\'); if ($autoloadedClassName !== $className && !$this->isExistsCheckCall()) { throw new \PHPStan\Broker\ClassAutoloadingException($autoloadedClassName); } diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index 941b487e3e..f1c828d53f 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -171,6 +171,14 @@ public function testNestedNamespaces(): void $this->assertSame(13, $errors[0]->getLine()); } + public function testClassExistsAutoloadingError(): void + { + $errors = $this->runAnalyse(__DIR__ . '/data/class-exists.php'); + $this->assertCount(1, $errors); + $this->assertSame('Instantiated class \PHPStan\GitHubIssue2359 not found.', $errors[0]->getMessage()); + $this->assertSame(12, $errors[0]->getLine()); + } + /** * @param string $file * @return \PHPStan\Analyser\Error[] diff --git a/tests/PHPStan/Analyser/data/class-exists.php b/tests/PHPStan/Analyser/data/class-exists.php new file mode 100644 index 0000000000..c0223a3f35 --- /dev/null +++ b/tests/PHPStan/Analyser/data/class-exists.php @@ -0,0 +1,16 @@ +