Skip to content

Commit

Permalink
Fixed fatal error when checking class existence with leading backslash
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 17, 2019
1 parent 01a76d1 commit 47a5cd2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Broker/Broker.php
Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Expand Up @@ -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[]
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/class-exists.php
@@ -0,0 +1,16 @@
<?php

namespace ClassExistsAutoloadingError;

class Foo
{

public function doFoo()
{
$className = '\PHPStan\GitHubIssue2359';
if (class_exists($className)) {
var_dump(new $className());
}
}

}

0 comments on commit 47a5cd2

Please sign in to comment.