Skip to content

Reject strict mixed when a template type is expected - #6416

Open
klimick wants to merge 1 commit into
phpstan:2.2.xfrom
klimick:fix-strict-mixed-template
Open

Reject strict mixed when a template type is expected#6416
klimick wants to merge 1 commit into
phpstan:2.2.xfrom
klimick:fix-strict-mixed-template

Conversation

@klimick

@klimick klimick commented Sep 11, 2026

Copy link
Copy Markdown

At level 9 and above, passing explicitly typed mixed to callable(A): A should produce an error because the value is not guaranteed to match A.
This change adds that diagnostic.

Below level 9, passing mixed to callable(A): A is allowed. This behavior remains unchanged.

Closes phpstan/phpstan#11935

@klimick

klimick commented Sep 11, 2026

Copy link
Copy Markdown
Author

This PR makes PHPStan report an error for code like this:

/**
* @phpstan-template TValue
*/
final class NonThreadSafeValue extends ThreadSafe
{
   /**
    * @phpstan-return TValue
    *
    * ERROR: Method NonThreadSafeValue::deserialize() should return TValue but returns mixed.
    */
   public function deserialize(): mixed
   {
       return igbinary_unserialize($this->variable);
   }
}

This is technically correct because mixed is not guaranteed to be the template type TValue.
Should this stricter behavior be enabled only under bleedingEdge or behind a separate configuration option?

P.S. I noticed this while investigating the failed PMMP integration job in CI.
P.S.S The failed jobs are unrelated to this change, except for PMMP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant