parser-reflection icon indicating copy to clipboard operation
parser-reflection copied to clipboard

Included/Required files are not parsed

Open pdelre opened this issue 8 years ago • 6 comments

Files containing include/require statements do not parse the included files. This limits the library to autoloading/Locator only.

This also feels like a regression in goaop/[email protected] which utilized andrewsville/php-token-reflection.

I have an example test added in https://github.com/goaop/parser-reflection/compare/master...pdelre:included-files-not-parsed resulting in below.

Class Go\ParserReflection\Stub\RequiredClass was not found by locator
 /Users/pdelre/Projects/github/parser-reflection/src/ReflectionEngine.php:112
 /Users/pdelre/Projects/github/parser-reflection/src/ReflectionEngine.php:128
 /Users/pdelre/Projects/github/parser-reflection/src/ReflectionClass.php:45
 /Users/pdelre/Projects/github/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:447
 /Users/pdelre/Projects/github/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:916
 /Users/pdelre/Projects/github/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:319
 /Users/pdelre/Projects/github/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:305
 /Users/pdelre/Projects/github/parser-reflection/tests/ReflectionClassTest.php:193

pdelre avatar Dec 08 '16 16:12 pdelre

What if you have require_once ABS_PATH . '/path/to/file.php? There is no way to resolve that kind of stuff.

Having locator (that is autoloader based or not) is plus in any case.

Also I strongly advise to start using class autoloading. If that isn't an option, then creating custom locator would be the only option for now.

aik099 avatar Dec 08 '16 16:12 aik099

Agreed. Maybe this is an issue upstream in goaop/framework or Codeception/AspectMock then. I'm upgrading a monolith codebase where AspectMock/GoAOP had been working before upgrading to 2.0.0 for PHP 7.0 support.

pdelre avatar Dec 08 '16 17:12 pdelre

Hi! I agree with @aik099 that you should use custom loader for your classes that couldn't be found by composer. However, it would be nice to perform ad-hoc analysis of all required/included files to be able to analyse non-trivial code with includes.

Still, there is a question what should we do with dynamic includes? Like this: include __DIR__ . '/' . $variableHere . '/SomeName.php

lisachenko avatar Dec 12 '16 10:12 lisachenko

We also use includes via constants (include INCLUDE_PATH . '/SomeName.php';) and it could possibly support the ini's include_path (http://php.net/manual/en/ini.core.php#ini.include-path) as well.

pdelre avatar Dec 12 '16 20:12 pdelre

Crazy but possible solution is to re-register file:// stream, see https://github.com/nette/tester/pull/348/files

dg avatar Feb 04 '17 13:02 dg

@dg for stream wrappers it will work (this is how patchwork implemented). But stream wrappers affects Opcache and file loading process, adding additional overhead, so I ended with stream filters as more native way for PHP to process source code :)

I have a WIP tool: https://github.com/goaop/ast-manipulator/blob/master/src/Hook/StreamWrapperHook.php that uses the same technique.

lisachenko avatar Feb 04 '17 18:02 lisachenko