packages/deriver/tests/Unit/Deriver/Evaluator/Index/ControlDependenciesTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Index;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\Evaluators;
10use Deriver\Deriver\Evaluator\Call\Registrations;
11use Deriver\Deriver\Evaluator\Evaluator;
12use Deriver\Deriver\Evaluator\EvaluatorConfig;
13use Deriver\Deriver\Evaluator\Index\ControlDependencies as Subject;
14use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
15use Deriver\Deriver\Evaluator\Request\Budget;
16use Deriver\Deriver\Evaluator\Scope\Context;
17use Deriver\Deriver\Evaluator\Scope\Frame;
18use Deriver\Deriver\Evaluator\TargetProfile;
19use Deriver\Deriver\Evaluator\Term;
20use Deriver\Deriver\Lowerer\Declaration\Index;
21use Deriver\Deriver\MicroEvaluatorConversion;
22use Deriver\Deriver\Parser\Node\File;
23use Deriver\Deriver\Parser\Parser;
24use Deriver\Deriver\Parser\ParserConfig;
25use Deriver\Deriver\Providers\ProviderExpansion;
26use Deriver\Deriver\Resolver\Resolver;
27use Deriver\Deriver\Sources\Path;
28use Deriver\Deriver\Sources\Source;
29use Deriver\Deriver\Sources\Sources;
30use PHPUnit\Framework\Attributes\CoversNothing;
31use PHPUnit\Framework\TestCase;
32
33#[CoversNothing]
34final class ControlDependenciesTest extends TestCase
35{
36 public function testBuildDoesNotControlAJoinWithIrrelevantBranches(): void
37 {
38 $source = 'function target($b){if($b){$x=1;}else{$x=2;}return 42;}';
39 $configuration = new DeriverConfig();
40 $budget = new Budget();
41 $contributions = new Contributions();
42 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
43 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
44 $contributions1 = $providers->contributions;
45 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
46 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
47 $snapshotId = 'test';
48 $input = $providers->input;
49 $parser = new Parser(new ParserConfig($target->version));
50 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
51 $program = new Index($snapshotId, $trees);
52 $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
53 $symbol = 'target';
54 $graph = $engine->context->index->graph($symbol);
55 self::assertNotNull($graph);
56 $frame = new Frame($graph, 'source:' . $symbol);
57 $returns = $frame->graph->returns();
58 self::assertSame([], (new Subject())->build($frame->graph)[$returns[0][0]] ?? []);
59 }
60 public function testPostdominatorsIncludesTheSingleExit(): void
61 {
62 $source = 'function target(){return 42;}';
63 $configuration = new DeriverConfig();
64 $budget = new Budget();
65 $contributions = new Contributions();
66 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
67 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
68 $contributions1 = $providers->contributions;
69 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
70 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
71 $snapshotId = 'test';
72 $input = $providers->input;
73 $parser = new Parser(new ParserConfig($target->version));
74 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
75 $program = new Index($snapshotId, $trees);
76 $evaluator = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
77 $symbol = 'target';
78 $graph1 = $evaluator->context->index->graph($symbol);
79 self::assertNotNull($graph1);
80 $graph = (new Frame($graph1, 'source:' . $symbol))->graph;
81 self::assertSame([-1 => -1, 0 => -1], (new Subject())->postdominators($graph, null));
82 }
83 public function testOrderStartsAtTheVirtualExit(): void
84 {
85 $source = 'function target(){return 42;}';
86 $configuration = new DeriverConfig();
87 $budget = new Budget();
88 $contributions = new Contributions();
89 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
90 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
91 $contributions1 = $providers->contributions;
92 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
93 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
94 $snapshotId = 'test';
95 $input = $providers->input;
96 $parser = new Parser(new ParserConfig($target->version));
97 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
98 $program = new Index($snapshotId, $trees);
99 $evaluator = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
100 $symbol = 'target';
101 $graph1 = $evaluator->context->index->graph($symbol);
102 self::assertNotNull($graph1);
103 $graph = (new Frame($graph1, 'source:' . $symbol))->graph;
104 self::assertSame([-1, 0], (new Subject())->order($graph));
105 }
106 public function testIntersectFindsTheNearestSharedAncestor(): void
107 {
108 self::assertSame(2, (new Subject())->intersect(0, 1, [-1 => -1, 2 => -1, 0 => 2, 1 => 2], [-1 => 0, 2 => 1, 0 => 2, 1 => 3]));
109 }
110}
111