diff --git a/src/ReflectionEngine.php b/src/ReflectionEngine.php index fc02da2..538cc03 100644 --- a/src/ReflectionEngine.php +++ b/src/ReflectionEngine.php @@ -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.'); diff --git a/tests/EvalDeclaredParentTest.php b/tests/EvalDeclaredParentTest.php new file mode 100644 index 0000000..b72f6e0 --- /dev/null +++ b/tests/EvalDeclaredParentTest.php @@ -0,0 +1,62 @@ + + * + * 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); + } +} diff --git a/tests/Stub/FileWithEvalDeclaredParent.php b/tests/Stub/FileWithEvalDeclaredParent.php new file mode 100644 index 0000000..485db12 --- /dev/null +++ b/tests/Stub/FileWithEvalDeclaredParent.php @@ -0,0 +1,22 @@ + + * + * 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 + { + } +}