TypePHP 编译器
https://swoole.com/aot/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.5 KiB
44 lines
1.5 KiB
<?php
|
|
|
|
namespace TypePhp\Tests\Analysis;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
use PhpParser\Node\Stmt\Function_;
|
|
use PhpParser\NodeFinder;
|
|
use PhpParser\ParserFactory;
|
|
use PHPUnit\Framework\TestCase;
|
|
use TypePhp\Analysis\ReferenceCaptureAnalyzer;
|
|
|
|
final class ReferenceCaptureAnalyzerTest extends TestCase
|
|
{
|
|
public function testCollectsOnlyCapturesOwnedByTheCurrentFunctionScope(): void
|
|
{
|
|
$nodes = (new ParserFactory())->createForHostVersion()->parse(<<<'PHP'
|
|
<?php
|
|
function example(): void {
|
|
static $staticValue = '';
|
|
global $globalValue;
|
|
$closure = function () use (&$outer, $copy): void {
|
|
$nested = function () use (&$inner): void {};
|
|
};
|
|
}
|
|
PHP);
|
|
self::assertNotNull($nodes);
|
|
|
|
$finder = new NodeFinder();
|
|
$function = $finder->findFirstInstanceOf($nodes, Function_::class);
|
|
self::assertInstanceOf(Function_::class, $function);
|
|
|
|
$analysis = (new ReferenceCaptureAnalyzer())->analyze($function->stmts);
|
|
self::assertSame(['outer' => true], $analysis['captures']);
|
|
self::assertSame(
|
|
['staticValue' => true, 'globalValue' => true],
|
|
$analysis['nonLocals'],
|
|
);
|
|
|
|
$closure = $finder->findFirstInstanceOf($function->stmts, Closure::class);
|
|
self::assertInstanceOf(Closure::class, $closure);
|
|
$nestedAnalysis = (new ReferenceCaptureAnalyzer())->analyze($closure->stmts);
|
|
self::assertSame(['inner' => true], $nestedAnalysis['captures']);
|
|
}
|
|
}
|
|
|