Skip to content

Commit eb03f2b

Browse files
authored
enforceReadOnlyPublicProperty: fix false positive on PHP <= 8.0 (#87)
* enforceReadOnlyPublicProperty: fix false positive on PHP <= 8.0 * code-80 typo
1 parent c99e2b9 commit eb03f2b

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

src/Rule/EnforceReadonlyPublicPropertyRule.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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

1011
/**
@@ -13,6 +14,13 @@
1314
class EnforceReadonlyPublicPropertyRule implements Rule
1415
{
1516

17+
private PhpVersion $phpVersion;
18+
19+
public function __construct(PhpVersion $phpVersion)
20+
{
21+
$this->phpVersion = $phpVersion;
22+
}
23+
1624
public function getNodeType(): string
1725
{
1826
return ClassPropertyNode::class;
@@ -24,6 +32,10 @@ public function getNodeType(): string
2432
*/
2533
public function processNode(Node $node, Scope $scope): array
2634
{
35+
if (!$this->phpVersion->supportsReadOnlyProperties()) {
36+
return [];
37+
}
38+
2739
if (!$node->isPublic() || $node->isReadOnly()) {
2840
return [];
2941
}

tests/Rule/EnforceReadonlyPublicPropertyRuleTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,39 @@
22

33
namespace ShipMonk\PHPStan\Rule;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use ShipMonk\PHPStan\RuleTestCase;
7-
use const PHP_VERSION_ID;
88

99
/**
1010
* @extends RuleTestCase<EnforceReadonlyPublicPropertyRule>
1111
*/
1212
class EnforceReadonlyPublicPropertyRuleTest extends RuleTestCase
1313
{
1414

15+
private ?PhpVersion $phpVersion = null;
16+
1517
protected function getRule(): Rule
1618
{
17-
return new EnforceReadonlyPublicPropertyRule();
19+
self::assertNotNull($this->phpVersion);
20+
return new EnforceReadonlyPublicPropertyRule($this->phpVersion);
21+
}
22+
23+
public function testPhp81(): void
24+
{
25+
$this->phpVersion = $this->createPhpVersion(80_100);
26+
$this->analyseFile(__DIR__ . '/data/EnforceReadonlyPublicPropertyRule/code-81.php');
1827
}
1928

20-
public function test(): void
29+
public function testPhp80(): void
2130
{
22-
if (PHP_VERSION_ID < 80_100) {
23-
self::markTestSkipped('Requires PHP 8.1');
24-
}
31+
$this->phpVersion = $this->createPhpVersion(80_000);
32+
$this->analyseFile(__DIR__ . '/data/EnforceReadonlyPublicPropertyRule/code-80.php');
33+
}
2534

26-
$this->analyseFile(__DIR__ . '/data/EnforceReadonlyPublicPropertyRule/code.php');
35+
private function createPhpVersion(int $version): PhpVersion
36+
{
37+
return new PhpVersion($version); // @phpstan-ignore-line ignore bc promise
2738
}
2839

2940
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace EnforceReadonlyPublicPropertyRule80;
4+
5+
trait MyTrait {
6+
7+
public ?string $public;
8+
9+
protected string $protected;
10+
11+
private string $private;
12+
13+
}
14+
15+
class MyClass {
16+
17+
use MyTrait;
18+
19+
public ?int $foo;
20+
21+
public int $bar;
22+
23+
protected int $baz;
24+
25+
private int $bag;
26+
27+
}
28+
29+

tests/Rule/data/EnforceReadonlyPublicPropertyRule/code.php renamed to tests/Rule/data/EnforceReadonlyPublicPropertyRule/code-81.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace EnforceReadonlyPublicPropertyRule;
3+
namespace EnforceReadonlyPublicPropertyRule81;
44

55
trait MyTrait {
66

0 commit comments

Comments
 (0)