Skip to content

Commit

Permalink
Fixed a crashing issue when a different version of a class from the a…
Browse files Browse the repository at this point in the history
…utoloaded one is analyzed
  • Loading branch information
ondrejmirtes committed Nov 22, 2016
1 parent 733145c commit 2bab7f3
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -35,6 +35,6 @@
"psr-4": {"PHPStan\\": "src/"}
},
"autoload-dev": {
"classmap": ["tests/"]
"classmap": ["tests/PHPStan", "tests/TestCase.php"]
}
}
1 change: 1 addition & 0 deletions phpstan.neon
@@ -1,6 +1,7 @@
parameters:
excludes_analyse:
- %rootDir%/tests/*/data/*
- %rootDir%/tests/notAutoloaded/*
ignoreErrors:
- '#PHPUnit_Framework_MockObject_MockObject::method\(\)#'
services:
Expand Down
8 changes: 8 additions & 0 deletions src/Reflection/ClassReflection.php
Expand Up @@ -93,6 +93,10 @@ public function getMethod(string $methodName): MethodReflection
}
}

if (!isset($this->methods[$methodName])) {
throw new \PHPStan\Reflection\MissingMethodFromReflectionException($this->getName(), $methodName);
}

return $this->methods[$methodName];
}

Expand All @@ -112,6 +116,10 @@ public function getProperty(string $propertyName, Scope $scope = null): Property
}
}

if (!isset($this->properties[$propertyName])) {
throw new \PHPStan\Reflection\MissingPropertyFromReflectionException($this->getName(), $propertyName);
}

return $this->properties[$propertyName];
}

Expand Down
19 changes: 19 additions & 0 deletions src/Reflection/MissingMethodFromReflectionException.php
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection;

class MissingMethodFromReflectionException extends \PHPStan\AnalysedCodeException
{

public function __construct(string $className, string $methodName)
{
parent::__construct(
sprintf(
'Method %s() was not found in reflection of class %s - probably the wrong version of class is autoloaded.',
$methodName,
$className
)
);
}

}
19 changes: 19 additions & 0 deletions src/Reflection/MissingPropertyFromReflectionException.php
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection;

class MissingPropertyFromReflectionException extends \PHPStan\AnalysedCodeException
{

public function __construct(string $className, string $propertyName)
{
parent::__construct(
sprintf(
'Property $%s was not found in reflection of class %s - probably the wrong version of class is autoloaded.',
$propertyName,
$className
)
);
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Expand Up @@ -17,4 +17,16 @@ public function testUndefinedVariableFromAssignErrorHasLine()
$this->assertSame(3, $error->getLine());
}

public function testMissingPropertyAndMethod()
{
$file = __DIR__ . '/../../notAutoloaded/Foo.php';
$analyser = $this->getContainer()->getByType(Analyser::class);
$errors = $analyser->analyse([$file]);
$this->assertCount(1, $errors);
$error = $errors[0];
$this->assertSame('Property $fooProperty was not found in reflection of class PHPStan\Tests\Foo - probably the wrong version of class is autoloaded.', $error->getMessage());
$this->assertSame($file, $error->getFile());
$this->assertNull($error->getLine());
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/data/Foo.php
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\Tests;

class Foo
{

}
18 changes: 18 additions & 0 deletions tests/notAutoloaded/Foo.php
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace PHPStan\Tests;

class Foo
{

/** @var string */
private $fooProperty;

public function doFoo(): string
{
$this->fooProperty = 'test';

return $this->fooProperty;
}

}

0 comments on commit 2bab7f3

Please sign in to comment.