packages/deriver/tests/Unit/Deriver/Evaluator/Expression/OriginsTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Expression;
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\Enumeration\Choices;
12use Deriver\Deriver\Evaluator\Evaluator;
13use Deriver\Deriver\Evaluator\EvaluatorConfig;
14use Deriver\Deriver\Evaluator\Evidence\EvidenceKind;
15use Deriver\Deriver\Evaluator\Evidence\Frontier;
16use Deriver\Deriver\Evaluator\Expression\Origins;
17use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
18use Deriver\Deriver\Evaluator\Request\Budget;
19use Deriver\Deriver\Evaluator\Request\ReturnQuery;
20use Deriver\Deriver\Evaluator\Request\ValueQuery;
21use Deriver\Deriver\Evaluator\Scope\Context;
22use Deriver\Deriver\Evaluator\Scope\Frame;
23use Deriver\Deriver\Evaluator\TargetProfile;
24use Deriver\Deriver\Evaluator\Term;
25use Deriver\Deriver\Evaluator\TermKind;
26use Deriver\Deriver\Lowerer\Declaration\Index;
27use Deriver\Deriver\MicroEvaluatorConversion;
28use Deriver\Deriver\Parser\Node\File;
29use Deriver\Deriver\Parser\Parser;
30use Deriver\Deriver\Parser\ParserConfig;
31use Deriver\Deriver\Providers\ProviderExpansion;
32use Deriver\Deriver\Publication\CandidateQuery;
33use Deriver\Deriver\Resolver\Resolver;
34use Deriver\Deriver\Selection\CallObservations;
35use Deriver\Deriver\SnapshotIdentity;
36use Deriver\Deriver\Sources\Path;
37use Deriver\Deriver\Sources\Source;
38use Deriver\Deriver\Sources\Sources;
39use PHPUnit\Framework\Attributes\CoversNothing;
40use PHPUnit\Framework\TestCase;
41
42#[CoversNothing]
43final class OriginsTest extends TestCase
44{
45 public function testParameterRetainsExternalInputThroughSameNamedForwardingMethods(): void
46 {
47 $source = new Source('forwarding.php', '<?php
48 class Service { public function create(string $input): string { return $input; } }
49 class Controller { public function create(Service $service, string $input): string { return $service->create($input); } }
50 ');
51 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse($source));
52 $context = new Context(new DefinitionIndex(new Index('forwarding', [$tree])), new EvaluatorConfig(), new Evaluators(), new Budget());
53 $graph = $context->index->graph('Service::create');
54 self::assertNotNull($graph);
55
56 $result = (new Evaluator($context))->returns(new Frame($graph, 'source:Service::create'), 64);
57 $rows = iterator_to_array((new Choices())->alternatives($result), false);
58
59 self::assertCount(1, $rows);
60 self::assertSame('$input', $rows[0][0]->literal);
61 self::assertSame('EXTERNAL_INPUT', $rows[0][0]->attributes['reason']);
62 self::assertSame('Controller::create', $rows[0][0]->attributes['scope']);
63 }
64
65 public function testCallersReturnsNullWhenOnlyUnrelatedMethodsHaveCallSites(): void
66 {
67 $source = new Source('unrelated.php', '<?php
68 class Controller { public function create(string $input): string { return $input; } }
69 class Service { public function create(string $input): string { return $input; } }
70 function caller(Service $service) { return $service->create("service input"); }
71 ');
72 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse($source));
73 $context = new Context(new DefinitionIndex(new Index('unrelated', [$tree])), new EvaluatorConfig(), new Evaluators(), new Budget());
74 $graph = $context->index->graph('Controller::create');
75 self::assertNotNull($graph);
76 self::assertNotEmpty($context->index->callers('Controller::create'));
77
78 self::assertNull((new Origins(new Evaluator($context)))->callers(new Frame($graph, 'source:Controller::create'), 'input', 64));
79 }
80
81 public function testCallersPreservesAnEmptyArgumentFromAMatchingCall(): void
82 {
83 $source = new Source('empty.php', '<?php function target($input) { return $input; } function caller($input) { return target($input); }');
84 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse($source));
85 $context = new Context(new DefinitionIndex(new Index('empty', [$tree])), new EvaluatorConfig(), new Evaluators(), new Budget());
86 $graph = $context->index->graph('target');
87 $caller = $context->index->graph('caller');
88 self::assertNotNull($graph);
89 self::assertNotNull($caller);
90 $context->entryFrames['caller'] = [new Frame($caller, 'entry:caller', ['input' => new Term(TermKind::Choice)], invocation: true)];
91
92 $result = (new Origins(new Evaluator($context)))->callers(new Frame($graph, 'source:target'), 'input', 64);
93
94 self::assertNotNull($result);
95 self::assertSame([], iterator_to_array((new Choices())->alternatives($result), false));
96 }
97
98 public function testCallersKeepsMatchingActualsWhenOtherClassesUseTheSameMethodName(): void
99 {
100 $source = new Source('matching.php', '<?php
101 class Controller { public function create(string $input): string { return $input; } }
102 class Service { public function create(string $input): string { return $input; } }
103 function caller(Service $service, Controller $controller) { $service->create("service input"); return $controller->create("controller input"); }
104 ');
105 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse($source));
106 $context = new Context(new DefinitionIndex(new Index('matching', [$tree])), new EvaluatorConfig(), new Evaluators(), new Budget());
107 $graph = $context->index->graph('Controller::create');
108 self::assertNotNull($graph);
109
110 $result = (new Evaluator($context))->returns(new Frame($graph, 'source:Controller::create'), 64);
111 $rows = iterator_to_array((new Choices())->alternatives($result), false);
112
113 self::assertCount(1, $rows);
114 self::assertSame('controller input', $rows[0][0]->native());
115 }
116
117 public function testParameterPreservesAnUnboundInput(): void
118 {
119 $source = 'function target(int $x){return $x;}';
120 $configuration = new DeriverConfig();
121 $budget = new Budget();
122 $contributions = new Contributions();
123 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
124 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
125 $contributions1 = $providers->contributions;
126 $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);
127 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
128 $snapshotId = 'test';
129 $input = $providers->input;
130 $parser = new Parser(new ParserConfig($target->version));
131 $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));
132 $program = new Index($snapshotId, $trees);
133 $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
134 $symbol = 'target';
135 $graph = $e->context->index->graph($symbol);
136 self::assertNotNull($graph);
137 $r = $e->returns(new Frame($graph, 'source:' . $symbol), 64);
138 self::assertSame('$x', $r->literal);
139 self::assertSame('int', $r->attributes['type']);
140 }
141 public function testCallersKeepsActualsCorrelated(): void
142 {
143 $prepared1Input = new Sources([new Source('candidate.php', '<?php ' . 'function target($x){observe($x);}function caller(){target(1);target(2);}')]);
144 $prepared1Configuration = new DeriverConfig();
145 $prepared1Contributions = new Contributions();
146 $prepared1Target = new TargetProfile($prepared1Configuration->phpVersion, $prepared1Configuration->integerBits, $prepared1Configuration->floatPrecision);
147 $prepared1Providers = new ProviderExpansion($prepared1Input, array_map(Term::fromNative(...), $prepared1Configuration->environment), $prepared1Contributions);
148 $contributions = $prepared1Providers->contributions;
149 $prepared1Evaluators = new Evaluators(new TargetProfile($prepared1Configuration->phpVersion, $prepared1Configuration->integerBits, $prepared1Configuration->floatPrecision), new Registrations($contributions->evaluators, $contributions->providers, $contributions->domains, $contributions->intrinsics, $contributions->stateSlots), $prepared1Configuration->standardEvaluators);
150 $prepared1Settings = new EvaluatorConfig($prepared1Target, $prepared1Providers->environment, $prepared1Configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $prepared1Configuration->evaluators));
151 $prepared1Identity = new SnapshotIdentity($prepared1Providers->input, $prepared1Settings, $prepared1Evaluators, $prepared1Providers->manifest);
152 $snapshotId = $prepared1Identity->id;
153 $input = $prepared1Providers->input;
154 $parser = new Parser(new ParserConfig($prepared1Target->version));
155 $trees = array_map(static fn (Source $source): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source->path), $source->contents, $source->declarationsOnly))), iterator_to_array($input, false));
156 $prepared1Program = new Index($snapshotId, $trees);
157 $prepared1Snapshot = $prepared1Identity->snapshot(array_map(Frontier::fromDiagnostic(...), $prepared1Program->diagnostics()));
158 $s = new CandidateQuery(new DefinitionIndex($prepared1Program), $prepared1Settings, $prepared1Evaluators, $prepared1Snapshot);
159 $result = $s->derive(new ValueQuery((new CallObservations($prepared1Program, $prepared1Evaluators))->find('observe')[0]->argument(0)));
160 $slot = 'value';
161 self::assertSame([1, 2], array_map(static fn (Term $value) => $value->native(), array_map(static fn ($candidate): Term => $candidate->result instanceof Term ? $candidate->result : Term::fromNative($candidate->result), iterator_to_array($result, false))));
162 }
163 public function testPropertyCollectsOrdinaryWrites(): void
164 {
165 $prepared2Input = new Sources([new Source('candidate.php', '<?php ' . 'class B{public $x=1;function set(){$this->x=2;}function get(){observe($this->x);}}')]);
166 $prepared2Configuration = new DeriverConfig();
167 $prepared2Contributions = new Contributions();
168 $prepared2Target = new TargetProfile($prepared2Configuration->phpVersion, $prepared2Configuration->integerBits, $prepared2Configuration->floatPrecision);
169 $prepared2Providers = new ProviderExpansion($prepared2Input, array_map(Term::fromNative(...), $prepared2Configuration->environment), $prepared2Contributions);
170 $contributions = $prepared2Providers->contributions;
171 $prepared2Evaluators = new Evaluators(new TargetProfile($prepared2Configuration->phpVersion, $prepared2Configuration->integerBits, $prepared2Configuration->floatPrecision), new Registrations($contributions->evaluators, $contributions->providers, $contributions->domains, $contributions->intrinsics, $contributions->stateSlots), $prepared2Configuration->standardEvaluators);
172 $prepared2Settings = new EvaluatorConfig($prepared2Target, $prepared2Providers->environment, $prepared2Configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $prepared2Configuration->evaluators));
173 $prepared2Identity = new SnapshotIdentity($prepared2Providers->input, $prepared2Settings, $prepared2Evaluators, $prepared2Providers->manifest);
174 $snapshotId = $prepared2Identity->id;
175 $input = $prepared2Providers->input;
176 $parser = new Parser(new ParserConfig($prepared2Target->version));
177 $trees = array_map(static fn (Source $source): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source->path), $source->contents, $source->declarationsOnly))), iterator_to_array($input, false));
178 $prepared2Program = new Index($snapshotId, $trees);
179 $prepared2Snapshot = $prepared2Identity->snapshot(array_map(Frontier::fromDiagnostic(...), $prepared2Program->diagnostics()));
180 $s = new CandidateQuery(new DefinitionIndex($prepared2Program), $prepared2Settings, $prepared2Evaluators, $prepared2Snapshot);
181 $result = $s->derive(new ValueQuery((new CallObservations($prepared2Program, $prepared2Evaluators))->find('observe')[0]->argument(0)));
182 $slot = 'value';
183 self::assertSame([1, 2], array_map(static fn (Term $value) => $value->native(), array_map(static fn ($candidate): Term => $candidate->result instanceof Term ? $candidate->result : Term::fromNative($candidate->result), iterator_to_array($result, false))));
184 }
185 public function testUnboundKeepsTheDeclaredInputType(): void
186 {
187 $source = 'function target(int $x){return $x;}';
188 $configuration = new DeriverConfig();
189 $budget = new Budget();
190 $contributions = new Contributions();
191 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
192 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
193 $contributions1 = $providers->contributions;
194 $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);
195 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
196 $snapshotId = 'test';
197 $input = $providers->input;
198 $parser = new Parser(new ParserConfig($target->version));
199 $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));
200 $program = new Index($snapshotId, $trees);
201 $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
202 $symbol = 'target';
203 $graph = $e->context->index->graph($symbol);
204 self::assertNotNull($graph);
205 $f = new Frame($graph, 'source:' . $symbol);
206 $operation = 'local';
207 $instruction = array_values(array_filter($f->graph->definitions, static fn ($instruction): bool => $instruction->operation === $operation))[0] ?? null;
208 self::assertNotNull($instruction);
209 $v = (new Origins($e))->unbound($f, $instruction, 'int', true);
210 self::assertSame('int', $v->attributes['type']);
211 self::assertSame('EXTERNAL_INPUT', $v->attributes['reason']);
212 }
213 public function testBindingEvidenceRetainsActualAndFormalRelation(): void
214 {
215 $source = 'function target($x){return $x;}function caller(){return target(9);}';
216 $configuration = new DeriverConfig();
217 $budget = new Budget();
218 $contributions = new Contributions();
219 $prepared3Input = new Sources([new Source('candidate.php', '<?php ' . $source)]);
220 $prepared3Configuration = $configuration;
221 $prepared3Contributions = $contributions;
222 $prepared3Target = new TargetProfile($prepared3Configuration->phpVersion, $prepared3Configuration->integerBits, $prepared3Configuration->floatPrecision);
223 $prepared3Providers = new ProviderExpansion($prepared3Input, array_map(Term::fromNative(...), $prepared3Configuration->environment), $prepared3Contributions);
224 $contributions1 = $prepared3Providers->contributions;
225 $prepared3Evaluators = new Evaluators(new TargetProfile($prepared3Configuration->phpVersion, $prepared3Configuration->integerBits, $prepared3Configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $prepared3Configuration->standardEvaluators);
226 $prepared3Settings = new EvaluatorConfig($prepared3Target, $prepared3Providers->environment, $prepared3Configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $prepared3Configuration->evaluators));
227 $prepared3Identity = new SnapshotIdentity($prepared3Providers->input, $prepared3Settings, $prepared3Evaluators, $prepared3Providers->manifest);
228 $snapshotId = $prepared3Identity->id;
229 $input = $prepared3Providers->input;
230 $parser = new Parser(new ParserConfig($prepared3Target->version));
231 $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));
232 $prepared3Program = new Index($snapshotId, $trees);
233 $prepared3Snapshot = $prepared3Identity->snapshot(array_map(Frontier::fromDiagnostic(...), $prepared3Program->diagnostics()));
234 $result = (new CandidateQuery(new DefinitionIndex($prepared3Program), $prepared3Settings, $prepared3Evaluators, $prepared3Snapshot))->derive(new ReturnQuery('target', budget: $budget));
235 self::assertSame(9, $result->forCaller('caller')[0]->result);
236 self::assertContains(EvidenceKind::ArgumentBinding, array_column($result[0]->evidence[0]->nodes(), 'kind'));
237 }
238}
239