Features
@template-contravariant(also@psalm-and@phpstan-template-contravariant): a template for a class that only takes values, such as a handler, writer or mapper. ASink<Animal>fits where aSink<Dog>is expected, not the other way round, and Psalm reports the template where the class gives a value out.Plugins read the variance with/** @template-contravariant T */ interface Sink { /** @param T $value */ public function put(mixed $value): void; /** @return T */ public function last(): mixed; // InvalidTemplateParam } /** @param Sink<Dog> $sink */ function takesDogSink(Sink $sink): void {} /** @param Sink<Animal> $animals */ function f(Sink $animals): void { takesDogSink($animals); // no issue }
ClassLikeStorage::getTemplateVariance(). See Template contravariance; the templated annotations page was rewritten with checked examples. (#14669, @alies-dev)- Performance profile:
--debug-performance(short name--profile) prints where a run spent its time and memory: stages, workers, the slowest files, statements and functions, and plugin handlers. With a path it also writes a JSON profile that you can drop on https://psalm.dev/profile/ to see it as charts; the page reads it in your browser and sends it nowhere.See Finding out why Psalm is slow. (#14737, #14769, #14807, @alies-dev)vendor/bin/psalm --no-cache --profile=psalm-perf.json
- Agent skill for Psalm users:
skills/psalmteaches AI coding agents to run Psalm, fix its issues, write annotations and editpsalm.xmlthe way the docs recommend. Install it withnpx skills add vimeo/psalm --skill psalm, with thellm/skillsComposer plugin, or by copyingvendor/vimeo/psalm/skills/psalminto your agent's skills directory. (#13152, @roxblnfk; #14751, @danog; #14795, @roxblnfk)
Performance
Fixes
Templates and type variables
new Pipe(fn (int $x) => ...)with aClosure(T): voidconstructor param capsTatint, so a later$pipe->push('s')isIncompatibleTypeParametersinstead of reaching theintcallback. (#14669, @alies-dev)- A covariant template in the bound of a method template (
@template U of T) is reported in a param even after an earlier param usedUlegally. (#14669, @alies-dev) - Callbacks over a widened type variable (
new Coll([1]), then$c->add('x')) see every value it holds, not only the construction-site ones. (#14551, @shu-chen-dot-ca) nullno longer fits a template without a bound:fn() => nullforcallable(): T,[null]forlist<T>. (#14550, @shu-chen-dot-ca)- Trait method params use the types given to
@use. (#13429, @jirkace) @implements Wrapper<static>resolvesstaticto the object it is read from, and chained calls on such a value no longer hang. (#14572, @jirkace)@return static<NewK, NewV>on a template receiver keeps its new type arguments. (#14579, @jirkace)- An implementation with the same conditional return type as its interface method is not an
ImplementedReturnTypeMismatch. (#14502, @jirkace) - A conditional return type reads an argument that does not fit its param as it is (
preg_replace()on an array ofStringablereturns an array). (#14421, @jirkace) - A template array
Tread at akey-of<T>offset isvalue-of<T>. (#14586, @jirkace) is_int(),is_scalar()and similar checks narrow a template bounded byscalar,numeric,array-keyor an int range instead of reportingDocblockTypeContradiction. (#14545, @jirkace)- A literal class name can be identical to a template
class-string<T>. (#14365, @jirkace) - Iterating a star-projected collection (
Coll<*>) gives the template's bound instead ofmixed. (#14666, @alies-dev) - Generated stubs keep
@template, the variance and the declared order of templates instead of marking every template@template-covariant. (#14669, @alies-dev)
Narrowing and arrays
$a !== $bwith several possible values of$bdoesn't exclude them all from$a. (#14525, @jirkace)- After
isset($array[$k])with$kone of several literal keys,$array[$k]is the value of those keys. (#14646, @jirkace) $arr[$key]stays known to be set when$keyis narrowed insideif (isset($arr[$key])). (#13344, @jirkace)InvalidArrayOffsetis reported when an item is read again under an index narrowed to a key the array can't have. (#14767, @orklah)($a ?? null) !== nullassertsisset($a). (#14517, @jirkace)instanceofa parent class narrows to the parent class. (#14318, @jirkace)get_class()of a union of classes is the name of any of them. (#14366, @jirkace)- A property of an object intersected with an object shape (
V&object{x: string}) has the type of both. (#14546, @jirkace) - With
ensureArray*OffsetsExist, an offset that may be one of several literals is set only when each of them is. (#14606, @jirkace) - Appends stop growing a list shape at
maxShapedArraySize. (#14641, @jirkace) - Quoted keys of an array shape are read as PHP string literals (
"a\r\nb"). (#14600, @jirkace) - No crash on PHP 8.5 for an array key that reads as a huge number, such as the UUID
'6e5212ed-…'in Symfony'sFormconstraint. (#14797, @leonardsimonse) pow()of two int literals is an int literal. (#14505, @MikeFTV)
Control flow
- A variable unset before a
continueorbreakis possibly undefined after the loop. (#14509, @jirkace) - A property or offset narrowed before a loop forgets the narrowing after a
breakthat reassigned it. (#14456, @jirkace) - Variables assigned in the left operand of a negated
&&are defined in the next operands of the enclosing&&. (#14409, @jirkace) - A superglobal assigned on some paths only keeps its default type on the others. (#14406, @jirkace)
- A closure that falls through after a
throworexitin a branch returnsvoid, notnever. (#14793, @MikeFTV) - The return of a required file keeps the use of the outer
return, so it is noUnusedFunctionCall. (#14562, @jirkace)
Callables and nullsafe calls
- Arguments of a call after a
?->in a chain are analysed with a non-null receiver, as PHP skips the whole chain when it is null. (#14608, @jirkace) - The if-true assertions of a method called with
?->apply where the call is truthy. (#14531, @jirkace) - An enum case with
__invoke()is callable. (#14590, @jirkace) - A first-class callable of a method that an unsealed class forwards to
__call()is not anUndefinedMagicMethod. (#14411, @jirkace) staticin closure hints of calls in a trait on other objects resolves to that object. (#14446, @jirkace)- A
Closureobject of unknown signature is aClosure. (#14599, @jirkace) - No crash on a callable array whose object intersects a template. (#14594, @jirkace)
yield fromforwards the send type of the inner generator. (#14340, @jirkace)
Reported issues
- A value that shares only
nullorfalsewith the expected type isInvalidArgument, notPossiblyInvalidArgument, in closure params ofarray_map()and similar functions and in__set()calls on a@property. (#14742, @orklah) ParadoxicalConditionon a dead||operand next to other values of the same variable. (#14821, @orklah)- No
ParamNameMismatchfor a name another overridden method uses. (#14536, @jirkace) - No
MethodSignatureMismatchfor@no-named-argumentson parameterless methods. (#14799, @orklah) - A write through a
sethook is checked against the hook's param type. (#14538, @jirkace) - Properties that
array_column()reads are used. (#14560, @jirkace) - Before PHP 8.6, unsetting, passing by reference or referencing a property of a constant object is reported. (#14519, @jirkace)
@implementsnaming an interface the class doesn't implement gets the same message with or without type arguments. (#14650, @jirkace)@psalm-suppressbefore the expression ofechoor<?=is read. (#14527, @jirkace)
Stubs
ini_get()of a core setting no longer includesfalse. (#14809, @alies-dev)intval(),floatval()anddoubleval()report what the casts report. (#14311, #14805, @jirkace)ArrayObject::offsetSet()andArrayIterator::offsetSet()accept anulloffset. (#14516, @jirkace)pgsqlextension stub with thePGSQL_*constants, and the classes of thegeosextension. (#14637, #14631, @jirkace)FILTER_THROW_ON_FAILUREis defined and has no failure value. (#14359, @jirkace)Redis::auth()andRedisCluster::__construct()accept phpredis ACL credentials. (#14800, @orklah)- SPL classes,
WeakMap,SoapClient,MemcachedandPDOStatementuse the PHP 8 param names. (#14353, #14367, @jirkace) - The property map drops php-parser v4's
Name::$partsand uses v5'sArrayItemfor array and list items. (#14514, @jirkace)
CLI, config and tools
reportInfo="false"also dropsPossibly*issues that takeinfofrom their parent issue. (#14785, @alies-dev)- With an output format other than
console,phpstormandgithub, run statistics (--stats, the timing line, the--profilesummary) go to stderr. (#14745, @alies-dev) --update-baselinekeeps multi-line samples of CRLF files. (#14794, @ARAYN7011)- The config schema allows naming the class or property in the handlers of
MissingDependencyand four other issues. (#14649, @jirkace) --generate-stubskeeps the trait uses and the interfaces of a class with a parent. (#14564, @jirkace)- The class of
@psalm-scope-thisis scanned. (#14565, @jirkace) psalm-refactorrejects reserved words as destinations. (#14552, @MohannadElemary)- The language server ignores requests for documents that aren't files. (#14563, @jirkace)
Internal changes
- Regression tests for fixed issues. (#14481, #14474, #14482, #14436, #14503, #14520, #14522, #14524, #14539, @jirkace; #14691, @danog)
- CI perf harness shows the files whose analysis got slower. (#14787, @alies-dev)
PluginTestworks with psalm/plugin-phpunit 1.3.3. (#14810, @orklah)- Plugin API:
FunctionLikeAnalyzer::getFunctionLikeNode()(#14635) andConfig::getPreloadedStubFiles()(#14633), @jirkace. Psalm now requires psalm/psalm-plugin-api 0.5.2. (#14846, @alies-dev)
New Contributors
- @MikeFTV made their first contribution in #14505
- @ARAYN7011 made their first contribution in #14794
- @leonardsimonse made their first contribution in #14797
Full Changelog: 7.17.0...7.18.0