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

Cannot suppress UnnecessaryVarAnnotation #3573

Closed
ostrolucky opened this issue Jun 12, 2020 · 10 comments · Fixed by #6242
Closed

Cannot suppress UnnecessaryVarAnnotation #3573

ostrolucky opened this issue Jun 12, 2020 · 10 comments · Fixed by #6242
Labels

Comments

@ostrolucky
Copy link
Contributor

https://psalm.dev/r/35a6f3cc6a

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/35a6f3cc6a
<?php

/** 
 * @param class-string $class 
 * @psalm-suppress UnnecessaryVarAnnotation
 */
function foo(string $class): Foo {
    if (!is_subclass_of($class, Foo::class)) {
        throw new \LogicException();
    }
    
    /** @psalm-suppress UnnecessaryVarAnnotation */ 
    /**
     * @psalm-suppress UnnecessaryVarAnnotation
     * @var Foo $instance 
     * @psalm-suppress UnnecessaryVarAnnotation
     */
    $instance = new $class();

    return $instance;
}
Psalm output (using commit 211f014):

ERROR: UndefinedClass - 7:30 - Class or interface Foo does not exist

ERROR: UndefinedClass - 8:33 - Class or interface Foo does not exist

INFO: MixedArgument - 8:33 - Argument 2 of is_subclass_of cannot be mixed, expecting string

ERROR: UndefinedDocblockClass - 18:5 - Docblock-defined class or interface Foo does not exist

ERROR: UnnecessaryVarAnnotation - 15:13 - The @var Foo annotation for $instance is unnecessary

@weirdan weirdan added the bug label Jun 12, 2020
@weirdan
Copy link
Collaborator

weirdan commented Jun 12, 2020

This: https://psalm.dev/r/7711914ceb not working looks lie a bug to me.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/7711914ceb
<?php

class Foo {}
/** 
 * @param class-string $class 
 */
function foo(string $class): Foo {
    if (!is_subclass_of($class, Foo::class)) {
        throw new \LogicException();
    }
    
    /**
     * @psalm-suppress UnnecessaryVarAnnotation
     * @var Foo $instance 
     */
    $instance = new $class();

    return $instance;
}
Psalm output (using commit 211f014):

ERROR: UnnecessaryVarAnnotation - 14:13 - The @var Foo annotation for $instance is unnecessary

@ostrolucky
Copy link
Contributor Author

Looks like same issue is for InvalidDocBlock https://psalm.dev/r/cdc465928b

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/cdc465928b
<?php

/**
 * @template T as (object|array<string, mixed>)
 */
abstract class Provider
{
    /**
     * @param mixed[]|object $entity
     * @psalm-param T $entity
     *
     * @return mixed[]
     */
    abstract protected function collectDataToReindexRow($entity): array;
}

/** @psalm-suppress InvalidDocblock */
/**
 * @psalm-suppress InvalidDocblock
 * @extends Provider<(array<string, mixed>>)>
 */
class A extends Provider
{
    protected function collectDataToReindexRow($row): array
    {
        return [];
    }
}
Psalm output (using commit 8ca7a88):

ERROR: InvalidDocblock - 22:1 - Cannot parse generic type in docblock for A

@muglug muglug closed this as completed in 683bde9 Jun 13, 2020
@martindilling
Copy link

Don't really know why this was closed without any comment.

So just for others who run into this. I found out how to suppress this: https://psalm.dev/r/b3c4b437cb

The suppress docblock should be right above the code line.

/** @var Foo $instance */
/** @psalm-suppress UnnecessaryVarAnnotation */
$instance = new $class();

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b3c4b437cb
<?php

class Foo {}
/** 
 * @param class-string $class 
 */
function foo(string $class): Foo {
    if (!is_subclass_of($class, Foo::class)) {
        throw new \LogicException();
    }
    
    /** @var Foo $instance */
    /** @psalm-suppress UnnecessaryVarAnnotation */
    $instance = new $class();

    return $instance;
}
Psalm output (using commit 8fe1fbc):

No issues!

@artemmolotov
Copy link

This issue isn't fixed. Please, check first snippet.

@jkowalleck
Copy link

jkowalleck commented Aug 4, 2021

@muglug,
since the issue was closed/fixed without a resolution/note here,
could you give an example how the reported behavior could be solved?

thanks in advance.

@orklah
Copy link
Collaborator

orklah commented Aug 4, 2021

The PR above should fix the issue.

@martindilling just FYI, your fix was not really a fix. The suppression was not effective, but neither was the @var because it was not above the code anymore. So you indirectly fixed the issue by removing what Psalm was complaining about 😄

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

Successfully merging a pull request may close this issue.

6 participants