Throw exception when PHP start tag is missing

This commit is contained in:
WouterJ 2016-05-25 12:12:27 +02:00
parent 052c314bd0
commit 1e765c82af
3 changed files with 17 additions and 0 deletions

View File

@ -92,6 +92,11 @@ class AnnotationFileLoader extends FileLoader
$class = false;
$namespace = false;
$tokens = token_get_all(file_get_contents($file));
if (1 === count($tokens) && T_INLINE_HTML === $tokens[0][0]) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the "<?php" start tag at the beginning of the file?', $file));
}
for ($i = 0; isset($tokens[$i]); ++$i) {
$token = $tokens[$i];

View File

@ -0,0 +1,3 @@
class NoStartTagClass
{
}

View File

@ -45,6 +45,15 @@ class AnnotationFileLoaderTest extends AbstractAnnotationLoaderTest
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooTrait.php');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Did you forgot to add the "<?php" start tag at the beginning of the file?
*/
public function testLoadFileWithoutStartTag()
{
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/NoStartTagClass.php');
}
/**
* @requires PHP 5.6
*/