New AI, UX and @template syntax features.
Features
- Call-site variance:
covariantorcontravariantbefore a type argument makes one param accept a generic of any subtype (or supertype) of it, without changing the class's template. As withBox<*>, values then go one way only: from aBox<covariant Node>you readNode, 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 thecompactformat (one line per issue), no progress output even ifCIis set, a one-line result (also0 issues), and[*]after each issue--altercan fix. An explicit--output-formatwins, 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-analysisskips taint analysis for one run (runTaintAnalysisis on by default), e.g. for a quick local check (#15078, @orklah).- New issues:
--alteron PHP 8.2 writestrue,falseandnullas native types (: true|nullinstead of: bool|nullplus 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
TaintedCallablefor 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>")isTaintedHtml. (#14753, @arpitjain099)
Stubs and plugins
- A stub's
@return(or its own@psalm-overload) replaces the original's@return $thisand overloads, so a stubbed call returns the stub's type again (a 7.19.0 regression). (#15090, @alies-dev) - No false
MethodSignatureMismatchfor an override that widens a param of a stubbed userland class, andfloat/intswaps PHP rejects are reported. (#15114, @orklah) []onDs\Map,Ds\Vectorand 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
nullkeeps the function's params checked. (#15040, @orklah) - Params providers can resolve
[$this, 'method']:CallAnalyzer::getFunctionIdsFromCallableArg()takes the call'sContext. (#15082, @orklah) - A return type provider of a class wins over its
@mixinfor 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 nestedisset($a["x"][3])narrows$a["x"]. (#15103, #15104, #15105, #15076, @orklah)- A
switchcase 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
mixedfallback keeps that key's type. (#14815, #14611, @jirkace) is_int(),is_bool()andis_string()keep the emptiness ofempty-scalarandnon-empty-scalar. (#14431, @jirkace)is_callable()on a list can be true. (#14319, @jirkace)- No
MixedAssignment/MixedArgumentin aforeachover an always-empty list, such aslist<T>of anewcovariant 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->valueof aTis avalue-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)
staticin the params of a@methodtag resolves to the called class. (#14480, @jirkace)void|neverisvoid, notnull:AssignmentToVoidis reported, and an always-exiting function is noInvalidReturnType. (#15087, @orklah)pow()with a zero and a float operand is a float. (#14845, @ARAYN7011)PHP_INT_MINis negative,PHP_DEBUG/PHP_ZTSare0|1(boolsince 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
PropertyNotSetInConstructorfor 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
$thisit writes, and a by-reference capture gets what the closure leaves in it. (#15048, #15052, @danog) - No false
ReferenceConstraintViolationfor a reference rebound before abreakor in aswitchcase. (#13710, @jirkace) - The body is still analysed after
MismatchingDocblockReturnType. (#13213, @jirkace) - An always-exiting function with a native return type is
InvalidReturnTypeon PHP 8.1+. (#15081, @orklah) global $xin a function has the type declared in<globals>. (#15116, @orklah)
Scanning and includes
requireof a ternary between two paths is resolved, and the files a dependency requires through one are scanned (Carbon 3'sCarbonPeriod). (#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) -cwith a directory or an empty path reportsCannot read the config fileinstead of crashing. (#14947, @tlq5l)config.xsdacceptsallowMissingFileson<projectFiles>,<extraFiles>and<errorLevel>. (#15037, @orklah)
Docs
allowMissingFilesand the*/**path patterns. (#15037, @orklah)SecurityIssuein<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
- @arpitjain099 made their first contribution in #14753
- @tlq5l made their first contribution in #14947
- @bardrop made their first contribution in #15094
Full Changelog: 7.21.1...7.22.0