Skip to content

Commit

Permalink
fix issue #374
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Apr 23, 2024
1 parent 69125a8 commit 24708a6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changes/7.x/unreleased/Fixed-20240423-145831.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Constructor readonly properties not detected as a PHP 8.1 feature
time: 2024-04-23T14:58:31.032104573Z
19 changes: 15 additions & 4 deletions src/Application/Sniffs/Classes/ReadonlyPropertySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @link https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties
* @link https://wiki.php.net/rfc/readonly_properties_v2
* @link https://php.watch/versions/8.1/readonly
* @see tests/Sniffs/ReadonlyPropertySniffTest.php
*/
final class ReadonlyPropertySniff extends SniffAbstract
Expand All @@ -42,14 +43,24 @@ public function getRules(): Generator
*/
public function enterNode(Node $node): int|Node|null
{
if (!$node instanceof Node\Stmt\Property) {
if ($node instanceof Node\Stmt\Property) {
if ($node->flags & Node\Stmt\Class_::MODIFIER_READONLY) {
$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '8.1.0beta1']);
$this->updateNodeElementRule($node, $this->attributeKeyStore, self::CA81);
}
return null;
}

if ($node->flags & Node\Stmt\Class_::MODIFIER_READONLY) {
$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '8.1.0beta1']);
$this->updateNodeElementRule($node, $this->attributeKeyStore, self::CA81);
if ($node instanceof Node\FunctionLike) {
foreach ($node->getParams() as $param) {
if ($param->isReadonly()) {
$this->updateNodeElementVersion($node, $this->attributeKeyStore, ['php.min' => '8.1.0beta1']);
$this->updateNodeElementRule($node, $this->attributeKeyStore, self::CA81);
break;
}
}
}

return null;
}
}
25 changes: 25 additions & 0 deletions tests/Sniffs/ReadonlyPropertySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,29 @@ public function testReadonlyProperties(): void
$versions['php.max']
);
}

/**
* Feature test for Constructor Readonly properties
*
* @link https://github.com/llaville/php-compatinfo/issues/374
* Constructor Readonly Properties are detected as PHP 8.1
* @group features
* @throws Exception
*/
public function testConstructorReadonlyProperties(): void
{
$dataSource = 'constructor_readonly_properties.php';
$metrics = $this->executeAnalysis($dataSource);
$versions = $metrics[self::$analyserId]['versions'];

$this->assertEquals(
'8.1.0beta1',
$versions['php.min']
);

$this->assertEquals(
'',
$versions['php.max']
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class Test {
public function __construct(public readonly string $prop) {
}
}

0 comments on commit 24708a6

Please sign in to comment.