github carthage-software/mago 1.65.0
Mago 1.65.0

one hour ago

Mago 1.65.0 reads a @var docblock on a parameter as its type, adds the PHP 8.6 stream errors, polling and URI APIs, and fixes 74 bugs, most of them in the analyzer. It also analyzes long elseif chains and match expressions faster, and speeds up mago guard.

✨ Features

Codex

  • @var on parameters: reads a @var docblock right before a parameter as the parameter's type. (#2779, #2963, e3c3cf3)
array_map(static fn (/** @var array{email: string} */ array $row): string => $row['email'], $rows);

A @param tag on the function still wins.

Prelude

  • PHP 8.6 stream errors: adds StreamErrorMode, StreamException, stream_last_errors() and more. (#2966, #2967, 1fd0ed3)
  • PHP 8.6 polling: adds the polling API, such as Io\Poll\Context and StreamPollHandle. (#2968, #2969, dc48b98)
  • PHP 8.6 URI additions: adds UriBuilder, UrlBuilder, getHostType(), url_percent_encode() and more. (#2970, #2971, b024f30)

🐛 Bug Fixes

Analyzer

Control Flow

  • switch fall-through: a case reached both directly and from the case above keeps what each path knows. (cf11e62)
  • Never-matching case labels: reports one at any place in a group of labels that share a body. (981520c)
  • Repeated match arms: reports an arm as unreachable when an earlier arm already matches its string. (a8e2a1a)
  • try and catch: after a catch that falls through, forgets what the try body proved before a throw. (425be40)
  • Long && and || chains: use less stack, so very long chains no longer overflow it. (78abdaa)
  • yield from: accepts a generator whose send type is void in any generator. (e6595d0)

Loops

  • Always-entered loops: variables that a while (true) or do-while body defines stay defined after an outer try. (b4eae4b)
  • ++ after an assignment: $i = 0; $i++; in a loop body makes $i exactly 1, not int. (bbf0c74)

Narrowing

  • === on typed values: narrows both sides, so after $a === $b with A $a and B $b, $b is an A too. (1103042)
  • Identical arrays: after $x === $y on arrays of unrelated types, both may still be empty. (54d7544)
  • Loose bool comparisons: is_null($s) == false, or a comparison with a false constant such as Flag::OFF, narrows. (a075c8a)
  • && chains: forget a property narrowed before a call in the chain that may change it. (d220ab1)
  • Calls on union types: $o->is($v) on A|B narrows $v only by what holds whichever method runs, not by A::is() alone. (5cd2217)
  • Named arguments: is_a(), is_subclass_of(), array_key_exists() and similar functions narrow with named arguments. (38f71fb)
  • != and numeric strings: after $a != '1', $a is no longer '01' or '1.0', which PHP treats as equal to '1'. (4714226)
  • is_scalar() on callables: narrows a callable to a callable-string. (ea3564b)
  • !is_scalar() on callables: removes the callable-string part, so a later is_string() is reported. (6cc2c47)
  • is_object() on callables: narrows a callable to a callable object that keeps its signature and __invoke(). (fc1bfc9)
  • is_resource() on templates: narrows a template with no bound to resource, not null. (76d94ae)
  • isset() with a computed key: isset($a[strtolower($s)]) proves that $a is set. (02a2730)
  • Generic static assertions: @assert-if-true static<never> $this narrows User<null> to User<never>. (#2960, 421a916)

Nullsafe

  • ?-> calls: $h?->foo($h = 1) leaves $h as 1 or null, not the original object. (966f816)
  • ?-> with ?? null: ($box?->n ?? null) !== null narrows both $box and $box->n. (ff1d9f3)

Variables and References

  • unset() on a reference: breaks the link, so later writes to the variable no longer reach the other one. (22c68d4)
  • By-reference captures: calling a closure that captures $x by reference no longer turns false into bool in $x. (15c87d6)
  • By-reference variadics: f($a, $b, $c) with int &...$items may change every argument. (8e6a2ae)
  • Untyped by-reference parameters: may take any value, such as $value in function (&$value) passed to array_walk(). (4dd50af)
  • isset() and ??: still report a possibly undefined variable used in them as a method receiver or a member name. (9b6df16)
  • @var before foreach: also types variables the loop does not bind, such as an outer $s. (ed981bc)
  • @var on destructuring: types the variables of ['a' => $a] = $row and [$x, $y] = $pair. (aeb5c05)

Arrays

  • Appends to lists with optional items: $a[] = 3 may land at any optional index, such as 0 when $a may be empty. (5c81d0b)
  • Offsets of iterable: $items[0] on an iterable<int, int> reads int from its array part. (f9e39be)
  • Template keys: $array[$key] = 3 with $key of T of 'a' sets key a and keeps the array's shape. (324865d)
  • array_map(null, ...): zips any mix of lists, shapes and unions of them into a list of tuples. (b5c2e89)
  • array_multisort(): treats every array argument as reordered, with its integer keys renumbered. (cfe3941)

Strings and Numbers

  • abs(): returns int|float when the argument may be PHP_INT_MIN. (dfe9693)

Generics

  • Inherited constructors: apply the @extends binding, so new IntBox('x') is reported when IntBox extends Box<int>. (5a4d8a8)
  • Templates in intersections: infers T from any member of an intersection, such as Box<T> in Marker&Box<T>. (a425785)
  • Unpacked lists: infers templates from a list unpacked into a call, such as identity(...[1]). (e14d687)
  • Unpacked arguments and closures: infer templates first, so array_map(fn($v) => $v, ...$lists) knows $v. (e408f96)
  • Static calls on templates: $obj::make() on a T returns T when make() returns static. (625f509)
  • @template T of static: checks @extends and @implements arguments against the extending class. (4e4cbc7)
  • Conditional returns on static: ($x is static ? A : B) gives A|B unless the receiver's class is final. (ded4de6)
  • instanceof a generic subclass: allows $item instanceof Kid on a Par<'b'> when Kid<U> extends Par<U>. (1d1213c)

Classes and Members

  • Untyped constants over typed ones: reports const D = 5 that overrides const int D, as PHP does. (aadd72a)
  • @return self over static: reports it on a final method of a class that is not final. (b12018b)
  • Deprecated hooked properties: stops reporting a @deprecated property that its own hooks use. (3420805)
  • (object) casts: a property of an object made by an (object) array cast takes any value. (d1aa550)
  • stdClass passed to calls: forgets its known properties after a call that may change them. (a091f4a)
  • Optional object shape properties: allows writing $o->q on object{q?: bool}, which may create it. (7398b16)

Callables

  • Callable arrays: [$obj, $name]() with $name of 'a'|'b' returns what either method returns. (441cf88)
  • Callable strings: reports 'f' passed as callable(int=) when f() requires its argument. (954f2ec)
  • array_map() callbacks: reports a callback that needs more arguments than there are arrays. (8e0267d)
  • Unpacked lists in calls: f(...['x', 1]) checks each item against the parameter at its position, variadics included. (ae08c45)
  • Return type providers: no longer read arguments at or after an unpacked one, so count(...[$list]) is not misread. (0aeaca0)

Diagnostics

  • Negative list indexes: undefined-int-array-index shows a negative index, such as -1, with its sign. (9d17b6d)

Codex

  • int<…> bounds: no longer wrap at or past the 64-bit limits; -$i may be a float when $i can be PHP_INT_MIN. (8cfcca6)
  • get_class() on enums: returns the enum's class-string, such as class-string<Suit>. (37d1ff6)
  • @param-out inheritance: a method without a docblock inherits @param-out from the method it overrides. (03fdb2c)
  • Shape rest types: reads ...<T> in an array shape as the type of the extra values, not of their keys. (7b895b3)
  • Inherited conditional returns: an override inherits a conditional @return when the parent has no native return type. (d426068)
  • void and null: no longer reports f() === null when f() returns void. (4f6d2d1)
  • Redeclared method templates: an override's own @template T applies to the types it inherits, such as class-string<T>. (a66ccf2)
  • Intersections with generics: reports Box<D>&Marker where Box<C>&Marker is expected. (718d885)
  • Projected templates: Checker<*> and Checker<contravariant LeafNode> read T as its bound, such as Node. (85d01f7)
  • static:: in docblocks: static::REF_* resolves against the class of the call, such as a child's constants. (969b146)
  • define(): defines the constant when called with named arguments in any order. (3524376)
  • use const in docblocks: resolves imported and aliased constants, such as int-mask-of<FLAG_A|OTHER>. (db17bbb)

Prelude

  • RecursiveRegexIterator::MATCH: accepts it as the mode of RecursiveRegexIterator, fixing a typo in the stub. (#2964, #2965, 4cc02a4)

Semantics

  • Hooked properties: reports a promoted hooked readonly property, and virtual property defaults as PHP does. (#2956, #2957, b6e9421)
  • Interface properties: checks private(set) and protected(set) against the property's hooks, as PHP does. (#2954, #2955, 57bef3f)
  • #[\Deprecated] on classes: accepts it next to #[\DelayedTargetValidation] on PHP 8.5 and later. (#2958, #2959, 154340c)

Syntax

  • yield in conditions: a yield in an if, elseif, loop header, switch case or echo makes the function a generator. (08eb9b4)
  • String interpolation offsets: reports offsets PHP rejects, such as "$user['name']", as parse errors. (#2972, #2973, 47816d7)

🏗️ Internal

Analyzer

  • Long elseif chains: indexes negated clauses, applies each negation once and shares clauses. (bb7e58b, 52181cc, 4779991, ab8c2d2)
  • Long match expressions: indexes their negated clauses; arms share clauses and skip ones already applied. (4ad28c4, 884e6ff)

Algebra

  • Clauses: caches and looks up negations by hash, and moves the left clause into its last disjunction. (61341db, 5c1d08f, fb90ebd)

Codex

  • Enum case unions: combines them by sorting the cases. (3a36074)

Guard

  • Builtin symbols: looks them up in tables built at compile time instead of decoding the prelude. (a301350)
  • One walk: runs the perimeter and structural guards in a single walk over each file. (98426f0)

CI

  • Pull request limit: enforces the open pull request limit on a schedule. (524b93e)

🧩 Extension Protocol

No protocol changes in this release.

🙏 Thank You

Contributors

Thank you to everyone who contributed to this release:

Issue Reporters

Thank you to everyone who reported issues that shaped this release:

Full Changelog: 1.64.0...1.65.0

Don't miss a new mago release

NewReleases is sending notifications on new releases.