Skip to content

fix(array_map): keep the null of a nullable callback param when several arrays are mapped - #15449

Open
jirkace wants to merge 1 commit into
vimeo:masterfrom
jirkace:fix/14263-array-map-several-arrays-nullable-params
Open

jirkace wants to merge 1 commit into
vimeo:masterfrom
jirkace:fix/14263-array-map-several-arrays-nullable-params

Conversation

@jirkace

@jirkace jirkace commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Psalm narrows a nullable parameter of an array_map() callback to the element type when several arrays are mapped, although PHP passes null for the elements a shorter array lacks.

Before

<?php
final class Mode {}

/**
 * @param list<string> $names
 * @param array<int, Mode> $modes
 * @return list<string>
 */
function describeAll(array $names, array $modes): array
{
    return array_map(
        static fn(string $name, ?Mode $mode = null): string => $mode === null ? $name : $name . "!",
        // InvalidParamDefault: Default value type null for argument 2 of method  does not match the given type Mode
        $names,
        $modes,
    );
}

After

        static fn(string $name, ?Mode $mode = null): string => $mode === null ? $name : $name . "!", // no issue

Fixes #14263

…al arrays are mapped vimeo#14263

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • array_map() with two or more arrays: a callback parameter whose declared type (native or docblock) is nullable keeps null next to the element type (?Mode $mode is Mode|null, it was Mode). This removes the InvalidParamDefault, DocblockTypeContradiction and RedundantConditionGivenDocblockType of the issue. PHP pads the shorter arrays with null and does not use the callback's default: array_map(fn($a, $b = "d") => [$a, $b], ["x", "y"], [1]) gives [["x", 1], ["y", null]] on PHP 8.4.
  • Unchanged: a parameter without a type, or with a non-nullable one, still gets the element type; with a single array a nullable parameter is still narrowed to the element type (nothing is padded).
  • I first added null for every parameter when several arrays are mapped, which is what PHP does in general. On Laravel (src/Illuminate) that added two PossiblyNullOperand for array_map(fn($a, $b) => $a . $b, $x, $y) with arrays of the same length, so the change is limited to parameters that say they accept null.
  • Perf corpora with the final change, measured locally (errorLevel="1"): Laravel src/Illuminate 27 720 issues before and after. Symfony was measured with the wider first version only: 11 773 before and after.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 795.4 114.1 $\color{gray}{\textsf{+0.6\%}}$ 25 814 52 365 819 0
✅ symfony 3 605.0 284.4 $\color{gray}{\textsf{+0.3\%}}$ 52 445 100 806 1 891 0
✅ doctrine-orm 339.1 24.6 $\color{gray}{\textsf{-0.2\%}}$ 3 352 5 800 0 0
✅ psl 624.1 31.0 $\color{gray}{\textsf{-0.6\%}}$ 1 296 5 125 2 0
✅ phpunit 434.9 22.9 $\color{gray}{\textsf{+0.2\%}}$ 1 575 2 363 18 0
✅ php-parser 229.1 15.5 $\color{gray}{\textsf{-0.8\%}}$ 17 032 21 091 0 0
✅ tempest 915.6 41.2 $\color{gray}{\textsf{-1.5\%}}$ 5 511 11 032 288 0
✅ amphp-amp 48.7 1.9 $\color{gray}{\textsf{+9.7\%}}$ 30 57 0 0
✅ webmozarts-assert 282.7 5.6 $\color{gray}{\textsf{+1.6\%}}$ 576 2 740 0 0
✅ guzzle-promises 47.3 1.9 $\color{gray}{\textsf{-2.1\%}}$ 148 403 2 0
✅ monolog 140.3 6.9 $\color{gray}{\textsf{-4.7\%}}$ 937 2 483 31 0
✅ drupal 5 084.5 182.6 $\color{gray}{\textsf{-1.2\%}}$ 97 868 168 986 687 0
✅ wordpress 2 035.5 224.2 $\color{gray}{\textsf{+0.5\%}}$ 143 503 678 445 479 1
Total 15 582.4 956.9 $\color{gray}{\textsf{-0.1\%}}$ 350 087 1 051 696 4 217 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 side by side on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a second round was taken and the faster run of each side 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.
  • Profiling: when both sides support it, the measured runs use --debug-performance=PATH for the hot-files block, which adds the same small CPU and memory cost to both sides.
  • 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.

head d1a5dac · base f122213 (merge-base) · workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A closure parameter's native nullable type is replaced by the type inferred from array_map()

1 participant