github vimeo/psalm 7.22.0
7.22.0 Call-site variance

44 minutes ago

New AI, UX and @template syntax features.

Features

  • Call-site variance: covariant or contravariant before a type argument makes one param accept a generic of any subtype (or supertype) of it, without changing the class's template. As with Box<*>, values then go one way only: from a Box<covariant Node> you read Node, but can't put anything in. (#15096, @alies-dev)
    /** @param Handler<covariant Node> $handler */
    function register(Handler $handler): void {}
    
    register(new LeafHandler()); // a Handler<LeafNode>: no issue (was InvalidArgument)
  • psalm --explain=<issue> prints an issue's documentation (what it means, an example, how to fix it) from the installed version, offline and without a config. It takes the issue type, its shortcode or its psalm.dev link. (#15066, @alies-dev, @roxblnfk)
    vendor/bin/psalm --explain=PossiblyNullArgument
    vendor/bin/psalm --explain=078
    vendor/bin/psalm --explain=https://psalm.dev/078
  • AI coding agents: when Psalm runs under an agent (CLAUDECODE, CURSOR_AGENT, CODEX_THREAD_ID, AI_AGENT, …), it prints the compact format (one line per issue), no progress output even if CI is set, a one-line result (also 0 issues), and [*] after each issue --alter can fix. An explicit --output-format wins, and nothing changes outside an agent. See AI agents. (#15068, #15069, #15067, @alies-dev)
  • Crash reports name the plugin and hook that threw and the statement Psalm was analysing, save the full report to a file, and print a link that opens a prefilled issue on Psalm's or the plugin's tracker. (#15070, @alies-dev)
  • --no-taint-analysis skips taint analysis for one run (runTaintAnalysis is on by default), e.g. for a quick local check (#15078, @orklah).
  • New issues:
    • InternalFunction for a call of an @internal or @psalm-internal function from outside its namespace. (#15084, @orklah)
    • ParseError for constructor property promotion when analysing PHP 7.x. (#15086, @orklah)
  • --alter on PHP 8.2 writes true, false and null as native types (: true|null instead of : bool|null plus a docblock), and no longer adds docblocks that repeat a native type. (#15083, @orklah)

Performance

  • Narrowing a variable no longer copies the whole scope for every other variable, which made 7.18 about 2% slower than 7.17. (#15121, @alies-dev)

Fixes

Taint analysis

  • TaintedCallable for a tainted method name: $object->$name(), A::$name(). (#15072, @alies-dev)
  • strip_tags() with allowed tags is not an html escape: strip_tags($_GET["x"], "<a>") is TaintedHtml. (#14753, @arpitjain099)

Stubs and plugins

  • A stub's @return (or its own @psalm-overload) replaces the original's @return $this and overloads, so a stubbed call returns the stub's type again (a 7.19.0 regression). (#15090, @alies-dev)
  • No false MethodSignatureMismatch for an override that widens a param of a stubbed userland class, and float/int swaps PHP rejects are reported. (#15114, @orklah)
  • [] on Ds\Map, Ds\Vector and other Ds collections is typed and checked, and a stub class that leaves out a method of its interfaces still has it. (#15039, @orklah)
  • A function params provider returning null keeps the function's params checked. (#15040, @orklah)
  • Params providers can resolve [$this, 'method']: CallAnalyzer::getFunctionIdsFromCallableArg() takes the call's Context. (#15082, @orklah)
  • A return type provider of a class wins over its @mixin for a method behind __call()/__callStatic(). (#15020, @shu-chen-dot-ca)

Arrays and narrowing

  • isset() and !isset() on array offsets: a negated check drops the shapes that always have the offset and keeps the optional offsets it leaves; an empty array stays empty; a nested isset($a["x"][3]) narrows $a["x"]. (#15103, #15104, #15105, #15076, @orklah)
  • A switch case is analysed knowing the cases before it didn't match. (#14528, @jirkace)
  • Assigning an offset to a possibly-null array keeps the other offsets optional, and a nested write under a listed key of a shape with a mixed fallback keeps that key's type. (#14815, #14611, @jirkace)
  • is_int(), is_bool() and is_string() keep the emptiness of empty-scalar and non-empty-scalar. (#14431, @jirkace)
  • is_callable() on a list can be true. (#14319, @jirkace)
  • No MixedAssignment/MixedArgument in a foreach over an always-empty list, such as list<T> of a new covariant generic. (#15095, @alies-dev)

Types and templates

  • The false branch of a conditional type bounds the template by the rest of its bound. (#14647, @jirkace)
  • value-of<T> of a backed enum template is an array key, and $case->value of a T is a value-of<T>. (#14618, @jirkace)
  • An intersection merges its object shapes (Card&object{last4: string}&object{brand: string}). (#14457, @jirkace)
  • An argument may fit any declaration of a method that several members of an intersection declare. (#14977, @shu-chen-dot-ca)
  • static in the params of a @method tag resolves to the called class. (#14480, @jirkace)
  • void|never is void, not null: AssignmentToVoid is reported, and an always-exiting function is no InvalidReturnType. (#15087, @orklah)
  • pow() with a zero and a float operand is a float. (#14845, @ARAYN7011)
  • PHP_INT_MIN is negative, PHP_DEBUG/PHP_ZTS are 0|1 (bool since PHP 8.4), and $_SERVER["DOCUMENT_ROOT"], ["SCRIPT_FILENAME"] and ["PATH_TRANSLATED"] can be empty. (#15079, #15092, @orklah)
  • get_object_vars() doesn't include static properties. (#15011, @jirkace)

Functions, classes and properties

  • No false PropertyNotSetInConstructor for a redeclared promoted property or one assigned through $this->$name. (#15038, @orklah)
  • Overriding a property of a builtin class with its native type is no NonInvariantPropertyType. (#14623, @jirkace)
  • Calling a closure forgets the properties of $this it writes, and a by-reference capture gets what the closure leaves in it. (#15048, #15052, @danog)
  • No false ReferenceConstraintViolation for a reference rebound before a break or in a switch case. (#13710, @jirkace)
  • The body is still analysed after MismatchingDocblockReturnType. (#13213, @jirkace)
  • An always-exiting function with a native return type is InvalidReturnType on PHP 8.1+. (#15081, @orklah)
  • global $x in a function has the type declared in <globals>. (#15116, @orklah)

Scanning and includes

  • require of a ternary between two paths is resolved, and the files a dependency requires through one are scanned (Carbon 3's CarbonPeriod). (#15094, @bardrop; #15080, @orklah)
  • With several scan threads, an autoloaded class is no longer replaced by a project file that redeclares it (IDE helpers). (#15077, @orklah)
  • ssh2_* functions belong to ext-ssh2, not ext-swoole. (#15106, @orklah)

CLI and config

  • A file that changes during a run stops it with src/C3.php changed while Psalm was running. Run Psalm again. instead of a crash, and a worker crash is printed once. (#15061, @alies-dev)
  • -c with a directory or an empty path reports Cannot read the config file instead of crashing. (#14947, @tlq5l)
  • config.xsd accepts allowMissingFiles on <projectFiles>, <extraFiles> and <errorLevel>. (#15037, @orklah)

Docs

  • allowMissingFiles and the */** path patterns. (#15037, @orklah)
  • SecurityIssue in <unsuppressibleIssues> keeps every security issue reported. (#15074, @danog)

Internal changes

  • CI: cancel superseded branch runs, start unit tests without the chunk-matrix job, refresh test times weekly, a 6× faster slowest test, and Windows-safe collector timing tests. (#15024, #15029, #15030, #15032, #15031, @alies-dev)

New Contributors

Full Changelog: 7.21.1...7.22.0

Don't miss a new psalm release

NewReleases is sending notifications on new releases.