Skip to content

Commit 38c4414

Browse files
committed
Fix an issue with traits referencing class constants
Solves: class_implements(): Class Zalas\Injector\PHPUnit\Tests\Symfony\Compiler\Fixtures\ does not exist and could not be loaded
1 parent 9af50d9 commit 38c4414

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Symfony/Compiler/Discovery/ClassFinder.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ private function findClassesInFile(callable $predicate, \SplFileInfo $phpFile):
5656
if (!\is_array($tokens[$index])) {
5757
continue;
5858
}
59-
if (T_NAMESPACE === $tokens[$index][0]) {
59+
if ($this->isNamespaceToken($tokens, $index)) {
6060
$index += 2; // Skip namespace keyword and whitespace
6161
while (isset($tokens[$index]) && \is_array($tokens[$index]) && T_WHITESPACE !== $tokens[$index][0]) {
6262
$namespace .= $tokens[$index++][1];
6363
}
6464
}
65-
if (T_CLASS === $tokens[$index][0]) {
65+
if ($this->isClassDefinitionToken($tokens, $index)) {
6666
$index += 2; // Skip class keyword and whitespace
6767
$class = $namespace . '\\' . $tokens[$index][1];
6868
if ($predicate($class)) {
@@ -76,6 +76,16 @@ private function findClassesInFile(callable $predicate, \SplFileInfo $phpFile):
7676
return $classes;
7777
}
7878

79+
private function isNamespaceToken($tokens, int $index): bool
80+
{
81+
return T_NAMESPACE === $tokens[$index][0];
82+
}
83+
84+
private function isClassDefinitionToken($tokens, int $index): bool
85+
{
86+
return T_CLASS === $tokens[$index][0] && T_WHITESPACE === $tokens[$index + 1][0] && T_STRING === $tokens[$index + 2][0];
87+
}
88+
7989
/**
8090
* @return \SplFileInfo[]|\Iterator
8191
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Zalas\Injector\PHPUnit\Tests\Symfony\Compiler\Fixtures;
5+
6+
trait FooTrait
7+
{
8+
/**
9+
* Reproduces https://github.com/jakzal/phpunit-injector/issues/3
10+
*/
11+
protected function bar(): string
12+
{
13+
return Service1::class;
14+
}
15+
}

0 commit comments

Comments
 (0)