Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented May 25, 2016

Q A
Branch? master (?)
Bug fix? yes
New feature? no
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets symfony/symfony-docs#6116 (and many more)
License MIT
Doc PR -

The Problem

In the documentation, we never use the PHP start tag. However, in the first tutorials, people simply copy/past the code examples, save the file and expect things to work. They seem to often forget to add the PHP start tag.

Without start tag, the annotation file loader simply skips the file without providing any reason why. As a big framework is quite overwhelming, simple things like this are completely forgotten.

The Fix

If a *.php file only consists of T_INLINE_HTML, it means there is no <?php start tag. In this case, instead of skipping the file, an exception is throwed with a possible fix.

BC Break?

As the file loader is only executed for *.php files, I think the BC break is minimal, but it is possible that people have applications with *.php files without a start tag. If this file lives in a routing loaded directory (e.g. when doing @AppBundle/Controller), it would now result in an exception.

I think this BC break is minimal and can be ignored. If you don't agree, we can add a little str match to check if class ... { exists. If that's the case, I think it's safe to say that it was meant to be a PHP file.

Bug or Feature?

I don't know if this is considered a bug or a feature, so I submitted it as a feature.

@wouterj wouterj changed the title Throw exception when PHP start tag is missing [Routing] Throw exception when PHP start tag is missing May 25, 2016
@javiereguiluz
Copy link
Member

A minor comment to reviewers: missing the opening PHP tag may seem like a stupid problem ... but please trust us when we say that we have received lots of reports about this error. We really need to fix this issue one way or another. Thank you!

@OskarStark
Copy link
Contributor

PR looks appropriate to me 👍 , but yes it is a BC break

@@ -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)

@linaori
Copy link
Contributor

linaori commented May 26, 2016

Besides of being a silly check forgotten, it's also quite embarrassing how you miss this error when helping someone. As more experienced phper, I kind of expect to see an open tag and if not present-make the basic assumption people simply left it out of the example. 👍 for this.

@nicolas-grekas
Copy link
Member

rebase needed

@wouterj wouterj force-pushed the routing_loader_without_php_start_tag branch from 556cfa1 to 1e765c8 Compare June 7, 2016 07:45
@wouterj
Copy link
Member Author

wouterj commented Jun 7, 2016

Status: Needs Review

@nicolas-grekas
Copy link
Member

👍

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 7, 2016

Personally I'd be OK to merge this into 2.7 btw. The potential BC break is not more allowed on master than on 2.7. Thus if we're ok to consider its potentiality is near to zero, then 2.7 should be the target.

@fabpot
Copy link
Member

fabpot commented Jun 7, 2016

@nicolas-grekas I don't agree. Introducing a potential BC break in master is ok and can be documented. Introducing a small BC break in a patch version is a no-go.

@dunglas
Copy link
Member

dunglas commented Jun 12, 2016

👍

@fabpot
Copy link
Member

fabpot commented Jun 12, 2016

Thank you @wouterj.

@fabpot fabpot merged commit 1e765c8 into symfony:master Jun 12, 2016
fabpot added a commit that referenced this pull request Jun 12, 2016
…g (WouterJ)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[Routing] Throw exception when PHP start tag is missing

| Q             | A
| ------------- | ---
| Branch?       | master (?)
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony-docs#6116 (and many more)
| License       | MIT
| Doc PR        | -

The Problem
---

In the documentation, we never use the PHP start tag. However, in the first tutorials, people simply copy/past the code examples, save the file and expect things to work. They seem to often forget to add the PHP start tag.

Without start tag, the annotation file loader simply skips the file without providing any reason why. As a big framework is quite overwhelming, simple things like this are completely forgotten.

The Fix
---

If a `*.php` file only consists of `T_INLINE_HTML`, it means there is no `<?php` start tag. In this case, instead of skipping the file, an exception is throwed with a possible fix.

BC Break?
---

As the file loader is only executed for `*.php` files, I think the BC break is minimal, but it is possible that people have applications with `*.php` files *without* a start tag. If this file lives in a routing loaded directory (e.g. when doing `@AppBundle/Controller`), it would now result in an exception.

I think this BC break is minimal and can be ignored. If you don't agree, we can add a little str match to check if `class ... {` exists. If that's the case, I think it's safe to say that it was meant to be a PHP file.

Bug or Feature?
---

I don't know if this is considered a bug or a feature, so I submitted it as a feature.

Commits
-------

1e765c8 Throw exception when PHP start tag is missing
@wouterj wouterj deleted the routing_loader_without_php_start_tag branch June 12, 2016 08:35
@fabpot fabpot mentioned this pull request Oct 27, 2016
fabpot added a commit that referenced this pull request Mar 2, 2017
…om annotations (jakzal)

This PR was merged into the 2.7 branch.

Discussion
----------

[Routing] Ignore hidden directories when loading routes from annotations

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21497
| License       | MIT
| Doc PR        | -

The problem surfaced after implementing #18869. Therefore it doesn't exist on 2.7, but I'd still merge it there to avoid conflicts when merging between branches. Without this fix, the oldest branch the added test will fail is 3.2.

Commits
-------

ce9df02 [Routing] Ignore hidden directories when loading routes from annotations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants