Add @ignore-var and @psalm-ignore-var#5488
Merged
muglug merged 2 commits intovimeo:masterfrom Mar 27, 2021
Merged
Conversation
|
I found these snippets: https://psalm.dev/r/c19d749dbe<?php
class A { public int $a = 0; }
class B { public int $b = 0; }
/** @template T */
interface Generic
{
/** @psalm-return iterable<T> */
public function fetchAll();
}
/** @param Generic<A> $ag */
function (Generic $ag): void {
// psalm itself can infer the type well enough
$collection = $ag->fetchAll();
foreach ($collection as $_) {
/** @psalm-trace $_ */
}
// need this @var to get the power of auto-completion in phpstorm
/** @var A[] */
$collection = $ag->fetchAll();
foreach ($collection as $item) {
echo $item->a; // auto-completion can be used here
/** @psalm-trace $item */
}
// but explicitly declared @var hides the programmer's mistake
/** @var B[] */
$collection = $ag->fetchAll();
foreach ($collection as $item) {
echo $item->b; // the item is actually A, it has no member named b
/** @psalm-trace $item */
}
// if psalm ignores @var which co-located with a specific annotation,
// programmers can annotate it for auto-completion,
// while psalm still catch type errors.
/**
* @var B[]
* @psalm-ignore-var
*/
$collection = $ag->fetchAll();
foreach ($collection as $item) {
echo $item->b; // if @var is ignored, this error can be caught in psalm
/** @psalm-trace $item */
}
}; |
Collaborator
|
I would also suggest to file an issue with JetBrains (https://youtrack.jetbrains.com/issues/WI) to implement support for |
Collaborator
|
Thanks, this is great! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This annotation is used to ignore the
@varannotation written in the same docblock.Background
To take advantage of the IDE's auto-completion, you may sometimes want to use explicit
@varannotations even when psalm can infer the type well enough. This weakens the effectiveness of type checking in many cases since the explicit@varannotation overrides the types inferred by psalm.See this example for more detail.
https://psalm.dev/r/c19d749dbe
What is changed if this PR is merged
Psalm ignores the
@varannotation which is co-located with@psalm-ignore-var. Then IDEs that don't fully understand complex types like generics can use the manually specified types with@varfor auto-completion, while psalm can still use its own inferred types for type checking.Disclaimer
I'm not confident about my English skills. The sentences may seem odd to you. I know you aren't my English teacher, but if any wordings of the added doc or this PR sounds unnatural, please feel free to correct them. Then I can train myself better.