[Routing] fix conflict with param named class in attribute

This commit is contained in:
Nicolas LHommet 2021-02-18 17:20:50 +01:00 committed by Nicolas Grekas
parent a7f12dab24
commit 27bba684d8
11 changed files with 206 additions and 3 deletions

View File

@ -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;
}

View File

@ -0,0 +1,16 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\Attributes;
#[\Attribute(\Attribute::TARGET_CLASS)]
class FooAttributes
{
public string $class;
public array $foo = [];
public function __construct(string $class, array $foo)
{
$this->class = $class;
$this->foo = $foo;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
use Symfony\Component\Security\Core\User\User;
#[FooAttributes(
foo: [
'bar' => ['foo','bar'],
'foo'
],
class: User::class
)]
class AttributesClassParamAfterCommaController
{
}

View File

@ -0,0 +1,18 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
use Symfony\Component\Security\Core\User\User;
#[FooAttributes(
class: User::class,
foo: [
'bar' => ['foo','bar'],
'foo'
]
)]
class AttributesClassParamAfterParenthesisController
{
}

View File

@ -0,0 +1,12 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
use Symfony\Component\Security\Core\User\User;
#[FooAttributes(foo: ['bar' => ['foo','bar'],'foo'],class: User::class)]
class AttributesClassParamInlineAfterCommaController
{
}

View File

@ -0,0 +1,12 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
use Symfony\Component\Security\Core\User\User;
#[FooAttributes(class: User::class,foo: ['bar' => ['foo','bar'],'foo'])]
class AttributesClassParamInlineAfterParenthesisController
{
}

View File

@ -0,0 +1,12 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
use Symfony\Component\Security\Core\User\User;
#[FooAttributes(foo: ['bar' => ['foo','bar'],'foo'],class: 'Symfony\Component\Security\Core\User\User')]
class AttributesClassParamInlineQuotedAfterCommaController
{
}

View File

@ -0,0 +1,12 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
use Symfony\Component\Security\Core\User\User;
#[FooAttributes(class: 'Symfony\Component\Security\Core\User\User',foo: ['bar' => ['foo','bar'],'foo'])]
class AttributesClassParamInlineQuotedAfterParenthesisController
{
}

View File

@ -0,0 +1,17 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
#[FooAttributes(
foo: [
'bar' => ['foo','bar'],
'foo'
],
class: 'Symfony\Component\Security\Core\User\User'
)]
class AttributesClassParamQuotedAfterCommaController
{
}

View File

@ -0,0 +1,17 @@
<?php
namespace Symfony\Component\Routing\Tests\Fixtures\AttributesFixtures;
use Symfony\Component\Routing\Tests\Fixtures\Attributes\FooAttributes;
#[FooAttributes(
class: 'Symfony\Component\Security\Core\User\User',
foo: [
'bar' => ['foo','bar'],
'foo'
]
)]
class AttributesClassParamQuotedAfterParenthesisController
{
}

View File

@ -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');
}
}