Skip to content

[WIP][Routing] Fix the annotation loader taking a class constant as a beginning of a class name #18634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected function findClass($file)
$token = $tokens[$i];

if (!isset($token[1])) {
$class = false;
Copy link

@mkruk-u2 mkruk-u2 Apr 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this really fixes the problem.

Example:

public function doBar()
{
    $isAbc = MyClass::class === ABC;
}

I'll find "ABC" as a class name.

It seems that the problm is on line 114/116, i.e. the condition:

if (T_CLASS === $token[0]) {

because having T_CLASS token doesn't really mean that we've found a class definition.
Until PHP token_get_all() function can differentiate between class MyClass { ... (which is really a class definition), and MyClass::class (which isn't), I think this condition needs to be extended, e.g.:

$previousTokenIsPaamayimNekudotayim = isset($tokens[$i - 1]) && T_PAAMAYIM_NEKUDOTAYIM === $tokens[$i - 1][0];
if (T_CLASS === $token[0] && !$previousTokenIsPaamayimNekudotayim) {
    $class = true;
}

Copy link
Contributor Author

@jakzal jakzal Apr 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said this is not ready yet. Thanks for feedback so far!


continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

trait FooTrait
{
public function doBar()
{
$baz = self::class;
if (true) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function testLoad()
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
}

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

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

/**
* @requires PHP 5.6
*/
Expand Down