llaville / php-compatinfo

Library that find out the minimum version and the extensions required for a piece of code to run
https://llaville.github.io/php-compatinfo/7.1/
Other
371 stars 21 forks source link

Constructor `readonly` properties not detected as a PHP 8.1 feature #374

Closed llaville closed 4 months ago

llaville commented 4 months ago

Bug report category

Summary

Constructor readonly properties not detected as a PHP 8.1 feature. Learn more with https://php.watch/versions/8.1/readonly#constructor-readonly

Expected behaviour

Detect constructor readonly properties with Bartlett\CompatInfo\Application\Sniffs\Classes\ReadonlyPropertySniff

Actual behaviour

Only class property in AST (with Node\Stmt\Property) is analysed. Should also analyse in AST (the param list of Node\FunctionLike)

llaville commented 4 months ago

Tests with https://github.com/llaville/php-compatinfo/releases/tag/7.1.3 on following source code.

<?php
class Test {
    public function __construct(public readonly string $prop) {
    }
}

analyser:run command returns with console reporter

[OK] Requires PHP 8.0.0alpha1 (min)

and on details with rule reporter

Stats
-----

 PHP 70 :   1 rule(s) found
 PHP 80 :   1 rule(s) found

Rules
-----

         code : CA7008
  description : Parameters Type Declaration 'string' is available since PHP 7.0.0
        sniff : ParamTypeDeclarationSniff
      methods : (1)
 Test\__construct

         code : CA8003
  description : Constructor property promotion is available since PHP 8.0.0
        sniff : PropertyPromotionSniff
      methods : (1)
 Test\__construct

While CA8102 rule should also be detected

llaville commented 4 months ago

Verifications are now done. Closing this report