2.0.0
Check out the UPGRADING guide!.
Buy the PHPStan elephpant and T-shirts!
Major new features 🚀
- Level 10 - level 9 on steroids, treats all
mixed
types strictly, not just explicitmixed
- Array
list
type (#1751), #3311, #8185, #6243, thanks @rvanvelzen!- Lists are arrays with sequential integer keys starting at 0
- Lower memory consumption thanks to breaking up of reference cycles
- Learn more »
- In testing the memory consumption was reduced by 50–70 %.
- Enhancements in handling parameters passed by reference
- Learn more on phpstan.org
- #2941, thanks @ljmaskey!
- New rules (level 0):
- MagicConstantContextRule (#2741), #10099, thanks @staabm!
- MissingMagicSerializationMethodsRule (#1711), #7482, thanks @staabm!
- Check vprintf/vsprintf arguments against placeholder count (#3126), thanks @staabm!
- Check if required file exists (#3294), #3397, thanks @Bellangelo!
- Add
@readonly
rule that disallows default values (#1391), thanks @herndlm! - Rule about
@phpstan-consistent-constructor
(#1296), thanks @canvural! - Check code in custom PHPStan extensions for runtime reflection concepts like
is_a()
orclass_parents()
(phpstan/phpstan-src@c4a662a) - Check code in custom PHPStan extensions for runtime reflection concepts like
new ReflectionMethod()
(phpstan/phpstan-src@5363066) - ApiInstanceofRule
- Report
instanceof
of classes not covered by backward compatibility promise (phpstan/phpstan-src@ff4d02d) - Report
instanceof
of classes covered by backward compatibility promise but where the assumption might change (phpstan/phpstan-src@996bc69)
- Report
- Check that PHPStan class in class constant fetch is covered by backward compatibility promise (phpstan/phpstan-src@9e00725)
- Previously absent type checks:
- Check existing classes in
@phpstan-self-out
(phpstan/phpstan-src@6838669) - Check nonexistent classes in local type aliases (phpstan/phpstan-src@2485b2e)
- Check unresolvable types in local type aliases (phpstan/phpstan-src@5f7d12b)
- Check generics in local type aliases (phpstan/phpstan-src@5a2d441)
- Check existing classes in
@param-out
(phpstan/phpstan-src@30c4b9e), #10260 - Check existing classes in
@param-closure-this
(phpstan/phpstan-src@2fa539a), #10933
- Check existing classes in
- New rules (level 2):
- Validate inline PHPDoc
@var
tag type against native type (phpstan/phpstan-src@a69e3bc)- Set
reportWrongPhpDocTypeInVarTag
totrue
to have all types validated, not just native ones - Use config option
reportAnyTypeWideningInVarTag: true
for stricter behaviour (#2840), thanks @janedbal! - IncompatibleDefaultParameterTypeRule for closures (phpstan/phpstan-src@0264f5b)
- Set
- Checking truthiness of
@phpstan-pure
above functions and methods - Check variance of template types in properties (#2314), thanks @jiripudil!
- Report narrowing
PHPStan\Type\Type
interface via@var
(phpstan/phpstan-src@713b98f), larastan/larastan#1567 (comment) - Previously absent type checks:
- Check
@mixin
PHPDoc tag above traits (phpstan/phpstan-src@0d0de94) - Check
@extends
,@implements
,@use
for unresolvable types (phpstan/phpstan-src@2bb5282), #11552 - Check types in
@method
tags (phpstan/phpstan-src@5b7e474) - Check generics
@method
@template
tags above traits (phpstan/phpstan-src@aadbf62) - Check types in
@property
tags (phpstan/phpstan-src@55ea2ae), #10752, #9356
- Check
- Validate inline PHPDoc
- New rule (level 3):
- New rules (level 4):
- Check too wide private property type (phpstan/phpstan-src@7453f4f)
- LogicalXorConstantConditionRule (phpstan/phpstan-src@3a12724, phpstan/phpstan-src@3b011f6), #7539
- Check that each trait is used and analysed at least once (phpstan/phpstan-src@c4d0527)
- Report useless return values of function calls like
var_export
without$return=true
(#3225), #11320, thanks @staabm! - ConstantLooseComparisonRule (phpstan/phpstan-src@6ebf236)
- Check
new
/function call/method call/static method call on a separate line without any side effects even without@phpstan-pure
PHPDoc tag on the declaration side - Always report always true conditions, except for last elseif and match arm (phpstan/phpstan-src@565fb0f)
- Remove "unreachable branches" rules: UnreachableIfBranchesRule, UnreachableTernaryElseBranchRule, unreachable arm error in MatchExpressionRule
- Because "always true" is always reported, these are no longer needed
- New rules (level 5):
- Check preg_quote delimiter sanity (#3252), #11338, thanks @staabm!
- Rule for
call_user_func()
(#2479), thanks @staabm! - Report useless
array_filter()
calls (#1077), #6840, thanks @leongersen! - Report useless
array_values()
calls (#2917), thanks @kamil-zacek! - Check array functions which require stringish values (#3132), #11141, #5848, #3694, #11111, thanks @schlndh!
- Check unresolvable parameters (#1319), thanks @rvanvelzen!
- Enforce
@no-named-arguments
(phpstan/phpstan-src@74ba8c2), #5968
- New rules (level 6):
- Previously absent type checks:
- Check missing types in
@phpstan-self-out
(phpstan/phpstan-src@892b319) - Check missing types in local type aliases (phpstan/phpstan-src@ce7ffaf)
- Check missing types in
@mixin
(phpstan/phpstan-src@3175c81)
- Check missing types in
- Previously absent type checks:
- New option:
polluteScopeWithBlock
(defaults totrue
,false
inphpstan-strict-rules
) (phpstan/phpstan-src@946cf18) - Support
@readonly
property and@immutable
class PHPDoc (#1295, #1335), #4082, thanks @herndlm! - Deprecate various
instanceof *Type
in favour of new methods onType
interface, (phpstan/phpstan-src@436e6d3), learn more: Why Is instanceof *Type Wrong and Getting Deprecated?
Improvements 🔧
- TableErrorFormatter - always output identifiers (phpstan/phpstan-src@fc66c24)
- Config option
exceptions.check.tooWideThrowType
made true by default (phpstan/phpstan-src@1b1da3e) - Use
implicitThrows
to only look for explicit throw points in too-wide@throws
rules when set tofalse
(phpstan/phpstan-src@a0e688c) - Rules about tooWideThrowType moved to level 4 (phpstan/phpstan-src@d7798d7)
- Both .php and .neon baselines now include error identifiers (phpstan/phpstan-src@f38addd, phpstan/phpstan-src@c8b7ea9)
- PHPDoc parser: Require whitespace before description with limited start tokens (phpstan/phpdoc-parser#128), phpstan/phpdoc-parser#125, thanks @rvanvelzen!
- Unescape strings in PHPDoc parser (phpstan/phpstan-src@97786ed)
- PHPDoc parser: add config for lines in its AST & enable ignoring errors within PHPDocs (#2807), thanks @janedbal!
- InvalidPhpDocTagValueRule: include PHPDoc line number in the error message (phpstan/phpstan-src@a04e0be)
- No implicit wildcard in FileExcluder (phpstan/phpstan-src@e19e6e5), #10299
- Report invalid exclude paths in PHP config (phpstan/phpstan-src@9718c14)
- Do not generalize template types, except when in
GenericObjectType
(#2818, #2821) - Non-static methods cannot be used as static callables in PHP 8+ (#2420), thanks @staabm!
- Analysis with zero files results in non-zero exit code (phpstan/phpstan-src@46ff440), #9410
- Fail build when project config uses custom extensions outside of analysed paths
- This will only occur after a run that uses already present and valid result cache
- Returning plain strings as errors no longer supported, use RuleErrorBuilder
- Require identifier in custom rules (phpstan/phpstan-src@969e6fa)
- New
RuleLevelHelper::accepts()
behaviour (phpstan/phpstan-src@941fc81), #11119, #4174 - Infer explicit mixed when instantiating generic class with unknown template types (phpstan/phpstan-src@089d4c6), #6398
- Use explicit mixed for global array variables (#1411), #7082, thanks @herndlm!
- Consider implicit throw points when the only explicit one is
Throw_
(phpstan/phpstan-src@22eef6d), #4912 - Run missing type check on
@param-out
(phpstan/phpstan-src@56b2002) - Report "missing return" error closer to where the return is missing (phpstan/phpstan-src@04f8636)
- Report dead types even in multi-exception catch (#2399), thanks @JanTvrdik!
- MethodSignatureRule - look at abstract trait method (phpstan/phpstan-src@5fd8cee)
- OverridingMethodRule - include template types in prototype declaring class description (phpstan/phpstan-src@ca2c66c)
- Detect overriding
@final
method in OverridingMethodRule, #9135 - Improve error wording of the NonexistentOffset, BooleanAndConstantConditionRule, and BooleanOrConstantConditionRule (#1882), thanks @VincentLanglet!
- Stricter ++/-- operator check (#3255), thanks @schlndh!
- Check mixed in binary operator (#3231), #7538, #10440, thanks @schlndh!
- Check mixed in unary operator (#3253), thanks @schlndh!
- Stub files validation - detect duplicate classes and functions (phpstan/phpstan-src@ddf8d5c, phpstan/phpstan-src@17e4b74)
- NoopRule - take advantage of impure points (phpstan/phpstan-src@a647052), #10389
- Improve impossible type checker for void-returning functions (#1857), #8169, thanks @rvanvelzen!
- Check template type variance in
@param-out
(phpstan/phpstan-src@7ceb19d), #8880 (comment) - Fix position variance of static method parameters (#2313), thanks @jiripudil!
- Empty
skipCheckGenericClasses
(phpstan/phpstan-src@28c2c79) - Report unnecessary nullsafe property fetch inside
??
/isset
/empty
with different message (#1253), thanks @rajyan! - Specify explicit mixed array type via
is_array
(#1191), thanks @herndlm! - TooWideMethodReturnTypehintRule - always report for final methods (phpstan/phpstan-src@c30e9a4)
- Move IllegalConstructorMethodCallRule and IllegalConstructorStaticCallRule to phpstan-strict-rules (phpstan/phpstan-src@124b30f, phpstan/phpstan-strict-rules@0c82c96)
- Check invalid PHPDocs in previously unchecked statement types (phpstan/phpstan-src@9780d35)
- InvalidPHPStanDocTagRule in StubValidator (phpstan/phpstan-src@9c2552b)
- CallToConstructorStatementWithoutSideEffectsRule - report class with no constructor (phpstan/phpstan-src@b116d25)
- ContainerFactory - always check duplicate files (phpstan/phpstan-src@939a715)
- Display parent class name for anonymous class like native PHP does (#3362), thanks @mvorisek!
- Always report static property fetch in
isset()
, not just on PHP 8.2+ (#3476), thanks @ondrejmirtes! - Revert "Dumb down parameter types in some recently added stubs" (phpstan/phpstan-src@950a491)
- Do not apply heuristics of
Collection<...>|Foo[]
being resolved to Collection of Foo (phpstan/phpstan-src@fff8f09) - Collected PHP errors cannot be ignored (phpstan/phpstan-src@1d3f431)
- Added missing rules to StubValidator (phpstan/phpstan-src@bf19914)
- Report precise offsets in errors (#3504), thanks @ruudk!
- IntersectionType - always describe list as list (phpstan/phpstan-src@f680629)
- ArrayType::describe - explicit mixed should be stated explicitly (phpstan/phpstan-src@6cf2238)
- Refactor IntersectionType::describe() (phpstan/phpstan-src@67fbfae)
- Remove inefficient caching from
PhpMethodReflection
andPhpFunctionReflection::isVariadic()
(#3534), thanks @staabm! - Clean file cache from unused items (phpstan/phpstan-src@466ad51)
- Journal for used generated containers (phpstan/phpstan-src@57c6588)
- Use named argument in error for variadic types (#3611), thanks @ruudk!
Bugfixes 🐛
- Fix invariance composition (#2054), thanks @jiripudil!
- Fix checking generic
mixed
type based on config (#2885), thanks @schlndh!
Function signature fixes 🤖
- Countable stub with
0|positive-int
(#1027), thanks @staabm! - More precise types for bcmath function parameters (#2217), thanks @Warxcell!
- Specify
Imagick
parameter types (#2334), thanks @zonuexe! max()
/min()
should expect non-empty-array (#2163), thanks @staabm!- Narrow
Closure::bind
$newScope
param (#2817), thanks @mvorisek! error_log
errors withmessage_type=2
(#2428), #9380, thanks @staabm!- Update functionMap (#2699, #2783), thanks @zonuexe!
- Improve image related functions signature (#3127), thanks @thg2k!
- Support
FILE_NO_DEFAULT_CONTEXT
infile()
(#2482), thanks @staabm! - Fix ftp related function signatures (#2551), thanks @thg2k!
- More precise
file()
flags args (#2476, #2482), thanks @staabm! - More precise
flock()
operation flags (#2477), thanks @staabm! - More precise
stream_socket_client()
signature (#2519), thanks @staabm! - More precise
scandir()
signature (#2518), thanks @staabm! - More precise
extract()
signature (#2517), thanks @staabm! - More precise
RecursiveIteratorIterator::__construct()
parameter types (#2835), thanks @staabm! - Update
Locale
signatures (#2880), thanks @devnix! - Improved the type of the
$mode
parameter for thecount()
(#3190), thanks @kuma3! - Check
filter_input*
type param type (#2271), thanks @herndlm! - Change
curl_setopt
function signature based on 2nd arg (#1719), thanks @staabm! - Support returning an array or a string in
count_chars()
(#3596), thanks @u01jmg3! - xdebug_get_function_stack: fix signature (#3605), thanks @janedbal!
Internals 🔍
- Tool to make optional parameters required across the codebase (phpstan/phpstan-src@7e366e0)
- A few more MutatingScope method parameters made required (phpstan/phpstan-src@2c4c0cd)
- CommandHelper::begin() parameters made required (phpstan/phpstan-src@f17cf9e)
- MethodTag - constructor parameter
$templateTags
is required (phpstan/phpstan-src@5b58f83) - InitializerExprTypeResolver - constructor parameter
$usePathConstantsAsConstantString
made required (phpstan/phpstan-src@f88d9ba) PhpMethodReflectionFactory::create()
- all parameters are required (phpstan/phpstan-src@8bfbf8f)- FunctionCallParametersCheck - parameters
$nodeType
and$acceptsNamedArguments
made required (phpstan/phpstan-src@4937527) - MethodParameterComparisonHelper - parameter
$ignorable
ofcompare()
method made required (phpstan/phpstan-src@f85a500) - Parameter
$dateTimeClass
of DateTimeModifyReturnTypeExtension constructor made required (phpstan/phpstan-src@a8cd423) - NativeFunctionReflection construct parameters made required (phpstan/phpstan-src@64ff598)
- Cover AccessoryArrayListType constructor with BC promise (phpstan/phpstan-src@51de903)
- Add
PhpVersion
parameter to variousType
methods (#3478), thanks @VincentLanglet! - Move ContainerDynamicReturnTypeExtension to build/PHPStan (phpstan/phpstan-src@5651bec)
- Renamed NewOptimizedDirectorySourceLocator to OptimizedDirectorySourceLocator (phpstan/phpstan-src@db02a30)
- Remove unneded abstraction (phpstan/phpstan-src@f302c90)
- Introduce native return types thanks to PHP 7.4 return type covariance (phpstan/phpstan-src@392f090)
- ReadWritePropertiesExtension - use ExtendedPropertyReflection in parameter type (phpstan/phpstan-src@f0a6296)
- Declare more precise
getClass()
return types in extension interfaces (#1754), thanks @staabm! - (phpstan/phpstan-src@38cb5a3)
- HasOffsetType - put constructor parameter type natively (phpstan/phpstan-src@b5accb3)
- Printer is covered by BC promise (phpstan/phpstan-src@b085833)
- More interfaces that are not supposed to be implemented in userland (phpstan/phpstan-src@778af2e, phpstan/phpstan-src@cb6ab55)
- Refactored
FunctionCallParametersCheck::check()
parameters (phpstan/phpstan-src@710e09c) - Spread list usages in Reflection, Scope, Type (#3530), thanks @janedbal!
- Remove $isFinal dead-code in PhpFunctionReflection (#3545), thanks @staabm!
- Get rid of unnecessary
instanceof self
inConstantArrayType
(#3552), thanks @herndlm! - test: use
bashunit -a
exit_code to check for errors (#3533), thanks @Chemaclass! - Upgrade bashunit:0.18.0 for e2e tests (#3614), thanks @Chemaclass!
- Remove dead code (#3575), thanks @staabm!
- Remove dead code in ConstantConditionRuleHelper (#3597), thanks @staabm!