Skip to content

Commit

Permalink
Better message for misconfigured autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 6, 2016
1 parent 8d8c2dd commit 1e95c58
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Broker/ClassNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ClassNotFoundException extends \PHPStan\AnalysedCodeException

public function __construct(string $functionName)
{
parent::__construct(sprintf('Class %s not found.', $functionName));
parent::__construct(sprintf('Class %s was not found while trying to analyse it - autoloading is not probably configured properly.', $functionName));
$this->className = $functionName;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function checkFunction(
): array
{
if ($function instanceof ClassMethod && $scope->getClass() !== null) {
if (!$this->broker->hasClass($scope->getClass())) {
return [];
}
return $this->checkParametersAcceptor(
$this->broker->getClass($scope->getClass())->getMethod($function->name),
$parameterMessage,
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ public function testMissingPropertyAndMethod()
$this->assertNull($error->getLine());
}

public function testErrorAboutMisconfiguredAutoloader()
{
$file = __DIR__ . '/../../notAutoloaded/Bar.php';
$analyser = $this->getContainer()->getByType(Analyser::class);
$errors = $analyser->analyse([$file]);
$this->assertCount(1, $errors);
$error = $errors[0];
$this->assertSame('Class PHPStan\Tests\Bar was not found while trying to analyse it - autoloading is not probably configured properly.', $error->getMessage());
$this->assertSame($file, $error->getFile());
$this->assertNull($error->getLine());
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Broker/BrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp()
public function testClassNotFound()
{
$this->expectException(\PHPStan\Broker\ClassNotFoundException::class);
$this->expectExceptionMessage('Class NonexistentClass not found.');
$this->expectExceptionMessage('NonexistentClass');
$this->broker->getClass('NonexistentClass');
}

Expand Down
12 changes: 12 additions & 0 deletions tests/notAutoloaded/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace PHPStan\Tests;

class Bar
{

public function doFoo()
{
}

}

0 comments on commit 1e95c58

Please sign in to comment.