Report implicit mixed inside compound types as too wide at all rule levels#5608
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Report implicit mixed inside compound types as too wide at all rule levels#5608phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…evels When a variable is typed as bare `array` (i.e. `array<mixed, mixed>` with implicit mixed), returning it from a function declared as `@return array<string, string>` should report an error regardless of the rule level. This was a regression introduced in 1.9.5 when the early return in `transformCommonType()` was added — it short-circuited the entire transformation when both `checkExplicitMixed` and `checkImplicitMixed` were false, which prevented implicit mixed types inside compound types from being converted to StrictMixedType. The fix narrows the early return to only fire for top-level MixedType, and adds a new condition that converts non-top-level implicit mixed to StrictMixedType even when `checkImplicitMixed` is false. This means `array`, `iterable`, and other compound types with unspecified type parameters are now checked strictly against their expected types at all rule levels. Closes phpstan/phpstan#8681
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes phpstan/phpstan#8681
Summary
Since 1.9.5, PHPStan stopped reporting that a bare
array(implicitarray<mixed, mixed>) is too wide for a declared@return array<string, string>. The root cause was inRuleLevelHelper::transformCommonType(): an early return checked!$this->checkExplicitMixed && !$this->checkImplicitMixedand short-circuited the entire transformation, preventing implicit mixed inside compound types from being converted toStrictMixedType.The fix:
MixedType(not a compound type containing mixed)$isTopLeveltracker so that implicit mixed at non-top-level positions (array value types, array key types, callable return types, generic type parameters) is converted toStrictMixedTypeeven whencheckImplicitMixedis falseTemplateMixedType, which should remain as-isThis restores the pre-1.9.5 behavior where
arrayis not silently accepted wherearray<string, string>is expected, regardless of rule level.Scope of changes
The fix applies to all call sites of
RuleLevelHelper::accepts(), meaning it affects:arrayassigned toarray<string, string>propertyarraypassed wherearray<string, string>parameter expectedNew errors are now correctly reported for:
arraywherearray<K, V>is expected (both key and value mismatch)iterablewhereiterable<K, V>is expectedClosure(): mixedwherecallable(): intis expected (implicit mixed return type)Generic<mixed>whereGeneric<Specific>is expected (when mixed is implicit)Test changes
bug-8681.php)@return string[]→@return list<string>annotations on test helper methods exposed by the change