php:S1144 should not report private method used in PHPDoc @dataProvider annotation

Which product ? Sonarcloud
Which language ? PHP
Which rule ? S1144

At the moment, in Unit Tests, we are getting Unused “private” methods should be removed rule violations on private methods used with PHPDoc @dataProvider annotation.
PHPStorm does not report these methods as unused and is perfectly able to link the annotation value to the method implementation.

I believe it’s a false-positive because, obviously, the method is used when executing the Unit Test.

Here is a code snippet :

class S1444Test extends TestCase
{
    /**
     * @dataProvider valueProvider
     */
    public function testShouldCastValue(string $rawValue, int $castedValue) : void
    {
        // Given // When
        $result = (int) $rawValue;

        // Then
        assertSame($castedValue, $result);
    }

    private function valueProvider() : array
    {
        return [
            ['1', 1],
            ['2', 2],
            ['wrong', 0]
        ];
    }
}

Hello @Mathieu_Roullet!

Welcome to the community! Thanks for your reports and for the reproducer. This does indeed look like a false positive. Cases specific for PHPUnit are not yet covered by this rule. FWIW, the rule does respect the @uses tag in PHPDoc, so you could try this as a workaround.

I’ve created a ticket SONARPHP-1538 for this issue; we might tackle it in a future sprint on the PHP analyzer.

Best,

Peter