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

possibly incorrect MixedMethodCall: "Cannot call method on an unknown class" when using templated class-string param & return types #1263

Closed
SignpostMarv opened this issue Jan 31, 2019 · 5 comments
Labels

Comments

@SignpostMarv
Copy link
Contributor

This templated scenario seems to trigger an error for two different reasons:

<?php
declare(strict_types=1);

/**
* @template T
*
* @param class-string $maybe
* @param class-string<T> $shouldBe
*
* @return class-string<T>
*
* @throws InvalidArgumentException if $maybe is not an implementation of $shouldBe
*/
function Foo(string $maybe, string $shouldBe) : string {
  if ( ! is_a($maybe, $shouldBe, true)) {
    throw new InvalidArgumentException('nope');
  }
  
  return $maybe;
}

Foo(DateTimeImmutable::class, DateTimeImmutable::class)::getFromMutable(new DateTime());
@weirdan
Copy link
Collaborator

weirdan commented Jan 31, 2019

I think it could be simplified further (https://getpsalm.org/r/94b619a6ac):

<?php
/**
* @template T
*
* @param class-string<T> $shouldBe
* @return class-string<T>
*/
function identity(string $shouldBe) : string {  return $shouldBe; }

identity(DateTimeImmutable::class)::getFromMutable(new DateTime());

@SignpostMarv
Copy link
Contributor Author

@weirdan does psalm internals make a distinction between class-string for a known class and class-string for an unknown class ?

@weirdan
Copy link
Collaborator

weirdan commented Jan 31, 2019

Depends on what you mean by that, I suppose. class-string<Exception> and just class-string do result in different behavior in some places.

However in this case the return type is inferred to be class-string<> (https://getpsalm.org/r/ecaf471209), which is certainly not something I'd expect to see.

@SignpostMarv
Copy link
Contributor Author

SignpostMarv commented Jan 31, 2019

@weirdan I was using class-string<void> for known unknown class testing, but it's detected as a reserved word now :p

i.e. void is a reserved word thus can never be a class.

@muglug
Copy link
Collaborator

muglug commented Feb 1, 2019

class-string is equivalent to class-string<object>

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

No branches or pull requests

3 participants