Permalink
Browse files

Merge pull request #197 from alphalemon/master

Avoid a critical error when parsed class is not found
  • Loading branch information...
2 parents 9e3bea9 + 7a58e39 commit 150cb23135aa338b8f425fe40fa450ed372c8085 @schmittjoh schmittjoh committed Oct 7, 2012
@@ -46,6 +46,11 @@ public function parseClass(\ReflectionClass $class)
}
$content = $this->getFileContent($filename, $class->getStartLine());
+
+ if (null === $content) {
+ return array();
+ }
+
$namespace = str_replace('\\', '\\\\', $class->getNamespaceName());
$content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
$tokenizer = new TokenParser('<?php ' . $content);
@@ -64,6 +69,10 @@ public function parseClass(\ReflectionClass $class)
*/
private function getFileContent($filename, $lineNumber)
{
+ if ( ! file_exists($filename)) {
+ return null;
+ }
+
$content = '';
$lineCnt = 0;
$file = new SplFileObject($filename);
@@ -39,6 +39,19 @@ public function testParseClassWhenNotUserDefined()
$this->assertEquals(array(), $parser->parseClass(new \ReflectionClass('\stdClass')));
}
+ public function testClassFileDoesNotExist()
+ {
+ $class = $this->getMockBuilder('\ReflectionClass')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $class->expects($this->once())
+ ->method('getFilename')
+ ->will($this->returnValue('/valid/class/Fake.php(35) : eval()d code'));
+
+ $parser = new PhpParser();
+ $this->assertEquals(array(), $parser->parseClass($class));
+ }
+
public function testParseClassWhenClassIsNotNamespaced()
{
$parser = new PhpParser();

0 comments on commit 150cb23

Please sign in to comment.