Skip to content

Commit

Permalink
bug #25801 [Router] Skip anonymous classes when loading annotated rou…
Browse files Browse the repository at this point in the history
…tes (pierredup)

This PR was squashed before being merged into the 2.7 branch (closes #25801).

Discussion
----------

[Router] Skip anonymous classes when loading annotated routes

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25796
| License       | MIT
| Doc PR        |

Skip any usage of anonymous classes when parsing files in `AnnotationFileLoader`

Commits
-------

d76a545 [Router] Skip anonymous classes when loading annotated routes
  • Loading branch information
nicolas-grekas committed Jan 16, 2018
2 parents 6c16252 + d76a545 commit 01de7ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ protected function findClass($file)
}

if (T_CLASS === $token[0]) {
// Skip usage of ::class constant
$isClassConstant = false;
// Skip usage of ::class constant and anonymous classes
$skipClassToken = false;
for ($j = $i - 1; $j > 0; --$j) {
if (!isset($tokens[$j][1])) {
break;
}

if (T_DOUBLE_COLON === $tokens[$j][0]) {
$isClassConstant = true;
if (T_DOUBLE_COLON === $tokens[$j][0] || T_NEW === $tokens[$j][0]) {
$skipClassToken = true;
break;
} elseif (!in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) {
break;
}
}

if (!$isClassConstant) {
if (!$skipClassToken) {
$class = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Tests\Fixtures\OtherAnnotatedClasses;

trait AnonymousClassInTrait
{
public function test()
{
return new class() {
public function foo()
{
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public function testLoadVariadic()
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/VariadicClass.php');
}

/**
* @requires PHP 7.0
*/
public function testLoadAnonymousClass()
{
$this->reader->expects($this->never())->method('getClassAnnotation');
$this->reader->expects($this->never())->method('getMethodAnnotations');

$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/AnonymousClassInTrait.php');
}

public function testSupports()
{
$fixture = __DIR__.'/../Fixtures/annotated.php';
Expand Down

0 comments on commit 01de7ad

Please sign in to comment.