Skip to content

Commit 91c878e

Browse files
committed
Skip anonymous classes inside a trait when loading annotated routes
1 parent 70c8c2d commit 91c878e

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ protected function findClass($file)
107107
}
108108

109109
if (T_CLASS === $token[0]) {
110-
// Skip usage of ::class constant
111-
$isClassConstant = false;
110+
// Skip usage of ::class constant and anonymous classes
111+
$isValidClass = true;
112112
for ($j = $i - 1; $j > 0; --$j) {
113113
if (!isset($tokens[$j][1])) {
114114
break;
115115
}
116116

117-
if (T_DOUBLE_COLON === $tokens[$j][0]) {
118-
$isClassConstant = true;
117+
if (T_DOUBLE_COLON === $tokens[$j][0] || T_NEW === $tokens[$j][0]) {
118+
$isValidClass = false;
119119
break;
120120
} elseif (!in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) {
121121
break;
122122
}
123123
}
124124

125-
if (!$isClassConstant) {
125+
if ($isValidClass) {
126126
$class = true;
127127
}
128128
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Tests\Fixtures\OtherAnnotatedClasses;
13+
14+
trait AnonymousClassInTrait
15+
{
16+
public function test()
17+
{
18+
return new class() {
19+
public function foo()
20+
{
21+
}
22+
};
23+
}
24+
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ public function testLoadVariadic()
5858
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/VariadicClass.php');
5959
}
6060

61+
/**
62+
* @requires PHP 7.0
63+
*/
64+
public function testLoadAnonymousClass()
65+
{
66+
$this->reader->expects($this->never())->method('getClassAnnotation');
67+
$this->reader->expects($this->never())->method('getMethodAnnotations');
68+
69+
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/AnonymousClassInTrait.php');
70+
}
71+
6172
public function testSupports()
6273
{
6374
$fixture = __DIR__.'/../Fixtures/annotated.php';

0 commit comments

Comments
 (0)