Skip to content

Commit c263971

Browse files
committed
Introduce PhpVersion object for checking if current PHP version supports hooks in interfaces
1 parent 5196b92 commit c263971

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ private function processStmtNode(
899899
}
900900
$propStmt = clone $stmt;
901901
$propStmt->setAttributes($prop->getAttributes());
902+
rd($propStmt);
902903
$nodeCallback(
903904
new ClassPropertyNode(
904905
$propertyName,

src/Php/PhpVersion.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,11 @@ public function supportsPregCaptureOnlyNamedGroups(): bool
347347
return $this->versionId >= 80200;
348348
}
349349

350+
public function supportsPublicPropertiesInInterfaces(): bool
351+
{
352+
return $this->versionId >= 80400;
353+
}
354+
350355
public function hasDateTimeExceptions(): bool
351356
{
352357
return $this->versionId >= 80300;
@@ -374,5 +379,4 @@ public function substrReturnFalseInsteadOfEmptyString(): bool
374379
{
375380
return $this->versionId < 80000;
376381
}
377-
378382
}

src/Rules/Properties/PropertiesInInterfaceRule.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\ClassPropertyNode;
8+
use PHPStan\Php\PhpVersion;
89
use PHPStan\Rules\Rule;
910
use PHPStan\Rules\RuleErrorBuilder;
10-
use const PHP_VERSION_ID;
1111

1212
/**
1313
* @implements Rule<ClassPropertyNode>
1414
*/
1515
final class PropertiesInInterfaceRule implements Rule
1616
{
1717

18-
private const PHP_8_4_VERSION_ID = 80400;
18+
public function __construct (private PhpVersion $phpVersion)
19+
{
20+
}
1921

2022
public function getNodeType(): string
2123
{
@@ -28,7 +30,7 @@ public function processNode(Node $node, Scope $scope): array
2830
return [];
2931
}
3032

31-
if (PHP_VERSION_ID >= self::PHP_8_4_VERSION_ID) {
33+
if ($node->isPublic() && $this->phpVersion->supportsPublicPropertiesInInterfaces()) {
3234
return [];
3335
}
3436

tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php

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

33
namespace PHPStan\Rules\Properties;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78
use const PHP_VERSION_ID;
@@ -12,16 +13,21 @@
1213
class PropertiesInInterfaceRuleTest extends RuleTestCase
1314
{
1415

15-
private const PHP_8_4_VERSION_ID = 80400;
16+
private PhpVersion $phpVersion;
17+
18+
protected function setUp(): void
19+
{
20+
$this->phpVersion = new PhpVersion(PHP_VERSION_ID);
21+
}
1622

1723
protected function getRule(): Rule
1824
{
19-
return new PropertiesInInterfaceRule();
25+
return new PropertiesInInterfaceRule(new PhpVersion(PHP_VERSION_ID));
2026
}
2127

2228
public function testRule(): void
2329
{
24-
if (PHP_VERSION_ID >= self::PHP_8_4_VERSION_ID) {
30+
if ($this->phpVersion->supportsPublicPropertiesInInterfaces()) {
2531
$this->analyse([__DIR__ . '/data/properties-in-interface.php'], []);
2632

2733
return;

0 commit comments

Comments
 (0)