Skip to content

Commit

Permalink
Fix FromAttributes::imports() for classes created inside eval
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Sep 30, 2023
1 parent e6d549c commit 5750499
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ XP Reflection ChangeLog

## ?.?.? / ????-??-??

## 2.14.1 / 2023-09-30

* Fixed `FromAttributes::imports()` for classes created inside `eval`
(@thekid)

## 2.14.0 / 2023-09-23

* Added support for partial meta information - @thekid
Expand Down
8 changes: 6 additions & 2 deletions src/main/php/lang/meta/FromAttributes.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ public function ofParameter($method, $reflect) {
/**
* Returns imports used in the class file the given class was declared in
*
* @param \ReflectionClass $reflect
* @param ReflectionClass $reflect
* @return [:string]
*/
public function imports($reflect) {
static $break= [T_CLASS => true, T_INTERFACE => true, T_TRAIT => true, 372 /* T_ENUM */ => true];
static $types= [T_WHITESPACE => true, 44 => true, 59 => true, 123 => true];

$tokens= PhpToken::tokenize(file_get_contents($reflect->getFileName()));
// Exclude classes declared inside eval(), their declaration is not accessible
$file= $reflect->getFileName();
if (false !== strpos($file, ': eval')) return [];

$tokens= PhpToken::tokenize(file_get_contents($file));
$imports= [];
for ($i= 0, $s= sizeof($tokens); $i < $s; $i++) {
if (isset($break[$tokens[$i]->id])) break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public function imports() {
);
}

#[Test]
public function imports_from_eval() {
$name= eval('class FromAttributesTest_eval { } return FromAttributesTest_eval::class;');
Assert::equals([], (new FromAttributes())->imports(new ReflectionClass($name)));
}

#[Test]
public function evaluate_constant() {
Assert::equals(
Expand Down

0 comments on commit 5750499

Please sign in to comment.