Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subtracting types from union template types #2652

Merged
merged 1 commit into from Sep 29, 2023

Conversation

rvanvelzen
Copy link
Contributor

@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 1.11.x. If your code is relevant on 1.10.x and you want it to be released sooner, please rebase your pull request and change its target to 1.10.x.

@ondrejmirtes
Copy link
Member

FYI @rvanvelzen I described an issue here phpstan/phpstan#9925 (reply in thread), I think it's up your alley to fix it :)

New_ has special behaviour in MutatingScope::exactInstantiation that looks at class-level @template and uses them for constructor. Maybe we would benefit from moving it into ParametersAcceptorSelector, maybe that would help us in part to solve that issue.

If you decide to look at it that would be great!

@@ -1341,6 +1341,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/nullsafe-vs-scalar.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8517.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-9803.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9939.php');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be put in PHP_VERSION_ID if.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it's better to put in some random place in the middle, because at the end it's more likely to have conflict when merging into 1.11.x

@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs // lint >= 8.1

@ondrejmirtes ondrejmirtes merged commit 7608186 into phpstan:1.10.x Sep 29, 2023
414 of 415 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@thg2k
Copy link
Contributor

thg2k commented Sep 29, 2023

Maybe a simplified test case instead of that bloated one? This is also PHP-version independent

<?php declare(strict_types = 1);

/**
 * @template T of string|array<string>
 */
class C {
  /**
   * @param T $a
   */
  public function foo($a): string {
    if (is_array($a)) {
      return "";
    }
    \PHPStan\Testing\assertType('T of string (class C, argument)', $a);
    return (string) $a;
  }
}

https://phpstan.org/r/bc6b8adf-a25e-4960-8f43-eca8399cf509

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants