Skip to content

Commit 14aafab

Browse files
committed
Prevent reporting property.inInterface error when PHP version is 8.4 or later
1 parent c0bfae6 commit 14aafab

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/Rules/Properties/PropertiesInInterfaceRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
final class PropertiesInInterfaceRule implements Rule
1515
{
16+
private const PHP_8_4_VERSION_ID = 80400;
1617

1718
public function getNodeType(): string
1819
{
@@ -25,6 +26,10 @@ public function processNode(Node $node, Scope $scope): array
2526
return [];
2627
}
2728

29+
if (PHP_VERSION_ID >= self::PHP_8_4_VERSION_ID) {
30+
return [];
31+
}
32+
2833
return [
2934
RuleErrorBuilder::message('Interfaces may not include properties.')
3035
->nonIgnorable()

tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Properties;
44

5+
use PHPStan\Analyser\Error;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78

@@ -10,6 +11,7 @@
1011
*/
1112
class PropertiesInInterfaceRuleTest extends RuleTestCase
1213
{
14+
private const PHP_8_4_VERSION_ID = 80400;
1315

1416
protected function getRule(): Rule
1517
{
@@ -30,4 +32,36 @@ public function testRule(): void
3032
]);
3133
}
3234

35+
public function testIgnoringPropertyInInterfaceOnPHPBelow84(): void
36+
{
37+
if (PHP_VERSION_ID >= self::PHP_8_4_VERSION_ID) {
38+
$this->markTestSkipped('Test is valid only on PHP 8.3 or earlier');
39+
}
40+
41+
$errors = $this->gatherAnalyserErrors([__DIR__ . '/data/properties-in-interface-ignore-property-in-interface.php']);
42+
43+
/** @var array<Error> $propertyInInterfaceErrors */
44+
$propertyInInterfaceErrors = array_filter($errors, static function (Error $error) {
45+
return $error->getIdentifier() === 'property.inInterface';
46+
});
47+
48+
$this->assertCount(1, $propertyInInterfaceErrors);
49+
$this->assertFalse($propertyInInterfaceErrors[0]->canBeIgnored());
50+
}
51+
52+
public function testIgnoringPropertyInInterfaceOnPHP84OrAbove(): void
53+
{
54+
if (PHP_VERSION_ID < self::PHP_8_4_VERSION_ID) {
55+
$this->markTestSkipped('Test is valid only on PHP 8.4 or later');
56+
}
57+
58+
$errors = $this->gatherAnalyserErrors([__DIR__ . '/data/properties-in-interface-ignore-property-in-interface.php']);
59+
60+
/** @var array<Error> $propertyInInterfaceErrors */
61+
$propertyInInterfaceErrors = array_filter($errors, static function (Error $error) {
62+
return $error->getIdentifier() === 'property.inInterface';
63+
});
64+
65+
$this->assertCount(0, $propertyInInterfaceErrors);
66+
}
3367
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Rules\Properties\data;
4+
5+
interface HelloWorld
6+
{
7+
public string $name;
8+
}

0 commit comments

Comments
 (0)