Skip to content

Commit e5de6b8

Browse files
committed
InvalidPromotedPropertiesRule - report final in promoted property on PHP < 8.5
1 parent 6afd608 commit e5de6b8

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/Php/PhpVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,9 @@ public function deprecatesNullArrayOffset(): bool
424424
return $this->versionId >= 80500;
425425
}
426426

427+
public function supportsFinalPromotedProperties(): bool
428+
{
429+
return $this->versionId >= 80500;
430+
}
431+
427432
}

src/Rules/Classes/InvalidPromotedPropertiesRule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public function processNode(Node $node, Scope $scope): array
8989
throw new ShouldNotHappenException();
9090
}
9191

92+
if ($param->isFinal() && !$this->phpVersion->supportsFinalPromotedProperties()) {
93+
$errors[] = RuleErrorBuilder::message(
94+
'Final promoted properties are supported only on PHP 8.5 and later.',
95+
)->identifier('property.invalidPromoted')->nonIgnorable()->line($param->getStartLine())->build();
96+
}
97+
9298
if (!$param->variadic) {
9399
continue;
94100
}

tests/PHPStan/Rules/Classes/InvalidPromotedPropertiesRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
88
use PHPUnit\Framework\Attributes\RequiresPhp;
9+
use const PHP_VERSION_ID;
910

1011
/**
1112
* @extends RuleTestCase<InvalidPromotedPropertiesRule>
@@ -113,4 +114,19 @@ public function testHooks(): void
113114
]);
114115
}
115116

117+
public function testFinalProperty(): void
118+
{
119+
$this->phpVersion = PHP_VERSION_ID;
120+
$errors = [];
121+
if (PHP_VERSION_ID < 80500) {
122+
$errors = [
123+
[
124+
'Final promoted properties are supported only on PHP 8.5 and later.',
125+
8,
126+
],
127+
];
128+
}
129+
$this->analyse([__DIR__ . '/data/final-promoted-property.php'], $errors);
130+
}
131+
116132
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php // lint >= 8.5
2+
3+
namespace FinalPromotedProperty;
4+
5+
class Foo
6+
{
7+
8+
public function __construct(final $i) {
9+
10+
}
11+
12+
}

0 commit comments

Comments
 (0)