onigoetz/php-constructor-promotion

★ 0Forks 0PHPGitHub ↗Compare

README

PHP S1172 false positive — readonly-only constructor promotion

Minimal reproduction for the SonarSource community report: https://community.sonarsource.com/t/false-positive-php-s1172-on-constructor-parameter-promoted-with-readonly-only-no-visibility-modif/182021

The bug

PHP 8.4 allows constructor property promotion using readonly alone, without an explicit visibility modifier:

class Foo {
    // PHP 8.4: readonly without public/protected/private still promotes $name to a property
    public function __construct(readonly string $name) {}
}

SonarQube / SonarCloud raises S1172 ("Remove this unused method parameter $name") on such parameters even though they are promoted properties and are not unused.

The same class written with an explicit visibility modifier (public readonly) does not trigger the false positive.

Files

File Description
src/WithVisibility.php public readonly — no S1172 (correct)
src/WithoutVisibility.php readonly only — false positive S1172
src/MixedArgs.php Mix of both styles — false positives on readonly-only parameters

Expected behaviour

S1172 should not be raised on any constructor parameter that carries the readonly modifier, since readonly alone is a valid promotion modifier in PHP 8.4.

Actual behaviour

S1172 is raised on every readonly-only promoted parameter.

Environment

  • PHP 8.4
  • SonarQube / SonarCloud with the PHP analyser