8000 Throw exception when PHP start tag is missing · symfony/symfony@1e765c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e765c8

Browse files
committed
Throw exception when PHP start tag is missing
1 parent 052c314 commit 1e765c8

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ protected function findClass($file)
9292
$class = false;
9393
$namespace = false;
9494
$tokens = token_get_all(file_get_contents($file));
95+
96+
if (1 === count($tokens) && T_INLINE_HTML === $tokens[0][0]) {
97+
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));
98+
}
99+
95100
for ($i = 0; isset($tokens[$i]); ++$i) {
96101
$token = $tokens[$i];
97102

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class NoStartTagClass
2+
{
3+
}

src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public function testLoadTraitWithClassConstant()
4545
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooTrait.php');
4646
}
4747

48+
/**
49+
* @expectedException \InvalidArgumentException
50+
* @expectedExceptionMessage Did you forgot to add the "<?php" start tag at the beginning of the file?
51+
*/
52+
public function testLoadFileWithoutStartTag()
53+
{
54+
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/NoStartTagClass.php');
55+
}
56+
4857
/**
4958
* @requires PHP 5.6
5059
*/

0 commit comments

Comments
 (0)
0