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

Skip to content

Commit 556cfa1

Browse files
committed
Throw exception when PHP start tag is missing
1 parent c2f3b1e commit 556cfa1

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
7A30
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public function testLoad()
3535
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
3636
}
3737

38+
/**
39+
* @expectedException \InvalidArgumentException
40+
* @expectedExceptionMessage Did you forgot to add the "<?php" start tag at the beginning of the file?
41+
*/
42+
public function testLoadFileWithoutStartTag()
43+
{
44+
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/NoStartTagClass.php');
45+
}
46+
3847
/**
3948
* @requires PHP 5.6
4049
*/

0 commit comments

Comments
 (0)
0