From 27bba684d88951f7475af027731b3701c1574197 Mon Sep 17 00:00:00 2001 From: Nicolas LHommet Date: Thu, 18 Feb 2021 17:20:50 +0100 Subject: [PATCH] [Routing] fix conflict with param named class in attribute --- .../Routing/Loader/AnnotationFileLoader.php | 7 +- .../Fixtures/Attributes/FooAttributes.php | 16 +++++ ...tributesClassParamAfterCommaController.php | 18 +++++ ...esClassParamAfterParenthesisController.php | 18 +++++ ...esClassParamInlineAfterCommaController.php | 12 ++++ ...sParamInlineAfterParenthesisController.php | 12 ++++ ...sParamInlineQuotedAfterCommaController.php | 12 ++++ ...InlineQuotedAfterParenthesisController.php | 12 ++++ ...esClassParamQuotedAfterCommaController.php | 17 +++++ ...sParamQuotedAfterParenthesisController.php | 17 +++++ .../Tests/Loader/AnnotationFileLoaderTest.php | 68 +++++++++++++++++++ 11 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/Attributes/FooAttributes.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterParenthesisController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterCommaController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterParenthesisController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterCommaController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterParenthesisController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterCommaController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterParenthesisController.php diff --git a/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php index 8f9af3a8f7..cd262f1ad1 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php @@ -97,12 +97,10 @@ class AnnotationFileLoader extends FileLoader $nsTokens = [\T_NS_SEPARATOR => true, \T_STRING => true]; if (\defined('T_NAME_QUALIFIED')) { - $nsTokens[T_NAME_QUALIFIED] = true; + $nsTokens[\T_NAME_QUALIFIED] = true; } - for ($i = 0; isset($tokens[$i]); ++$i) { $token = $tokens[$i]; - if (!isset($token[1])) { continue; } @@ -124,6 +122,9 @@ class AnnotationFileLoader extends FileLoader $skipClassToken = false; for ($j = $i - 1; $j > 0; --$j) { if (!isset($tokens[$j][1])) { + if ('(' === $tokens[$j] || ',' === $tokens[$j]) { + $skipClassToken = true; + } break; } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/Attributes/FooAttributes.php b/src/Symfony/Component/Routing/Tests/Fixtures/Attributes/FooAttributes.php new file mode 100644 index 0000000000..9edd0ece2e --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/Attributes/FooAttributes.php @@ -0,0 +1,16 @@ +class = $class; + $this->foo = $foo; + } +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php new file mode 100644 index 0000000000..3fdf55f30c --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php @@ -0,0 +1,18 @@ + ['foo','bar'], + 'foo' + ], + class: User::class +)] +class AttributesClassParamAfterCommaController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterParenthesisController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterParenthesisController.php new file mode 100644 index 0000000000..06edcabf6d --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamAfterParenthesisController.php @@ -0,0 +1,18 @@ + ['foo','bar'], + 'foo' + ] +)] +class AttributesClassParamAfterParenthesisController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterCommaController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterCommaController.php new file mode 100644 index 0000000000..4b47967f13 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterCommaController.php @@ -0,0 +1,12 @@ + ['foo','bar'],'foo'],class: User::class)] +class AttributesClassParamInlineAfterCommaController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterParenthesisController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterParenthesisController.php new file mode 100644 index 0000000000..876cdc33f7 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineAfterParenthesisController.php @@ -0,0 +1,12 @@ + ['foo','bar'],'foo'])] +class AttributesClassParamInlineAfterParenthesisController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterCommaController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterCommaController.php new file mode 100644 index 0000000000..471efd305a --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterCommaController.php @@ -0,0 +1,12 @@ + ['foo','bar'],'foo'],class: 'Symfony\Component\Security\Core\User\User')] +class AttributesClassParamInlineQuotedAfterCommaController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterParenthesisController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterParenthesisController.php new file mode 100644 index 0000000000..dc0ea7a820 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterParenthesisController.php @@ -0,0 +1,12 @@ + ['foo','bar'],'foo'])] +class AttributesClassParamInlineQuotedAfterParenthesisController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterCommaController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterCommaController.php new file mode 100644 index 0000000000..1d82cd1c61 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterCommaController.php @@ -0,0 +1,17 @@ + ['foo','bar'], + 'foo' + ], + class: 'Symfony\Component\Security\Core\User\User' +)] +class AttributesClassParamQuotedAfterCommaController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterParenthesisController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterParenthesisController.php new file mode 100644 index 0000000000..b1456c758a --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterParenthesisController.php @@ -0,0 +1,17 @@ + ['foo','bar'], + 'foo' + ] +)] +class AttributesClassParamQuotedAfterParenthesisController +{ + +} diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php index ff2dd53909..ee66ef1804 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php @@ -85,4 +85,72 @@ class AnnotationFileLoaderTest extends AbstractAnnotationLoaderTest $this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified'); $this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified'); } + + /** + * @requires PHP 8 + */ + public function testLoadAttributesClassAfterComma() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php'); + } + + public function testLoadAttributesInlineClassAfterComma() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineAfterCommaController.php'); + } + + /** + * @requires PHP 8 + */ + public function testLoadAttributesQuotedClassAfterComma() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterCommaController.php'); + } + + public function testLoadAttributesInlineQuotedClassAfterComma() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterCommaController.php'); + } + + /** + * @requires PHP 8 + */ + public function testLoadAttributesClassAfterParenthesis() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamAfterParenthesisController.php'); + } + + public function testLoadAttributesInlineClassAfterParenthesis() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineAfterParenthesisController.php'); + } + + /** + * @requires PHP 8 + */ + public function testLoadAttributesQuotedClassAfterParenthesis() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterParenthesisController.php'); + } + + public function testLoadAttributesInlineQuotedClassAfterParenthesis() + { + $this->reader->expects($this->once())->method('getClassAnnotation'); + + $this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterParenthesisController.php'); + } }