10000 bug #40244 [Routing] fix conflict with param named class in attribute… · symfony/symfony@070f003 · GitHub
[go: up one dir, main page]

Skip to content

Commit 070f003

Browse files
bug #40244 [Routing] fix conflict with param named class in attribute (nlhommet)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Routing] fix conflict with param named class in attribute | 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" Commits ------- 27bba68 [Routing] fix conflict with param named class in attribute
2 parents 0027d1c + 27bba68 commit 070f003

11 files changed

+206
-3
lines changed

src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,10 @@ protected function findClass($file)
9797

9898
$nsTokens = [\T_NS_SEPARATOR => true, \T_STRING => true];
9999
if (\defined('T_NAME_QUALIFIED')) {
100-
$nsTokens[T_NAME_QUALIFIED] = true;
100+
$nsTokens[\T_NAME_QUALIFIED] = true;
101101
}
102-
103102
for ($i = 0; isset($tokens[$i]); ++$i) {
104103
$token = $tokens[$i];
105-
106104
if (!isset($token[1])) {
107105
continue;
108106
}
@@ -124,6 +122,9 @@ protected function findClass($file)
124122
$skipClassToken = false;
125123
for ($j = $i - 1; $j > 0; --$j) {
126124
if (!isset($tokens[$j][1])) {
125+
if ('(' === $tokens[$j] || ',' === $tokens[$j]) {
126+
$skipClassToken = true;
127+
}
127128
break;
128129
}
129130

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\Attributes;
4+
5+
#[\Attribute(\Attribute::TARGET_CLASS)]
6+
class FooAttributes
7+
{
8+
public string $class;
9+
public array $foo = [];
10+
11+
public function __construct(string $class, array $foo)
12+
{
13+
$this->class = $class;
14+
$this->foo = $foo;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
use Symfony\Component\Security\Core\User\User;
7+
8+
#[FooAttributes(
9+
foo: [
10+
'bar' => ['foo','bar'],
11+
'foo'
12+
],
13+
class: User::class
14+
)]
15+
class AttributesClassParamAfterCommaController
16+
{
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
use Symfony\Component\Security\Core\User\User;
7+
8+
#[FooAttributes(
9+
class: User::class,
10+
foo: [
11+
'bar' => ['foo','bar'],
12+
'foo'
13+
]
14+
)]
15+
class AttributesClassParamAfterParenthesisController
16+
{
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
use Symfony\Component\Security\Core\User\User;
7+
8+
#[FooAttributes(foo: ['bar' => ['foo','bar'],'foo'],class: User::class)]
9+
class AttributesClassParamInlineAfterCommaController
10+
{
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
use Symfony\Component\Security\Core\User\User;
7+
8+
#[FooAttributes(class: User::class,foo: ['bar' => ['foo','bar'],'foo'])]
9+
class AttributesClassParamInlineAfterParenthesisController
10+
{
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
use Symfony\Component\Security\Core\User\User;
7+
8+
#[FooAttributes(foo: ['bar' => ['foo','bar'],'foo'],class: 'Symfony\Component\Security\Core\User\User')]
9+
class AttributesClassParamInlineQuotedAfterCommaController
10+
{
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
use Symfony\Component\Security\Core\User\User;
7+
8+
#[FooAttributes(class: 'Symfony\Component\Security\Core\User\User',foo: ['bar' => ['foo','bar'],'foo'])]
9+
class AttributesClassParamInlineQuotedAfterParenthesisController
10+
{
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
7+
#[FooAttributes(
8+
foo: [
9+
'bar' => ['foo','bar'],
10+
'foo'
11+
],
12+
class: 'Symfony\Component\Security\Core\User\User'
13+
)]
14+
class AttributesClassParamQuotedAfterCommaController
15+
{
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
6+
7+
#[FooAttributes(
8+
class: 'Symfony\Component\Security\Core\User\User',
9+
foo: [
10+
'bar' => ['foo','bar'],
11+
'foo'
12+
]
13+
)]
14+
class AttributesClassParamQuotedAfterParenthesisController
15+
{
16+
17+
}

src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,72 @@ public function testSupports()
8585
$this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified');
8686
$this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified');
8787
}
88+
89+
/**
90+
* @requires PHP 8
91+
*/
92+
public function testLoadAttributesClassAfterComma()
93+
{
94+
$this->reader->expects($this->once())->method('getClassAnnotation');
95+
96+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamAfterCommaController.php');
97+
}
98+
99+
public function testLoadAttributesInlineClassAfterComma()
100+
{
101+
$this->reader->expects($this->once())->method('getClassAnnotation');
102+
103+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineAfterCommaController.php');
104+
}
105+
106+
/**
107+
* @requires PHP 8
108+
*/
109+
public function testLoadAttributesQuotedClassAfterComma()
110+
{
111+
$this->reader->expects($this->once())->method('getClassAnnotation');
112+
113+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterCommaController.php');
114+
}
115+
116+
public function testLoadAttributesInlineQuotedClassAfterComma()
117+
{
118+
$this->reader->expects($this->once())->method('getClassAnnotation');
119+
120+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterCommaController.php');
121+
}
122+
123+
/**
124+
* @requires PHP 8
125+
*/
126+
public function testLoadAttributesClassAfterParenthesis()
127+
{
128+
$this->reader->expects($this->once())->method('getClassAnnotation');
129+
130+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamAfterParenthesisController.php');
131+
}
132+
133+
public function testLoadAttributesInlineClassAfterParenthesis()
134+
{
135+
$this->reader->expects($this->once())->method('getClassAnnotation');
136+
137+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineAfterParenthesisController.php');
138+
}
139+
140+
/**
141+
* @requires PHP 8
142+
*/
143+
public function testLoadAttributesQuotedClassAfterParenthesis()
144+
{
145+
$this->reader->expects($this->once())->method('getClassAnnotation');
146+
147+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamQuotedAfterParenthesisController.php');
148+
}
149+
150+
public function testLoadAttributesInlineQuotedClassAfterParenthesis()
151+
{
152+
$this->reader->expects($this->once())->method('getClassAnnotation');
153+
154+
$this->loader->load(__DIR__.'/../Fixtures/AttributesFixtures/AttributesClassParamInlineQuotedAfterParenthesisController.php');
155+
}
88156
}
31FE

0 commit comments

Comments
 (0)
0