Skip to content

fix(stubs): ArrayObject and ArrayIterator offsetSet() accept a null offset - #14516

Merged
orklah merged 7 commits into
vimeo:masterfrom
jirkace:fix/14499-arrayobject-append
Nov 23, 2026
Merged

orklah merged 7 commits into
vimeo:masterfrom
jirkace:fix/14499-arrayobject-append

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Appending ($o[] = ...) to a typed ArrayObject or ArrayIterator reports a false NullArgument.

Before

<?php
final class Bag {
    public ArrayObject $items;
    /** @var ArrayObject<int, int> */
    public ArrayObject $typed;
    /** @var ArrayIterator<int, int> */
    public ArrayIterator $iterator;

    public function __construct() {
        $this->items = new ArrayObject();
        $this->typed = new ArrayObject();
        $this->iterator = new ArrayIterator();
    }
}

$bag = new Bag();
$bag->items[] = 1;    // NullArgument
$bag->typed[] = 1;    // NullArgument
$bag->iterator[] = 1; // NullArgument
$bag->typed->offsetSet(null, 2); // NullArgument

After

$bag->items[] = 1;    // no issue
$bag->typed[] = 1;    // no issue
$bag->iterator[] = 1; // no issue
$bag->typed->offsetSet(null, 2); // no issue

Fixes #14499

…ffset vimeo#14499

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • ArrayObject::offsetSet() and ArrayIterator::offsetSet() stubs take TKey|null $offset (null appends), like ArrayAccess::offsetSet(): no more NullArgument for $o[] = ... or offsetSet(null, ...) on a typed instance (a property or a @var-typed variable; an unannotated new ArrayObject() was not affected).
  • Checked on PHP 8.4: both append with a null offset; WeakMap (Cannot append to WeakMap) and SplFixedArray (int $offset) are left as they are.
  • Unchanged: a key of another type is still InvalidArgument (testInvalidCode@stringOffsetOfAnIntKeyedArrayObject).

Verified: ArrayAccessTest::testValidCode@appendToATypedArrayObjectOrArrayIterator fails on master and passes here. Full self-analysis: no new issue (only 2 MixedArgument from my local revolt/event-loop v1.0.9, unrelated).

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 772 80.2 $\color{gray}{\textsf{+0.7\%}}$ 25 784 53 058 743 0
✅ symfony 3 619 286.3 $\color{gray}{\textsf{-2.2\%}}$ 51 754 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-1}}$ 100 577 $\color{green}{\textsf{-6}}$ 1 582 0
✅ doctrine-orm 380 15.7 $\color{gray}{\textsf{-3.9\%}}$ 3 457 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-1}}$ 6 209 $\color{green}{\textsf{-6}}$ 0 0
✅ psl 618 21.0 $\color{gray}{\textsf{+1.8\%}}$ 1 390 5 613 1 0
✅ phpunit 456 16.4 $\color{gray}{\textsf{+0.9\%}}$ 1 735 3 418 18 0
✅ php-parser 226 10.9 $\color{gray}{\textsf{-0.3\%}}$ 17 116 21 187 0 0
✅ tempest 898 28.9 $\color{gray}{\textsf{+0.8\%}}$ 5 092 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-1}}$ 10 561 $\color{green}{\textsf{-6}}$ 275 0
✅ amphp-amp 48 1.2 $\color{gray}{\textsf{-4.0\%}}$ 29 49 0 0
✅ webmozarts-assert 273 3.5 $\color{gray}{\textsf{-1.1\%}}$ 575 2 714 0 0
✅ guzzle-promises 46 1.4 $\color{gray}{\textsf{+5.3\%}}$ 156 436 2 0
✅ monolog 136 4.7 $\color{gray}{\textsf{+0.9\%}}$ 918 2 491 11 0
✅ drupal 4 844 211.3 $\color{gray}{\textsf{+1.9\%}}$ 97 854 169 770 575 0
✅ wordpress 2 042 174.7 $\color{gray}{\textsf{-1.2\%}}$ 143 516 678 190 461 1
Total 15 358 856.3 $\color{gray}{\textsf{-0.5\%}}$ 349 376 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-3}}$ 1 054 273 $\color{green}{\textsf{-18}}$ 3 668 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.
✅ symfony: issues +0 −1

Memory 3 619 → 3 619 MB (<0.1 %) · Wall 292.8 → 286.3 s (−2.2 %)

Weighted 100 583 → 100 577 (−6, −0.01 %): L6 −1

Issue type added removed
NullArgument 0 1

Removed (1)

  • src/Symfony/Component/HttpClient/TraceableHttpClient.php:60 NullArgument (error): Argument 1 of ArrayObject::offsetSet cannot be null, null value provided to parameter with type array-key
✅ doctrine-orm: issues +0 −1

Memory 380 → 380 MB (<0.1 %) · Wall 16.3 → 15.7 s (−3.9 %)

Weighted 6 215 → 6 209 (−6, −0.10 %): L6 −1

Issue type added removed
NullArgument 0 1

Removed (1)

  • src/Mapping/Driver/AttributeReader.php:125 NullArgument (error): Argument 1 of RepeatableAttributeCollection::offsetSet cannot be null, null value provided to parameter with type int
✅ tempest: issues +0 −1

Memory 898 → 898 MB (+0.0 %) · Wall 28.7 → 28.9 s (+0.8 %)

Weighted 10 567 → 10 561 (−6, −0.06 %): L6 −1

Issue type added removed
NullArgument 0 1

Removed (1)

  • packages/container/src/GenericContainer.php:317 NullArgument (error): Argument 1 of ArrayIterator::offsetSet cannot be null, null value provided to parameter with type array-key

head 4b8f755 · base 9bdbe63 (merge-base) · workflow run

jirkace and others added 2 commits November 22, 2026 15:09
…hat stands for never vimeo#14499

`new ArrayObject()` binds its templates to type variables standing for
`never` until something is written. Combining the param type `TKey|null`
dropped such a variable, so the param became `null` and a write under a
key reported `InvalidArgument: ... expects null`. Keep the variable when
the bound param type is combined and expanded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change (addition), after the perf report of the previous head:

  • The previous head added a false positive in drupal, Core/Config/MemoryStorage.php:34: InvalidArgument: Argument 1 of ArrayObject::offsetSet expects null, but string provided for $this->config = new \ArrayObject(); $this->config[$collection] = [];. new ArrayObject() binds TKey to a type variable that stands for never until something is written. Combining the new param type TKey|null dropped that variable, so the param became null. Master has the same bug for any generic param T|null of an object built empty, e.g. a Bag::set(K|null $key) after new Bag(). The stub change only made ArrayObject/ArrayIterator reach it.
  • Fixed in 6c31e8b: the bound param type keeps such a variable when it is combined (TemplateStandinTypeReplacer::replace()) and expanded (TypeExpander::expandUnion()).
  • New tests: ArrayAccessTest::testValidCode@writeUnderAKeyOfANewArrayObjectOrArrayIterator (fails on the previous head) and TypeVariableTest::testValidCode@paramOfATemplateOrNullTakesAValueForAnObjectBuiltEmpty (fails on master too). I also ran these single cases and they pass: appendToATypedArrayObjectOrArrayIterator, five TypeVariableTest invalid cases (unionAlternativesAllViolated, typeVariableBoundViolation, constructorlessConstructionKeepsTemplateConstraint, argumentRequirementThenInvalidWrite, constructorlessConstructionSeesValuesAddedAfterward) and three valid ones.
  • Self-analysis shows only the two PoolEventLoopDriverTest MixedArgument errors that I also get on master locally. phpcs is clean. Master is merged into the branch.
  • Not run: the full suite (CI runs it) and the benchmark projects.

jirkace and others added 4 commits November 22, 2026 16:40
…is preserved vimeo#14499

Returning the variable unchanged from TypeExpander kept the cached ids of the types holding it after its bounds grew. Rebuild it again and keep a never-bound variable in one combine helper, also when another part of the union is expanded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	stubs/CoreGenericClasses.phpstub
#	stubs/CoreGenericIterators.phpstub
#	tests/ArrayAccessTest.php
# Conflicts:
#	tests/ArrayAccessTest.php
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 2026
@orklah

orklah commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jirkace!

@orklah
orklah merged commit ca41f7d into vimeo:master Nov 23, 2026
72 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive NullArgument when appending to an ArrayObject stored in a property

2 participants