phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

@noinspection OnlyWritesOnParameterInspection marked as redundant when using Suppress for statement

Open InvisibleSmiley opened this issue 1 year ago • 0 comments

Subject Details
Plugin Php Inspections (EA Extended)
Language level PHP 8.1

Current behaviour

Suppress for statement produces @noinspection OnlyWritesOnParameterInspection at statement which then is marked as redundant. Suppress for file/method work without producing new issues.

Expected behaviour

Either Suppress for statement should not be available in this case, or the result should not be marked as redundant.

Test case

<?php

declare(strict_types=1);

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

class ReadonlyArrayAccess implements ArrayAccess
{
    /** @param array<mixed> $data */
    public function __construct(private array $data)
    {
    }

    public function offsetExists(mixed $offset): bool
    {
        return isset($this->data[$offset]);
    }

    public function offsetGet(mixed $offset): mixed
    {
        return $this->data[$offset] ?? null;
    }

    public function offsetSet(mixed $offset, mixed $value): void
    {
        throw new Exception('not allowed');
    }

    public function offsetUnset(mixed $offset): void
    {
        throw new Exception('not allowed');
    }
}

#[CoversClass(ReadonlyArrayAccess::class)]
class ReadonlyAccessTest extends TestCase
{
    public function testArrayAccessSet(): void
    {
        $data = new ReadonlyArrayAccess([]);
        $this->expectExceptionObject(new Exception('not allowed'));
        $data['foo'] = 'bar'; // This line triggers "Parameter/variable is not used" inspection
    }
}

InvisibleSmiley avatar Oct 17 '24 10:10 InvisibleSmiley