PHP private constant's are reported as unused when referenced in other consts only

We use a fresh install of SonarQube 8.4.1 with latest docker-based scanner on our php codebase.

With private constants that are only used as constant array key/value, the constants are reported as unused. I.e. this rule triggers for the two private constants below in the below snippet.

https://rules.sonarsource.com/php/RSPEC-1068

final class Vendor
{
    public const PURPOSES = [self::PURPOSE_FUNCTIONAL, self::PURPOSE_ANALYTICAL];

    private const PURPOSE_ANALYTICAL = 'analytical';
    private const PURPOSE_FUNCTIONAL = 'functional';

// more code that doesn't use those constants
}

We have similar pieces of code, with variants that have constant-based array-keys or where the array is protected rather than public.

Hello @Arjenm and welcome to the community,

Thanks for reaching us. We are looking into it and will get back to you soon.

Best, Nils

Hi @Arjenm,

We are happy to inform you that we have improved the rule and now the noise described in your example disappears. This improvement was released with SQ 8.6. We would love to hear your feedback.
(Here is the ticket: https://jira.sonarsource.com/browse/SONARPHP-1049)

Cheers,
Nils

Hi @Nils_Werner,

Thanks for mentioning me. It seems to have been fixed.

Cheers,
Arjen

Hi @Arjenm,

nice to hear.
Can you use my previous message as a solution for the thread so that it is closed?
Thank you very much.

Cheers,

Sure, marked it as solution :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.