packagist vimeo/psalm 7.18.0
7.18.0 Contravariant templates

one hour ago

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. A Sink<Animal> fits where a Sink<Dog> is expected, not the other way round, and Psalm reports the template where the class gives a value out.
    /** @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
    }
    Plugins read the variance with 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.
    vendor/bin/psalm --no-cache --profile=psalm-perf.json
    See Finding out why Psalm is slow. (#14737, #14769, #14807, @alies-dev)
  • Agent skill for Psalm users: skills/psalm teaches AI coding agents to run Psalm, fix its issues, write annotations and edit psalm.xml the way the docs recommend. Install it with npx skills add vimeo/psalm --skill psalm, with the llm/skills Composer plugin, or by copying vendor/vimeo/psalm/skills/psalm into your agent's skills directory. (#13152, @roxblnfk; #14751, @danog; #14795, @roxblnfk)

Performance

Fixes

Templates and type variables

  • new Pipe(fn (int $x) => ...) with a Closure(T): void constructor param caps T at int, so a later $pipe->push('s') is IncompatibleTypeParameters instead of reaching the int callback. (#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 used U legally. (#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)
  • null no longer fits a template without a bound: fn() => null for callable(): T, [null] for list<T>. (#14550, @shu-chen-dot-ca)
  • Trait method params use the types given to @use. (#13429, @jirkace)
  • @implements Wrapper<static> resolves static to 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 of Stringable returns an array). (#14421, @jirkace)
  • A template array T read at a key-of<T> offset is value-of<T>. (#14586, @jirkace)
  • is_int(), is_scalar() and similar checks narrow a template bounded by scalar, numeric, array-key or an int range instead of reporting DocblockTypeContradiction. (#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 of mixed. (#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 !== $b with several possible values of $b doesn't exclude them all from $a. (#14525, @jirkace)
  • After isset($array[$k]) with $k one of several literal keys, $array[$k] is the value of those keys. (#14646, @jirkace)
  • $arr[$key] stays known to be set when $key is narrowed inside if (isset($arr[$key])). (#13344, @jirkace)
  • InvalidArrayOffset is reported when an item is read again under an index narrowed to a key the array can't have. (#14767, @orklah)
  • ($a ?? null) !== null asserts isset($a). (#14517, @jirkace)
  • instanceof a 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's Form constraint. (#14797, @leonardsimonse)
  • pow() of two int literals is an int literal. (#14505, @MikeFTV)

Control flow

  • A variable unset before a continue or break is possibly undefined after the loop. (#14509, @jirkace)
  • A property or offset narrowed before a loop forgets the narrowing after a break that 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 throw or exit in a branch returns void, not never. (#14793, @MikeFTV)
  • The return of a required file keeps the use of the outer return, so it is no UnusedFunctionCall. (#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 an UndefinedMagicMethod. (#14411, @jirkace)
  • static in closure hints of calls in a trait on other objects resolves to that object. (#14446, @jirkace)
  • A Closure object of unknown signature is a Closure. (#14599, @jirkace)
  • No crash on a callable array whose object intersects a template. (#14594, @jirkace)
  • yield from forwards the send type of the inner generator. (#14340, @jirkace)

Reported issues

  • A value that shares only null or false with the expected type is InvalidArgument, not PossiblyInvalidArgument, in closure params of array_map() and similar functions and in __set() calls on a @property. (#14742, @orklah)
  • ParadoxicalCondition on a dead || operand next to other values of the same variable. (#14821, @orklah)
  • No ParamNameMismatch for a name another overridden method uses. (#14536, @jirkace)
  • No MethodSignatureMismatch for @no-named-arguments on parameterless methods. (#14799, @orklah)
  • A write through a set hook 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)
  • @implements naming an interface the class doesn't implement gets the same message with or without type arguments. (#14650, @jirkace)
  • @psalm-suppress before the expression of echo or <?= is read. (#14527, @jirkace)

Stubs

  • ini_get() of a core setting no longer includes false. (#14809, @alies-dev)
  • intval(), floatval() and doubleval() report what the casts report. (#14311, #14805, @jirkace)
  • ArrayObject::offsetSet() and ArrayIterator::offsetSet() accept a null offset. (#14516, @jirkace)
  • pgsql extension stub with the PGSQL_* constants, and the classes of the geos extension. (#14637, #14631, @jirkace)
  • FILTER_THROW_ON_FAILURE is defined and has no failure value. (#14359, @jirkace)
  • Redis::auth() and RedisCluster::__construct() accept phpredis ACL credentials. (#14800, @orklah)
  • SPL classes, WeakMap, SoapClient, Memcached and PDOStatement use the PHP 8 param names. (#14353, #14367, @jirkace)
  • The property map drops php-parser v4's Name::$parts and uses v5's ArrayItem for array and list items. (#14514, @jirkace)

CLI, config and tools

  • reportInfo="false" also drops Possibly* issues that take info from their parent issue. (#14785, @alies-dev)
  • With an output format other than console, phpstorm and github, run statistics (--stats, the timing line, the --profile summary) go to stderr. (#14745, @alies-dev)
  • --update-baseline keeps multi-line samples of CRLF files. (#14794, @ARAYN7011)
  • The config schema allows naming the class or property in the handlers of MissingDependency and four other issues. (#14649, @jirkace)
  • --generate-stubs keeps the trait uses and the interfaces of a class with a parent. (#14564, @jirkace)
  • The class of @psalm-scope-this is scanned. (#14565, @jirkace)
  • psalm-refactor rejects reserved words as destinations. (#14552, @MohannadElemary)
  • The language server ignores requests for documents that aren't files. (#14563, @jirkace)

Internal changes

New Contributors

Full Changelog: 7.17.0...7.18.0

Don't miss a new psalm release

NewReleases is sending notifications on new releases.