Web:S7930 - Flags Conditional IDs as Duplicates

Language: PHP (PHTML)

Rule: Web:S7930 - HTML elements should have unique “id” attribute values

Why do you believe it’s a false positive?

The rule reports duplicate id attributes when the same id is used in mutually exclusive if/else branches within a PHTML template.

In the example below, Sonar reports test-id as a duplicate ID. However, only one branch can be rendered at runtime, so the generated HTML can never contain both elements simultaneously. Since duplicate IDs can only exist if both elements are present in the final HTML, this appears to be a false positive.

Product:

  • SonarQube Server / Community Build (or SonarQube Cloud, as applicable)

How can we reproduce the problem?

Analyze the following PHTML file:

<?php if (random_int(0, 1)) { ?>
    <input type="text" id="test-id" name="customer_id" readonly />
<?php } else { ?>
    <input type="text" id="test-id" name="customer_id" readonly />
<?php } ?>

Expected behavior

No issue should be raised because only one <input> element can be present in the rendered HTML.

Actual behavior

Web:S7930 reports a duplicate id value (test-id) even though the two elements are in mutually exclusive branches and cannot coexist in the final output.