github phpstan/phpstan 2.4.0

53 minutes ago

Celebrating 10 years of PHPStan with explosion of features! 💥

On December 4th 2016 I published the first article about the public release of PHPStan 🎉

What a ride it has been! From 128 downloads a day to 1.3 million. I'm so grateful for all of this and for the community around PHPStan. Here's to 10 more years! 🍻

PHPStan 2.4.0 addresses a lot of popular feature requests users had over the years. And it closes 131 issues on top of that. Enjoy!

Major new features 🚀

Bleeding edge 🔪

  • Check that array values passed to strtr(), substr_replace(), preg_grep() and similar functions are castable to string (#7150), #15800, thanks @VincentLanglet and @staabm!
  • Keep unions of general arrays told apart by their kinds separate (#7211), #7759, #8963, #11176, #12195, #12434, #12533, #13394, #15431, #14797
  • Report a wrong type that a path produces next to mixed (witnessed mixed) (#7278, #7293), #11281
    • For $a = rand(0, 1) ? $mixed : null; requireInt($a), this will report an error even on level 5. Previously the type was normalized to mixed and was reported only on level 9 or 10.
  • Fix missing and too-wide return types with --fix, add projectType (open/closed) config parameter (#7283)
  • Name the keys of array shapes that might not be lists (#7309)
  • Check assert tags of overrides (#7291)

If you want to see the shape of things to come and adopt bleeding edge features early, you can include this config file in your project's phpstan.neon:

includes:
	- vendor/phpstan/phpstan/conf/bleedingEdge.neon

Of course, there are no backwards compatibility guarantees when you include this file. The behaviour and reported errors can change in minor versions with this file included. Learn more

Improvements 🔧

  • GithubErrorFormatter supports identifiers, tips and --fix (#7292), thanks @staabm!
  • Infer the return type of a closure calling itself through a by-ref use (#7216)
  • Track object aliases on the scope so property writes and calls reach every alias (#7241)
  • Update nikic/php-parser to 5.10.0 (#7250)
  • Add TemplateIntegerRangeType, TemplateFloatRangeType and TemplateConstantFloatType (#7290), #10660
  • Report the sampled peak memory of the whole process tree (#6348), thanks @janedbal!

Bugfixes 🐛


  • Remove a sealed type from itself in a union removal (#6718), #15392, thanks @janedbal!
  • Require the union with the other branch's type to equal the consequent before rescuing a one-sided conditional holder (#6835), #15534, thanks @VincentLanglet and @staabm!
  • Narrow getName() and $name instead of invariant self<T&BackedEnum> in the PHP 8.4+ ReflectionEnum::isBacked() stub (#7109), #15165, thanks @VincentLanglet and @staabm!
  • Cast SymfonyOutput::isDecorated() to bool (#7048), #15696, thanks @Gabrieljr42!
  • Match parent's conditional return type parameters to the child's names by position in MethodSignatureRule (#7127), #15801
  • Do not let anonymous class names depend on the working directory (#7128)
  • Report renamed named argument parameters for calls on ancestors of the prototype (#7130), #15803
  • Check that the class exists in ReflectionMethodConstructorThrowTypeExtension (#7132), thanks @localheinz!
  • Widen a float range that grows in a loop like an integer range (#7084), thanks @zonuexe!
  • Widen constant floats that change in a loop to a float range like constant integers (#7085), thanks @zonuexe!
  • Fold array shapes sharing a required key into one shape before degrading them to an oversized array (#7138), #15780
  • Find the phar's fds through /proc/self/fd when /dev/fd is missing (#7144), #15815
  • Do not resolve PHPDocs when hashing class value dependencies (#7147), #15816
  • Intersect an array shape with an array that cannot have its required key to never (#7158)
  • Respect exclusive bounds in composer.json PHP constraints (#7115), #15777, thanks @MohannadElemary!
  • Keep what other entries held when writing a shape into a general array's item shape (#7163), #15825
  • Keep the variable flow of the declare body (#7167), #15394
  • Allow initializing readonly properties in methods only the constructor calls (#7168), #9326
  • Answer maybe in ObjectType::isInstanceOf() where a subclass may be an instance (#7173)
  • Treat invariant template arguments that are subtypes of each other as the same type (#7174)
  • Read the current second for stat signatures off the filesystem clock (#7189)
  • Keep a native union member that the PHPDoc type does not describe (#7192), #15842
  • Keep conditional expressions a loop join can prove at loop heads, breaks and exits (#7182)
  • Exclude the files under a directory that an exclude pattern matches (#7175), thanks @SanderMuller!
  • Widen loop scopes against the loop entry and give up soundly at the iteration limit (#7183)
  • Give up soundly at the iteration limit of by-ref closures, backward goto and static variables (#7197)
  • Continue after try-finally from the finally block walked from the normal completion (#7198)
  • Price closure returns on the scope after a capped nested walk (#6772), #15406, thanks @SanderMuller!
  • Keep the callee's templates of a walked closure apart from generic calls in its body (#7200)
  • Type closures in ternary, match and coalesce arguments from the parameter they are passed to (#7203)
  • Process array arguments holding only closures after their siblings (#7204)
  • Fix array unions, offset types and array_is_list() narrowing found while separating array unions (#7206)
  • Narrow the native type of an array that array_is_list() rejects from the native type (#7235), #15875
  • Swap template bounds in contravariant positions (#7208), #13875, #12444
  • Keep single-key shapes with disjoint values apart in a union (#7207), #15864
  • Resolve a template contravariant in the return type to its upper bound (#7209)
  • Accept merged shapes in unions and bound accepts reasons (#7210)
  • Resolve accumulator templates of a closure to a fixed point (#7212), #15654, #7280
  • Solve accumulator templates that cycle through several closures together (#7213)
  • Resolve an unconstrained template argument from all its bounds (#7227)
  • Keep the keys of both shapes when intersecting shapes that may have unlisted keys (#7223), #15855
  • Do not record or keep conditional expressions that stop loops from converging (#7226), #15850
  • Keep the list in an intersection of a list shape with another shape (#7221), #15857
  • Merge degraded members into one oversized array only when each is an array (#7217), #15861
  • Keep the native values the PHPDoc type rules out in disjunction branch unions (#7220), #15858
  • Write a shape's optional keys as optional into each item shape of a general array (#7219), #15863
  • Keep the empty array apart when generalizing shapes at a loop head (#7224), #15854
  • Strip x-mode regex comments after removing delimiters (#7229), #15853
  • Keep a for loop's initial values on the path that never enters it (#7230), #15851
  • Revisit only newly guarded expressions in conditional-expression matching rounds (#7232)
  • Widen an unresolvable closure accumulator to mixed and drop array_reduce()'s initial value for non-empty arrays (#7236), #15874
  • Keep narrowed list keys in cache descriptions and drop array_reduce()'s null for a non-empty array part (#7254), #15882
  • Widen the values of a lone shape instead of degrading it to an oversized array (#7239), #15869
  • Re-specify tracked comparison verdicts when an operand narrows (#7240)
  • Keep the members of unions of general arrays apart through joins, writes and foreach (#7237), #15873
  • Keep only the accessories both passes satisfy when widening a type (#7252)
  • Widen appended keys past integer intervals and read integer bounds through NumberSet (#7244), #15878
  • Count a tracked chain link as evidence of an existing offset only for an unmodified foreach key (#7256)
  • Tie variables destructured from a union of array shapes to each other (#7260), #15893, #12487
  • Keep template-dependent key types such as key-of<T> as array keys in PHPDoc types (#7261), #14571, #15890
  • Count a property bound by reference as read when the other side can read it (#7266), #15886, #10068
  • Support description after deprection version in PHPStorm stubs (#7276), #15911, thanks @staabm!
  • Infer a naked template of a union from what its other members infer nothing from (#7277), #11130, #8707
  • Keep shapes precise when generalizing and writing nested offsets in loops (#7273), #15907
  • Do not follow one arm of contradictory conditional holders (#7272), #15909
  • Keep the templates of a generic callable argument its own (#7262), #12038, #15889
  • Keep the keys of a constant array when writing to an existing non-constant offset (#7280), #15917
  • Treat a switch whose cases cover every value as exhaustive (#7264), #15892
  • Report errors on non-variable foreach targets only from the final walk (#7286), #15924
  • Format PHP version without PhpVersion in RestartExtensionRegistry (phpstan/phpstan-src@6a460c5), #15913
  • Keep narrowed types after an unrolled foreach whose iterations all terminate (#6716), #15390, thanks @janedbal!
  • Renumber string keys of unpacked shapes without a key order below PHP 8.1 (phpstan/phpstan-src@678cfac)

Performance 🏎️

  • Turbo: native Php7 parser; skip type building in FloatClassFunctionTypeSpecifyingExtension (#7129)
  • Parse each PHPDoc once, remember function lookups, native RuleLevelHelper (#7131)
  • Write the collected data from the workers to a spool file (#6976), thanks @janedbal!
  • Build the fix diffs of rules on CollectedDataNode only for --fix (#7310), thanks @janedbal!

Function signature fixes 🤖

  • Treat clamp() as a function without side effects (#7116), thanks @sebastianbergmann!
  • Treat 352 built-in functions and methods without side effects as pure (#7118)
  • Describe Ds\Vector::toArray() as a list (#7123), thanks @simPod!
  • Describe the errors Uri\WhatWg\Url writes by reference (#7134), thanks @spaze!
  • Add a dynamic return type extension for getenv() (#7139), #15789, thanks @Amoifr!
  • Allow int return from CURLOPT_READFUNCTION callback (#7202), thanks @stobrien89!
  • Infer non-empty and lowercase strings from sprintf() with unpacked arguments (#7308), thanks @zonuexe!

Internals 🔍

Don't miss a new phpstan release

NewReleases is sending notifications on new releases.