Skip to content

Conversation

@leongersen
Copy link
Contributor

@leongersen leongersen commented Aug 20, 2020

A take at phpstan/phpstan#3753. I couldn't find an existing way to retain the original type information in a NeverType, but it helps the clarity of the error message. An alternative would be to check the type in InvalidPhpDocVarTagTypeRule, instead of in TypeNodeResolver.

If this is the correct aproach, I'll add the same checks to IncompatiblePhpDocTypeRule.

@leongersen leongersen marked this pull request as draft August 20, 2020 12:58
@ondrejmirtes
Copy link
Member

Hi, please read my instructions at phpstan/phpstan#3753 (comment) more carefully and follow them.

  1. we need to TypeCombinator::intersect() the key here with new UnionType([new IntegerType(), new StringType()])
  2. implement recursive checking for ErrorType/NeverType using TypeTraverser in this rule and others

This PR cannot be accepted because the code is different and doesn't behave as we want it to.

@leongersen
Copy link
Contributor Author

@ondrejmirtes Could you give a hint on how the original type would be infered from the NeverType (so that it can be used in the error message)?

@ondrejmirtes
Copy link
Member

That's out of scope of this PR and too hard. I simply want to achieve this error message: https://phpstan.org/r/ab5ad711-5186-4fae-a52a-3cb4106892c1

Improving this error message would be something for another time and a major version.

@leongersen leongersen force-pushed the check-array-key-type branch 3 times, most recently from 9bb33a0 to 8ee6d2b Compare August 20, 2020 14:57
@leongersen
Copy link
Contributor Author

Ok, that's clear. I've applied your suggestions and added support for @param and @return.

@leongersen leongersen force-pushed the check-array-key-type branch from 8ee6d2b to d2822c8 Compare August 20, 2020 15:02
Copy link
Member

Choose a reason for hiding this comment

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

I imagined something like hasUnresolvableType(Type $type): bool instead.

Copy link
Member

Choose a reason for hiding this comment

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

The same logic should be applied in IncompatiblePropertyPhpDocTypeRule as well.

@leongersen leongersen force-pushed the check-array-key-type branch from d2822c8 to 6eee9d2 Compare August 21, 2020 07:10
@leongersen
Copy link
Contributor Author

@ondrejmirtes What would you like me to do with the failing tests in NodeScopeResolverTest?

For example, for the type array<TKey, TValue> $iterator, the added intersection result in the following change:

-'array<TKey (method Bug3266\Foo::iteratorToArray(), argument), TValue (method Bug3266\Foo::iteratorToArray(), argument)>' +'array<(int&TKey (method Bug3266\Foo::iteratorToArray(), argument))|(string&TKey (method Bug3266\Foo::iteratorToArray(), argument)), TValue (method Bug3266\Foo::iteratorToArray(), argument)>' 

Which is correct, but I don't know if changing this output is desirable?

@ondrejmirtes
Copy link
Member

@leongersen I think I want array<mixed, X> to keep the mixed untouched, which will also fix the template type issue.

@leongersen
Copy link
Contributor Author

Sorry, I don't really understand what you mean. Are you saying to check if the array key type is a supertype of the mixed type?

Also, would you mind elaborating on why checking whether int|string->accepts() the array key type is not a valid solution here? I'm guessing I'm misunderstanding what accepts is for.

@ondrejmirtes ondrejmirtes force-pushed the master branch 2 times, most recently from f54fd5f to eca550a Compare October 28, 2020 19:41
@ondrejmirtes ondrejmirtes force-pushed the master branch 3 times, most recently from d45166a to 3526237 Compare December 12, 2020 10:56
@ondrejmirtes
Copy link
Member

Superseded by 1de5de8

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

Labels

None yet

2 participants