-
Notifications
You must be signed in to change notification settings - Fork 147
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
Added type assertions for multiple methods not covered by existing @psalm-assert
functionality
#160
Added type assertions for multiple methods not covered by existing @psalm-assert
functionality
#160
Conversation
Note: I do not know how to test |
Note to self:
|
…e `string|object` An object implementing `#__toString()` suffices for `filter_var()` to work, so we can't assume that after the assertion, the value being asserted upon is a `string`. Instead, `string|object` is our closest bet. Ref: webmozarts#160 (comment) Note: will fail until vimeo/psalm#2452 has some sort of resolution.
|
32d2ad6
to
4e28a31
Compare
The entire I don't have an immediate solution: will likely resort to dropping all |
59fcf57
to
500f5d2
Compare
@BackEndTea I've added |
We still have the issue of type assertions being considered redundant by psalm: https://psalm.dev/r/33a97818c1 Another thing i'm not quite sure of is the effect of the |
This one is indeed incorrect/to be removed, since having the types being constrained seemed more useful in the typical PHPUnit scenario: /** @var T $a */
$a = someExpr();
$b = someOtherExpr();
Assert::same($a, $b);
$b->canNowCallSomethingOnThis(); I'll drop it though, since the Would you consider
Considering |
I guess it is somewhat unusual for a pure method to have a |
Yeah, I'm raising an upstream issue: exceptions would be pure if it wasn't for the fact that they contain a stack trace. In practice, any method that |
@BackEndTea I reported vimeo/psalm#2456 about |
I'd be okay with a The To solve that we need to either remove them here, or we need a way to give psalm that information, without triggering the redundant condition error |
@BackEndTea ok, will likely need to go through the assertions one-by-one and check them for redundancy.
We should probably remove the |
Just an update here: vimeo/psalm@4b715cd fixes |
…as `Assert::string()` will not be considered "redundant" Ref: vimeo/psalm#2456 Ref: vimeo/psalm@4b715cd Ref: webmozarts#160 (comment)
I do have a concern about using We don't analyse the But marking it as pure prevents our users from overriding that method and doing impure things, e.g. any kind of logging they may do from that class. But not marking it and leaving it as is, allows our users to do that, but it also means that we are lying about the methods being pure. In all honesty i'm not sure what the best solution would be. |
Few things to consider:
Yes, this means that the library won't pass any sort of internal SA unless either:
Consumers will be able to ignore this though, unless they subclass (which probably already has broken SA). The |
@BackEndTea would you be OK with deprecating |
I have to think about that. I'm not sure how many people make use of it. |
Note: I'll also add this library to upstream psalm test suite, once through 👍 |
…e `lowercase-string` type Ref: https://github.com/vimeo/psalm/releases/tag/3.9.0
No worries. I compared both branches and you already addressed a lot more here. When this one gets merged #167 can be considered obsolete. I don't mind how pure/immutable supports makes it into this lib as long as it does ;-) |
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.
You didn't mark Assert::ip*()
as pure. Is there a reason why not?
While these functions support integers, we do not necessarily intend downstream consumers to rely on the widened type. Ref: webmozarts#160 (comment)
I got a few comments/questions. When those are resolved this should be good to merge. |
All functions that have `@psalm-assert` annotations now have static analysis tests. Ref: https://github.com/webmozart/assert/pull/160/files#r381317558
@BackEndTea all done: let's see if CI passes, then we should be good to go 👍 |
Found a few more things, didn't think of these edge cases being impure, but the |
…o `__toString` In fact, the following explains it: ```php 'foo' == new class { public function __toString() : string {echo 'hi'; return 'bar'; }}; ``` This is a fucking mess of a language, but it's what we have to deal with :-( Ref: webmozarts#160 (comment) Ref: webmozarts#160 (comment) Ref: webmozarts#160 (comment)
@BackEndTea adjusted according to comments above about purity: good looking out! |
Thanks for the work on this |
W00P W00P! Thanks for the very careful review, @BackEndTea! |
🎉 Thanks for all the psalm improvements @Ocramius |
@BackEndTea could you please push a |
Or better: revert the merge on `master, then merge without squash? :S |
…4220) * Github Actions: add a job which tests the lowest supported versions * Update tests.yaml * run lowest only on php 7.2 to reduce the number of generated jobs * faise min webmozart/assert version we need at least webmozarts/assert#160 * use quotes
Related:
scalar&!empty
? vimeo/psalm#2449non-empty-list
type assertions are incorrectly considered redundant whenlist
is encountered vimeo/psalm#2450@psalm-assert
type assertions seem to ignorecallable():T
types vimeo/psalm#2451@psalm-pure
,: void
methods (assertions) and exceptions vimeo/psalm#2456