10000 [Routing] Throw exception when PHP start tag is missing by wouterj · Pull Request #18869 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Throw exception when PHP start tag is missing #18869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ protected function findClass($file)
$class = false;
$namespace = false;
$tokens = token_get_all(file_get_contents($file));

if (1 === count($tokens) && T_INLINE_HTML === $tokens[0][0]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't warn for code looking like

foo
<?php
bar

intended?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. We're catching the case of forgetting the open PHP tag here, not whether the PHP tag is the first token in the file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to account for the fact that tokens are not always arrays

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first one is always an array :)

Copy link
Member Author
@wouterj wouterj May 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, as it's the first item, only two options are available: inline HTML or PHP start tag. These both are arrays, so we don't have to worry about it.

Btw, I just discovered that we can simplify this by only doing count($tokens) === 1, just <?php returns T_INLINE_HTML with <?php as value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is because the PHP start tag is either <?php or <?phpEOL (with whatever supported EOL char)

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];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class NoStartTagClass
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public function testLoadTraitWithClassConstant()
$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
*/
Expand Down
0