File tree 3 files changed +64
-1
lines changed
tests/PHPStan/Rules/Properties
3 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -1238,7 +1238,7 @@ public function isImmutable(): bool
1238
1238
{
1239
1239
if ($ this ->isImmutable === null ) {
1240
1240
$ resolvedPhpDoc = $ this ->getResolvedPhpDoc ();
1241
- $ this ->isImmutable = $ resolvedPhpDoc !== null && $ resolvedPhpDoc ->isImmutable ();
1241
+ $ this ->isImmutable = $ resolvedPhpDoc !== null && ( $ resolvedPhpDoc ->isImmutable () || $ resolvedPhpDoc -> isReadOnly () );
1242
1242
1243
1243
$ parentClass = $ this ->getParentClass ();
1244
1244
if ($ parentClass !== null && !$ this ->isImmutable ) {
Original file line number Diff line number Diff line change @@ -160,4 +160,22 @@ public function testFeature7648(): void
160
160
$ this ->analyse ([__DIR__ . '/data/feature-7648.php ' ], []);
161
161
}
162
162
163
+ public function testFeature11775 (): void
164
+ {
165
+ if (PHP_VERSION_ID < 70400 ) {
166
+ $ this ->markTestSkipped ('Test requires PHP 7.4. ' );
167
+ }
168
+
169
+ $ this ->analyse ([__DIR__ . '/data/feature-11775.php ' ], [
170
+ [
171
+ '@readonly property Feature11775\FooImmutable::$i is assigned outside of the constructor. ' ,
172
+ 22 ,
173
+ ],
174
+ [
175
+ '@readonly property Feature11775\FooReadonly::$i is assigned outside of the constructor. ' ,
176
+ 43 ,
177
+ ],
178
+ ]);
179
+ }
180
+
163
181
}
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 ); // lint >= 7.4
2
+
3
+ namespace Feature11775 ;
4
+
5
+ /** @immutable */
6
+ class FooImmutable
7
+ {
8
+ private int $ i ;
9
+
10
+ public function __construct (int $ i )
11
+ {
12
+ $ this ->i = $ i ;
13
+ }
14
+
15
+ public function getId (): int
16
+ {
17
+ return $ this ->i ;
18
+ }
19
+
20
+ public function setId (): void
21
+ {
22
+ $ this ->i = 5 ;
23
+ }
24
+ }
25
+
26
+ /** @readonly */
27
+ class FooReadonly
28
+ {
29
+ private int $ i ;
30
+
31
+ public function __construct (int $ i )
32
+ {
33
+ $ this ->i = $ i ;
34
+ }
35
+
36
+ public function getId (): int
37
+ {
38
+ return $ this ->i ;
39
+ }
40
+
41
+ public function setId (): void
42
+ {
43
+ $ this ->i = 5 ;
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments