psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Allow suppressing a psalm issue for a single Attribute or parameter

Open annervisser opened this issue 10 months ago • 1 comments

When using attributes on a class, there is no way to ignore an issue for just one attribute. Currently, the only option is to ignore the error type for the WHOLE class.

  • A way to ignore an issue for a single attribute would solve this particular issue.
  • A way to ignore an issue for just a specific parameter would allow more fine-grained suppressing for this case but also any suppresses on functions and methods.

As far as I can tell this is also not possible using the config issueHandlers, since InvalidArgument wants a referencedFunction. I haven't been able to get that to work for an Attribute

https://psalm.dev/r/312d1075f3

annervisser avatar Apr 10 '24 09:04 annervisser

I found these snippets:

https://psalm.dev/r/312d1075f3
<?php

#[Attribute(Attribute::TARGET_CLASS)]
final class Attribute1 {
    public function __construct(string $input) {}
}

#[Attribute(Attribute::TARGET_CLASS)]
final class Attribute2 {
    public function __construct(string $input) {}
}

// The InvalidArgument on Attribute1 should not be suppressed:
#[Attribute1(false)]
// I want to suppress the argument on Attribute2:
#[Attribute2(false)]
class Example {
	public function method1(string $input): array {
        // This InvalidArgument should not be suppressed:
        return array_values($input);
    }
}
Psalm output (using commit ef3b018):

ERROR: InvalidArgument - 14:14 - Argument 1 of Attribute1::__construct cannot be false, string value expected

ERROR: InvalidArgument - 16:14 - Argument 1 of Attribute2::__construct cannot be false, string value expected

ERROR: InvalidArgument - 20:29 - Argument 1 of array_values expects array<array-key, mixed>, but string provided

psalm-github-bot[bot] avatar Apr 10 '24 09:04 psalm-github-bot[bot]