Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/ReflectionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public static function locateClassFile(string $fullClassName): string
) {
$refClass = new \ReflectionClass($fullClassName);
$classFileName = $refClass->getFileName();
// A class declared by eval() reports a pseudo file name ("file.php(12) : eval()'d code"), which
// can not be read: report it as not found instead of letting the parser warn on a missing file
if ($classFileName !== false && !is_file($classFileName)) {
throw new InvalidArgumentException("Class $fullClassName is not declared in a file: $classFileName");
}
} else {
if (self::$locator === null) {
throw new \LogicException('ReflectionEngine locator is not initialized. Call ReflectionEngine::init() first.');
Expand Down
62 changes: 62 additions & 0 deletions tests/EvalDeclaredParentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);
/**
* Parser Reflection API
*
* @copyright Copyright 2026, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\ParserReflection;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
* A class declared by eval() reports a pseudo file name ("file.php(12) : eval()'d code"): reflecting it, or a class
* inheriting from it, must not try to read that file
*/
final class EvalDeclaredParentTest extends TestCase
{
private const string PARENT_CLASS = 'Go\ParserReflection\Stub\EvalDeclared\EvalDeclaredParent';
private const string CHILD_CLASS = 'Go\ParserReflection\Stub\EvalDeclared\ChildOfEvalDeclaredParent';

public static function setUpBeforeClass(): void
{
if (!class_exists(self::PARENT_CLASS, false)) {
eval('namespace Go\ParserReflection\Stub\EvalDeclared; class EvalDeclaredParent { public function inheritedMethod(): int { return 1; } }');
}
}

public function testLocatingAnEvalDeclaredClassReportsItAsNotFound(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Class ' . self::PARENT_CLASS . ' is not declared in a file');

ReflectionEngine::locateClassFile(self::PARENT_CLASS);
}

public function testMethodsInheritedFromAnEvalDeclaredParentAreReflectedWithoutWarnings(): void
{
$file = new ReflectionFile(__DIR__ . '/Stub/FileWithEvalDeclaredParent.php');
$class = $file->getFileNamespace('Go\ParserReflection\Stub\EvalDeclared')->getClass(self::CHILD_CLASS);
$errors = [];
set_error_handler(static function (int $type, string $message) use (&$errors): bool {
$errors[] = $message;

return true;
});
try {
$methods = $class->getMethods();
} finally {
restore_error_handler();
}

$this->assertSame([], $errors);
$names = array_map(static fn (\ReflectionMethod $method): string => $method->getName(), $methods);
$this->assertSame(['ownMethod', 'inheritedMethod'], $names);
}
}
22 changes: 22 additions & 0 deletions tests/Stub/FileWithEvalDeclaredParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Parser Reflection API
*
* @copyright Copyright 2026, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
declare(strict_types=1);

namespace Go\ParserReflection\Stub\EvalDeclared;

/**
* Its parent is declared by eval() in the test, so it has no source file
*/
class ChildOfEvalDeclaredParent extends EvalDeclaredParent
{
public function ownMethod(): void
{
}
}
Loading