File tree Expand file tree Collapse file tree 4 files changed +60
-8
lines changed
data/EnforceReadonlyPublicPropertyRule Expand file tree Collapse file tree 4 files changed +60
-8
lines changed Original file line number Diff line number Diff line change 5
5
use PhpParser \Node ;
6
6
use PHPStan \Analyser \Scope ;
7
7
use PHPStan \Node \ClassPropertyNode ;
8
+ use PHPStan \Php \PhpVersion ;
8
9
use PHPStan \Rules \Rule ;
9
10
10
11
/**
13
14
class EnforceReadonlyPublicPropertyRule implements Rule
14
15
{
15
16
17
+ private PhpVersion $ phpVersion ;
18
+
19
+ public function __construct (PhpVersion $ phpVersion )
20
+ {
21
+ $ this ->phpVersion = $ phpVersion ;
22
+ }
23
+
16
24
public function getNodeType (): string
17
25
{
18
26
return ClassPropertyNode::class;
@@ -24,6 +32,10 @@ public function getNodeType(): string
24
32
*/
25
33
public function processNode (Node $ node , Scope $ scope ): array
26
34
{
35
+ if (!$ this ->phpVersion ->supportsReadOnlyProperties ()) {
36
+ return [];
37
+ }
38
+
27
39
if (!$ node ->isPublic () || $ node ->isReadOnly ()) {
28
40
return [];
29
41
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace ShipMonk \PHPStan \Rule ;
4
4
5
+ use PHPStan \Php \PhpVersion ;
5
6
use PHPStan \Rules \Rule ;
6
7
use ShipMonk \PHPStan \RuleTestCase ;
7
- use const PHP_VERSION_ID ;
8
8
9
9
/**
10
10
* @extends RuleTestCase<EnforceReadonlyPublicPropertyRule>
11
11
*/
12
12
class EnforceReadonlyPublicPropertyRuleTest extends RuleTestCase
13
13
{
14
14
15
+ private ?PhpVersion $ phpVersion = null ;
16
+
15
17
protected function getRule (): Rule
16
18
{
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 ' );
18
27
}
19
28
20
- public function test (): void
29
+ public function testPhp80 (): void
21
30
{
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
+ }
25
34
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
27
38
}
28
39
29
40
}
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace EnforceReadonlyPublicPropertyRule ;
3
+ namespace EnforceReadonlyPublicPropertyRule81 ;
4
4
5
5
trait MyTrait {
6
6
You can’t perform that action at this time.
0 commit comments