SonarQube PHP duplication detection and dependency injection constructors

Hello,

  • developper edition 9.9.8 server
  • how is SonarQube deployed : Docket

I am currently analyzing a PHP/Symfony project with a relatively high code duplication rate reported by SonarQube.

While investigating the duplicated blocks, I noticed that SonarQube appears to consider identical dependency injection constructors as duplicated code.

For example, we have several controllers with constructors like this:

final class ModifierMoyenCommunicationInlineController extends AbstractController
{
    public function __construct(
        private readonly PersonneService $personneService,
        private readonly LoggerInterface $logger,
    ) {
    }

    // Controller-specific logic...
}

Questions:

  • Is it expected that identical PHP constructors are counted as duplicated code by the duplication engine?
  • Is there a recommended configuration or exclusion mechanism to prevent dependency injection constructors from contributing to duplication metrics?

Thank you.

Hey @olivier252, I don’t think there’s any specific condition for constructors in terms of duplicated code calculation. However, this short constructor alone is unlikely to meet the default threshold of 100 consecutive duplicated tokens across 10 lines, so either the reported duplication includes a larger repeated region or you have configured a different threshold.

Regarding configuration recommendations:

By the way, you’re running a really old version (9.9.8) which is way past its EOL. You should update it.