Skip to content

Commit 5750499

Browse files
committed
Fix FromAttributes::imports() for classes created inside eval
1 parent e6d549c commit 5750499

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ XP Reflection ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
## 2.14.1 / 2023-09-30
7+
8+
* Fixed `FromAttributes::imports()` for classes created inside `eval`
9+
(@thekid)
10+
611
## 2.14.0 / 2023-09-23
712

813
* Added support for partial meta information - @thekid

src/main/php/lang/meta/FromAttributes.class.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ public function ofParameter($method, $reflect) {
5858
/**
5959
* Returns imports used in the class file the given class was declared in
6060
*
61-
* @param \ReflectionClass $reflect
61+
* @param ReflectionClass $reflect
6262
* @return [:string]
6363
*/
6464
public function imports($reflect) {
6565
static $break= [T_CLASS => true, T_INTERFACE => true, T_TRAIT => true, 372 /* T_ENUM */ => true];
6666
static $types= [T_WHITESPACE => true, 44 => true, 59 => true, 123 => true];
6767

68-
$tokens= PhpToken::tokenize(file_get_contents($reflect->getFileName()));
68+
// Exclude classes declared inside eval(), their declaration is not accessible
69+
$file= $reflect->getFileName();
70+
if (false !== strpos($file, ': eval')) return [];
71+
72+
$tokens= PhpToken::tokenize(file_get_contents($file));
6973
$imports= [];
7074
for ($i= 0, $s= sizeof($tokens); $i < $s; $i++) {
7175
if (isset($break[$tokens[$i]->id])) break;

src/test/php/lang/reflection/unittest/FromAttributesTest.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public function imports() {
3131
);
3232
}
3333

34+
#[Test]
35+
public function imports_from_eval() {
36+
$name= eval('class FromAttributesTest_eval { } return FromAttributesTest_eval::class;');
37+
Assert::equals([], (new FromAttributes())->imports(new ReflectionClass($name)));
38+
}
39+
3440
#[Test]
3541
public function evaluate_constant() {
3642
Assert::equals(

0 commit comments

Comments
 (0)