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

add psalm types #531

Merged
merged 4 commits into from
Jan 15, 2020
Merged

add psalm types #531

merged 4 commits into from
Jan 15, 2020

Conversation

orklah
Copy link
Contributor

@orklah orklah commented Jan 14, 2020

This is a draft PR with the changes I made for #525 to see the rest of the CI and to discuss about some changes

The increase in type coverage is disappointing because a lot of mixed types still comes from outside.

Still, the changes prepare the way for when
nikic/PHP-Parser#647
and
vimeo/psalm#2612

will be merged.

@@ -448,6 +450,8 @@ private function assertClassExist(string $className) : void
* @throws NoObjectProvided
* @throws NotAnObject
* @throws ObjectNotInstanceOfClass
*
* @psalm-assert object $object
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is a good addition, but it adds an error on psalm because it complains that when this method is called, the variable can only be an object and this is redundant.

I think we should suppress the error and keep the assert.

Copy link
Member

@Ocramius Ocramius Jan 15, 2020

Choose a reason for hiding this comment

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

I think the assertion could be refined further to @psalm-assert T, where T is the type for this ReflectionProperty's owner, but that's for another time/discussion.

Indeed, psalm assertions are annoying when put in test scope, because they disallow obvious/dumb scenarios :D

@@ -16,13 +19,14 @@ final class ReflectionClassConstantStringCast
public static function toString(ReflectionClassConstant $constantReflection) : string
{
$value = $constantReflection->getValue();
assert($value === null || is_scalar($value));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A constant can technically return an array. This makes sure that it doesn't happen

@@ -874,7 +874,7 @@ public function isInterface() : bool
* Get the traits used, if any are defined. If this class does not have any
* defined traits, this will return an empty array.
*
* @return ReflectionClass[]
* @return list<ReflectionClass>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is discussed here: #530 It raises an error with Psalm that I think is legitimate and I don't know how to handle.

@@ -519,7 +519,7 @@ static function (ReflectionClassConstant $constant) : string {
* Get an associative array of the defined constants in this class,
* with keys as constant names and values as {@see ReflectionClassConstant} objects.
*
* @return ReflectionClassConstant[] indexed by name
* @return array<string, ReflectionClassConstant> indexed by name
Copy link
Contributor Author

@orklah orklah Jan 14, 2020

Choose a reason for hiding this comment

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

This raise a new error on Psalm too because their internal stubs expects this to be an array<int, ReflectionClassConstant>. Not sure who's right on this

Copy link
Member

Choose a reason for hiding this comment

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

I think the stub may need updating here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://3v4l.org/t05Dv I took this example directly from php.net. The index of array is numerical.
I'll add the case to #530 as it is roughly the same issue

Copy link
Member

Choose a reason for hiding this comment

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

Yeh, the question is whether it is a list or not: I think it is a list in core too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeh, the question is whether it is a list or not: I think it is a list in core too

I think you got me wrong. The library returns array<string, ReflectionClassConstant> right now but native Object return array<int, ReflectionClassConstant> (and more probably a list<ReflectionClassConstant> but this is not the issue).

I'll silence those errors in psalm to allow the merge, but I think those are legitimate issues.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, careful! We have adapters and then there is this stuff: the adapters are supposed to be compliant with internal definitions, but these classes don't need to be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, careful! We have adapters and then there is this stuff: the adapters are supposed to be compliant with internal definitions, but these classes don't need to be.

oh, I get it. But Psalm sees that this class implements Reflection and warn about the type of the interface being different than declared

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Overall excellent work, but we need (unavoidable, sorry :-( ) to use @psalm--prefixed annotations for any types that aren't understood by the wider ecosystem, sorry :S

See for example in ProxyManager: https://github.com/Ocramius/ProxyManager/blob/20cd371e97e73d04711f21caf3079d783f9ab936/src/ProxyManager/Proxy/GhostObjectInterface.php#L12-L23

src/NodeCompiler/CompileNodeToValue.php Outdated Show resolved Hide resolved
src/Reflection/ReflectionClass.php Outdated Show resolved Hide resolved
src/SourceLocator/Ast/FindReflectionsInTree.php Outdated Show resolved Hide resolved
src/SourceLocator/Ast/Locator.php Outdated Show resolved Hide resolved
@orklah
Copy link
Contributor Author

orklah commented Jan 14, 2020

Overall excellent work, but we need (unavoidable, sorry :-( ) to use @psalm--prefixed annotations for any types that aren't understood by the wider ecosystem, sorry :S

Will do 👍

Can you look at my own review? I highlighted some important changes

Thanks!

@Ocramius
Copy link
Member

Read through review comments.

Note that the list type was added to psalm recently

@orklah orklah marked this pull request as ready for review January 15, 2020 17:01
@orklah
Copy link
Contributor Author

orklah commented Jan 15, 2020

Note that the list type was added to psalm recently

Yeah but PHPStorm won't read it, so I added @psalm- where possible while keeping @return

I just removed the draft tag. I suppressed or fixed the remaining psalm errors

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Let's ship this as-is for now, it is a whole lot of added detail that is indeed immediate added value for the lib 🚢

@Ocramius
Copy link
Member

Note that the list type was added to psalm recently

Yeah but PHPStorm won't read it, so I added @psalm- where possible while keeping @return

I just removed the draft tag. I suppressed or fixed the remaining psalm errors

This was to note that the stubs may not yet use list, since it is a recent addition ;-)

@Ocramius Ocramius merged commit e276075 into Roave:master Jan 15, 2020
@Ocramius
Copy link
Member

Thanks @orklah!

@orklah orklah deleted the types6 branch January 15, 2020 17:32
@orklah
Copy link
Contributor Author

orklah commented Jan 15, 2020

@Ocramius Thanks for merging :)

I must have broke Psalm on CI though. I added a windows path on xml file. It worked fine locally but it breaks the CI. Can you fix it or should I create a PR?

@Ocramius
Copy link
Member

Please do send a separate patch when you have time: I'm not actively working on the package these weeks, besides following contributions

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

Successfully merging this pull request may close these issues.

2 participants