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

Calls to parent::someMethod should remember type params #2621

Closed
muglug opened this issue Nov 27, 2019 · 8 comments
Closed

Calls to parent::someMethod should remember type params #2621

muglug opened this issue Nov 27, 2019 · 8 comments

Comments

@muglug
Copy link
Contributor

muglug commented Nov 27, 2019

/**
 * @template T
 */
class Stringer {
  /**
   * @param T $t
   */
  public static function takesParam($t) : string {
    return "hello";
  }
}

class AParent {}
class A extends AParent {}

/**
 * @template-extends Stringer<A>
 */
class AStringer extends Stringer {
  public static function foo() : string {
    return parent::takesParam("should not be allowed");
  }
}

Expected output

Argument 1 of Stringer::takesParam expects A, string provided

Actual output

No issue - https://phpstan.org/r/c58ef371-1108-4e75-ae3b-b4f9a077ec69

@arnaud-lb
Copy link
Contributor

Do you have examples when it's unsafe ?

@muglug
Copy link
Contributor Author

muglug commented Nov 30, 2019

This is my best effort so far: https://phpstan.org/r/63f3bd8d-d5c1-4f59-9bbf-a5f91b93e442

But it's also, I think, how generics should work.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Dec 30, 2019
@eigan
Copy link

eigan commented May 22, 2020

I think this might be relevant: https://phpstan.org/r/efdfcc33-84d0-40e3-8b9f-28e2193b52d8

@ondrejmirtes
Copy link
Member

@eigan I guess your bug is the same bug as #3251

@phpstan-bot
Copy link
Contributor

@muglug After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-37: Method AStringer::foo() should return A but returns object.
+No errors

@phpstan-bot
Copy link
Contributor

@eigan After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-35: Method Child::give() should return B but returns A.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@4acbc6c

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants