packages/sql-semantics-sqlite/src/Model/Validation/LeafEmbedding.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Model\Validation;
6
7use SqlSemantics\Platform\Sqlite\Model\Lowering\Leaves;
8
9/**
10 * Checks that every operand leaf lowered from the source is part of the published structure.
11 *
12 * The check is by object identity against the values actually stored in the
13 * statement, not by counting consumed tokens.
14 *
15 * @visibility SqlSemantics
16 */
17final class LeafEmbedding
18{
19    /**
20     * Answers the first recorded leaf that is not among the reachable objects, or null when all are.
21     *
22     * @param list<object> $reachable Every object reachable from the statement
23     */
24    public function dropped(Leaves $leaves, array $reachable): ?object
25    {
26        $present = [];
27        foreach ($reachable as $object) {
28            $present[spl_object_id($object)] = $object;
29        }
30        foreach ($leaves->all() as $leaf) {
31            if (($present[spl_object_id($leaf)] ?? null) !== $leaf) {
32                return $leaf;
33            }
34        }
35
36        return null;
37    }
38}
39