-
Notifications
You must be signed in to change notification settings - Fork 131
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
add psalm types #531
Conversation
@@ -448,6 +450,8 @@ private function assertClassExist(string $className) : void | |||
* @throws NoObjectProvided | |||
* @throws NotAnObject | |||
* @throws ObjectNotInstanceOfClass | |||
* | |||
* @psalm-assert object $object |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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
src/Reflection/ReflectionClass.php
Outdated
@@ -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> |
There was a problem hiding this comment.
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.
src/Reflection/ReflectionClass.php
Outdated
@@ -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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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
Will do 👍 Can you look at my own review? I highlighted some important changes Thanks! |
Read through review comments. Note that the |
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 |
There was a problem hiding this 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 🚢
This was to note that the stubs may not yet use |
Thanks @orklah! |
@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? |
Please do send a separate patch when you have time: I'm not actively working on the package these weeks, besides following contributions |
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.