packagist vimeo/psalm 3.5.2
Allow config for checking that array offsets exist

latest releases: 5.x-dev, dev-master, dev-isTypeContainedByType_flags...
4 years ago

Features

Allow errors when string offsets may not exist

Given the code

/**
 * @param array<string, Exception> $arr
 */
function shouldTakeArrayOfExceptions(array $arr) : void {
  echo $arr['foo']->getMessage();
}

shouldTakeArrayOfExceptions(
  ["bar" => new Exception('hello')]
);

Psalm by default doesn't see the error, but now with the config flag ensureArrayStringOffsetsExist="true" Psalm will complain when calling $arr['foo']->getMessage();.

It's not on by default because too much existing code would be broken.

Detecting unnecessary @var annotations

Thanks to an idea from @iluuu1994, Psalm now catches unnecessary @var annotations when unused code detection is turned on (with --find-unused-variables or --find-unused-code).

function getString() : string {
  return "hello";
}

/** @var string */
$a = getString(); // now UnnecessaryVarAnnotation is emitted

echo $a;

Bugfixes

  • Allow immutable manipulation inside unserialize methods (#2116)
  • Fix some signatures - thanks @villfa and @LeSuisse (#2119, #2122)
  • Don’t coerce callmap args to types unless declare(strict_types=1); is used (#2125)
  • Allow Xdebug stubs to be used - thanks @SignpostMarv (#2118)
  • Do better at inferring class const types (#1551, #2139)
  • Fix unused @psalm-suppress discovery in threaded mode (#2127)
  • Make byref params passed to closures mixed after the fact (#2145)
  • Prevent crash when analysing count call with no args (#2146)
  • Coerce false/null to 0 when used as array offset (#2155, #2165)
  • Improve handling of elseif reconciliation on object properties (#2159)
  • Make builtin constructers mutation-free (#2114)
  • Allow @psalm-assert string[] $foo and @psalm-assert array<int, string> $foo syntaxes (#2121, #2148)
  • Complain when inheriting from immutable classes/interfaces without also the same annotations (#2138)
  • Don’t allow union closure parameter types when combining (#2157)
  • Loose equality should not imply type equivalence (#2158)
  • Allow docblock inheritance from grandparents (#2166)

Don't miss a new psalm release

NewReleases is sending notifications on new releases.