Skip to content

0.5

Compare
Choose a tag to compare
@ondrejmirtes ondrejmirtes released this 01 Jan 23:06

This is the biggest release of PHPStan yet! I and other contributors have worked hard over the holiday season to bring you awesome new features and other enhancements.

PHPStan is now on Patreon! You can show your support by backing PHPStan on Patreon. Choose your reward from different tiers and be a part of PHPStan's sustainable future development. I will deeply appreciate it. Thanks!

Major new features in 0.5

  • Completely reworked traits support. Previously, each trait was analysed separately and users complained about a lot of "method undefined" errors when calling $this->x(). In 0.5, each trait is analysed in context of class that uses it, so it's analysed multiple times for each using class. (#19)
  • Check for unused constructor parameters (level 0). If it finds one, it can either mean that it's supposed to be unused and you can safely delete it, or that you forgot to use it, e. g. assign it to a property.
  • PHPStan now reads phpDocs from functions, enriching data about parameter types and return types.
  • Check return types of functions (level 3), in a similar fashion to already checked return types from methods and closures.
  • Support for @property and @property-read annotations on classes.
  • Added experimental level 5 with two new features:
    • Union types support. On level 0-4, any type connected with a | (like Foo|Bar with the exception of null) is mixed and no checks are performed on them. On level 5, Foo|Bar is a full-fledged type checked when returning from a function or assigned to a property or an array. Additionally, if one of the types is an iterable (Foo[]|Bar), it's specifying the type of the value when entering foreach.
    • Checking arguments passed to functions and methods. This was the missing piece of a puzzle from previous versions and it brings PHPStan to a whole new level. You should have much less TypeErrors in your application thanks to it. It also works with union types mentioned above. This check even found some errors in PHPStan itself!
    • Level 5 is marked as experimental because it's a complex area and there can be some rough edges. I'd appreciate if you tried it out in your app and reported issues it finds you don't consider errors in your code.
  • true or false (as opposed to bool) are also valid types that can be documented in phpDoc
  • Check that function or method return result of type void is not further used (for methods called on $this on level 0 and for others on level 2)
  • Check whether clone is called on an object (level 3)
  • Support for empty() - it's like !isset() - variables in if (!empty(...)) are not reported as undefined, similarly with properties
  • Support for @return $this - similar resolving type logic to @return static.
  • Added --no-progress option to hide progress bar during analysis (#45)

Enhancements

  • PHPStan can be installed alongside PHP-Parser 2.x so that folks using Laravel can also analyse their code :) However, using PHP-Parser 3.x if you're on PHP 7.1 is strongly recommended and PHPStan will output a warning if that's not the case.
  • Removed tracy/tracy as a dependency. PHPStan is now more lighweight.
  • Callables accept strings (references to functions) and arrays with two strings (static methods).
  • Reporting accessing a property on an unknown class
  • Reporting calling a method on an unknown class
  • Better support for aliased classes
  • Helpful message if PHPStan crashes on low memory limit
  • Methods with func_num_args() and/or func_get_arg() are also considered variadic
  • Added %currentWorkingDirectory% config parameter
  • Variables in unset() are not reported as undefined
  • Getting type from the ternary operator, including the Elvis ?: variant
  • Getting type from coalesce ?? operator
  • Correctly resolving static type logic in more complex types like static[]
  • Getting type from clone keyword
  • Specifying expression type with is_string
  • Improved isset() support for specifying properties existence
  • Specifying expression type in switch using switch (get_class($foo)) case Foo::class:

BC breaks for extension developers

  • Added ParameterReflection::isVariadic()

Bugfixes

  • Arguments passed by reference can also define a new variable in a static method call
  • Closures recognize variadic arguments
  • Fixed "undefined variable" errors in a lot of weird code situations
  • Fixed assigning $$variable (PHPStan used to crash on internal error here)
  • Fixed error with variadic function detection
  • Protected methods from a child class can be called from the parent class.
  • Some types' description were missing |null in case of nullables.
  • Fixed strtok() call check
  • Fixes around defined variables and finally block
  • Fixed mixing variable types in some switch cases
  • float can be combined with integer resulting in float instead of mixed
  • Class reflection extension for deficiencies reported in native PHP reflection - currently supports ZipArchive properties that are missing in reflection (PHP bug #73803)