Remove ArrayAccess from NodeList and enforce contiguous int keys - #1323
Open
spawnia wants to merge 3 commits into
Open
Remove ArrayAccess from NodeList and enforce contiguous int keys#1323spawnia wants to merge 3 commits into
ArrayAccess from NodeList and enforce contiguous int keys#1323spawnia wants to merge 3 commits into
Conversation
ArrayAccess from NodeList and enforce contiguous int keys
3 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the NodeList AST container to behave as a true list (contiguous int keys) and removes array-style access, then adjusts core visitor logic and test suite usages accordingly.
Changes:
- Replace
ArrayAccesssemantics onNodeListwith explicit list APIs (get/has/set/add/unset) and update internal iterator behavior. - Update visitor/editing logic and multiple tests to use
NodeListlist APIs instead of[]/offsetGet. - Document the behavioral change in
CHANGELOG.md.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/Utils/BuildSchemaTest.php | Switches definitions[0] to definitions->get(0) for the new NodeList API. |
| tests/Utils/AstGetOperationAstTest.php | Replaces offsetGet() access with get() in operation AST assertions. |
| tests/Language/VisitorTest.php | Updates NodeList parent/key assertions and element access to has()/get(). |
| tests/Language/SerializationTest.php | Iterates NodeLists using get() and normalizes path segments to strings. |
| tests/Language/ParserTest.php | Updates definitions[0] access to definitions->get(0). |
| tests/Language/NodeListTest.php | Refactors NodeList tests to reflect list-only behavior and new mutator APIs. |
| tests/Language/AST/NodeTest.php | Replaces array indexing with get() for nested NodeLists. |
| tests/Executor/ExecutorTest.php | Uses get() for definitions/selections NodeList indexing. |
| tests/Error/PrintErrorTest.php | Switches AST element access from [] to get(). |
| tests/Error/ErrorTest.php | Switches AST element access from [] to get(). |
| src/Validator/Rules/QueryComplexity.php | Collects variable definitions via NodeList->add() instead of []=. |
| src/Language/Visitor.php | Uses NodeList->set() and NodeList->get() in edit/traversal paths. |
| src/Language/AST/NodeList.php | Core change: remove ArrayAccess, add list APIs, and adjust iterator/splice/merge/clone behaviors. |
| CHANGELOG.md | Notes NodeList behavior change under “Unreleased”. |
Comments suppressed due to low confidence (2)
src/Language/AST/NodeList.php:98
NodeList::splice()always forwards the default$replacement = nulltoarray_splice(). In PHP, passingnullas the 4th argument inserts anullelement instead of performing a pure removal. Call sites likeSingleFieldSubscription($selections->splice(1, count($selections))) andVisitor($node->splice($editKey, 1)) expect removal with no replacement, so this will leavenullentries in the list and break downstream iteration/type expectations.
public function splice(int $offset, int $length, $replacement = null): NodeList
{
$spliced = \array_splice($this->nodes, $offset, $length, $replacement);
// @phpstan-ignore-next-line generic type mismatch
src/Language/AST/NodeList.php:56
NodeList::set()relies onassert()to enforce contiguous keys. Assertions are commonly disabled in production, so this does not reliably enforce the invariant advertised by the PR title. Also, without a real check, callers can create gaps (e.g.set(5, ...)) which will later cause surprises elsewhere. Consider validating the offset and throwingInvariantViolationwhen it would create a non-contiguous list.
public function set(int $offset, Node $value): void
{
$this->nodes[$offset] = $value;
assert($this->nodes === \array_values($this->nodes));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
26
to
29
| public function __construct(array $nodes) | ||
| { | ||
| $this->nodes = $nodes; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reintroduces #1315 and #1321.
I found that AST manipulation is common enough that we should not break it in a minor release.