Skip to content
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

[Routing] fix conflict with param named class in attribute #40244

Merged

Conversation

nlhommet
Copy link
Contributor

@nlhommet nlhommet commented Feb 18, 2021

Q A
Branch 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #40225
License MIT
Doc PR -

Fix conflict with AnnotationFileLoader and class PHP8 Attribute with param named "class"

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.x branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title [Routing] fix conflict with param named class in attribure fix conflict with param named class in attribure Feb 18, 2021
@nicolas-grekas nicolas-grekas changed the title fix conflict with param named class in attribure [Routing] fix conflict with param named class in attribure Feb 18, 2021
@nicolas-grekas nicolas-grekas added this to the 5.2 milestone Feb 18, 2021
@nicolas-grekas nicolas-grekas changed the base branch from 5.x to 5.2 February 18, 2021 16:51
@nicolas-grekas
Copy link
Member

Can you please rebase+retarget on 5.2 if that's really the target?

@nicolas-grekas nicolas-grekas changed the base branch from 5.2 to 5.x February 18, 2021 16:52
@nlhommet nlhommet changed the base branch from 5.x to 5.2 February 18, 2021 17:21
@nlhommet nlhommet force-pushed the issue-40225/annotation-file-loader branch from 9d6129b to 0c77083 Compare February 18, 2021 17:29
@nlhommet
Copy link
Contributor Author

Sorry it's my first PR, can you tell me how to correct my PR to be on 5.2?

@wouterj wouterj removed their request for review February 18, 2021 21:18
@nicolas-grekas nicolas-grekas changed the base branch from 5.2 to 5.x February 18, 2021 23:16
@nicolas-grekas nicolas-grekas changed the base branch from 5.x to 5.2 February 18, 2021 23:18
@nicolas-grekas nicolas-grekas force-pushed the issue-40225/annotation-file-loader branch from 0c77083 to 197843d Compare February 18, 2021 23:18
@nicolas-grekas
Copy link
Member

I force-pushed on your fork.
FYI, I used git rebase -i to do the rebase on 5.2.
Do you think you could add a test case, please?
(don't miss fetching your remote branch first of course)

@derrabus
Copy link
Member

Are we sure that 4.4 is not affected?

@nicolas-grekas nicolas-grekas changed the base branch from 5.2 to 4.4 February 19, 2021 07:30
@nicolas-grekas nicolas-grekas force-pushed the issue-40225/annotation-file-loader branch from 197843d to a6b2aaa Compare February 19, 2021 07:30
@nicolas-grekas nicolas-grekas modified the milestones: 5.2, 4.4 Feb 19, 2021
@nicolas-grekas
Copy link
Member

(now targetting 4.4)

@nlhommet
Copy link
Contributor Author

Tx for the help.
This impacts all branches compatible with PHP8 Attributes.
I set up tests today.

@nlhommet
Copy link
Contributor Author

I need to understand why it does not work in PHP7.1 on multiline Attributes

@nlhommet
Copy link
Contributor Author

I think there are tests that can only be done in PHP8.
Because an Attributes multiline like this

#[FooAttributes(
    class: User::class,
    foo: [
        'bar' => ['foo','bar'],
        'foo'
    ]
)]

is not recognized as a valid instruction in PHP7.X.

With #[FooAttributes(class: User::class,foo: ['bar' => ['foo','bar'],'foo'])], the Attribute is recognized as comment in PHP7.X and therefore poses no problem.

@nicolas-grekas
Copy link
Member

I think there are tests that can only be done in PHP8.

correct. You can add the @requires PHP 8 annotation on top of those tests.

@nicolas-grekas nicolas-grekas changed the title [Routing] fix conflict with param named class in attribure [Routing] fix conflict with param named class in attribute Feb 19, 2021
@derrabus
Copy link
Member

Thank you for working on this. However, I'm not convinced that bracket counting is the way we should fix this. We already have a piece of code in that file that handles incorrect T_CLASS tokens and I think we can extend that logic.

diff --git a/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php
--- a/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php	(revision eeed8c8e847b81ea5d16685395e85a1df9077449)
+++ b/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php	(date 1613917175487)
@@ -100,20 +100,8 @@
             $nsTokens[T_NAME_QUALIFIED] = true;
         }
 
-        $openBrackets = 0;
         for ($i = 0; isset($tokens[$i]); ++$i) {
             $token = $tokens[$i];
-            if (80000 <= \PHP_VERSION_ID) {
-                if (\T_ATTRIBUTE === ($token[0] ?? 0) || ($openBrackets > 0 && '[' === $token)) {
-                    ++$openBrackets;
-                }
-                if ($openBrackets > 0 && ']' === $token) {
-                    --$openBrackets;
-                }
-                if ($openBrackets > 0) {
-                    continue;
-                }
-            }
 
             if (!isset($token[1])) {
                 continue;
@@ -146,6 +134,15 @@
                         break;
                     }
                 }
+                for ($j = $i + 1; isset($tokens[$j]); ++$j) {
+                    if (':' === $tokens[$j]) {
+                        $skipClassToken = true;
+                        break;
+                    }
+                    if (!\in_array($tokens[$j][0], [\T_WHITESPACE, \T_DOC_COMMENT, \T_COMMENT])) {
+                        break;
+                    }
+                }
 
                 if (!$skipClassToken) {
                     $class = true;

@nlhommet
Copy link
Contributor Author

With this test I check if the T_CLASS token is after a comma or a parenthesis
This covers these 2 scenarios :

#[FooAttributes(
    foo: [
        'bar' => ['foo','bar'],
        'foo'
    ],
    class: User::class
)]
#[FooAttributes(
    class: User::class,
    foo: [
        'bar' => ['foo','bar'],
        'foo'
    ]
)]

Normally it shouldn't have side effects

Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

Please make fabbot happy. Apart from that, I think we're good. Thank you for working on this issue.

@nicolas-grekas nicolas-grekas force-pushed the issue-40225/annotation-file-loader branch from 27e6348 to 27bba68 Compare February 22, 2021 15:26
@nicolas-grekas
Copy link
Member

Thank you @nlhommet.

@nicolas-grekas nicolas-grekas merged commit 070f003 into symfony:4.4 Feb 22, 2021
This was referenced Mar 4, 2021
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.

conflict with AnnotationFileLoader and PHP8 attributes
4 participants