@@ -34,19 +34,19 @@ public function testFix(string $expected, ?string $input = null): void
3434 }
3535
3636 /**
37- * @return iterable<list< string> >
37+ * @return iterable<array{ string, 1?: string} >
3838 */
3939 public static function provideFixCases (): iterable
4040 {
41- yield [
41+ yield ' not promoted property ' => [
4242 '<?php class Foo {
4343 public function __construct(
4444 int $x
4545 ) {}
4646 } ' ,
4747 ];
4848
49- yield [
49+ yield ' multiple promoted properties ' => [
5050 '<?php class Foo {
5151 public function __construct(
5252 public readonly int $a,
@@ -63,7 +63,7 @@ public function __construct(
6363 } ' ,
6464 ];
6565
66- yield [
66+ yield ' already readonly properties ' => [
6767 '<?php class Foo {
6868 public function __construct(
6969 public readonly int $a,
@@ -88,7 +88,7 @@ public function __construct(
8888 } ' ,
8989 ];
9090
91- yield [
91+ yield ' not in constructor ' => [
9292 '<?php
9393 class Foo { public function __construct(public readonly int $x) {} }
9494 class Bar { public function notConstruct(int $x) {} }
@@ -101,14 +101,52 @@ class Baz { public function __construct(public int $x) {} }
101101 ' ,
102102 ];
103103
104- yield [
105- '<?php class Foo {
106- public function __construct(public int $x) {}
107- public function doSomething() { $this->x = 42; }
108- } ' ,
104+ yield 'property used in assignment ' => [
105+ <<<'PHP'
106+ <?php class Foo {
107+ public function __construct(
108+ public int $p1,
109+ public int $p2,
110+ public int $p3,
111+ public int $p4,
112+ public int $p5,
113+ public int $p6,
114+ public int $p7,
115+ public int $p8,
116+ public int $p9,
117+ public bool $p10,
118+ public bool $p11,
119+ public bool $p12,
120+ public int $p13,
121+ public int $p14,
122+ public int $p15,
123+ public string $p16,
124+ public array $p17,
125+ ) {}
126+ public function f() {
127+ $this->p1 = 1;
128+ $this->p2++;
129+ $this->p3--;
130+ $this->p4 += 4;
131+ $this->p5 -= 5;
132+ $this->p6 *= 6;
133+ $this->p7 /= 7;
134+ $this->p8 %= 8;
135+ $this->p9 **= 9;
136+ $this->p10 &= true;
137+ $this->p11 |= true;
138+ $this->p12 ^= true;
139+ $this->p13 <<= 1;
140+ $this->p14 >>= 1;
141+ $this->p15 ??= 15;
142+ $this->p16 .= '16';
143+ $this->p17[0][0][0][0][0][0][0][0] = 17;
144+ }
145+ }
146+ PHP,
109147 ];
110148
111- yield [
149+ yield ' property of other object used in assignment ' => [
112150 '<?php class Foo {
113151 public function __construct(public readonly int $x) {}
114152 public function doSomething() { $object->x = 42; }
@@ -119,7 +157,7 @@ public function doSomething() { $object->x = 42; }
119157 } ' ,
120158 ];
121159
122- yield [
160+ yield ' multiple properties with assignments ' => [
123161 '<?php class Foo {
124162 public function __construct(public int $x, public readonly int $y, public int $z) {}
125163 public function doSomething() { $this->x = 42; $this->z = 10; }
@@ -129,6 +167,149 @@ public function __construct(public int $x, public int $y, public int $z) {}
129167 public function doSomething() { $this->x = 42; $this->z = 10; }
130168 } ' ,
131169 ];
170+
171+ yield 'property used in return statement ' => [
172+ <<<'PHP'
173+ <?php class Foo {
174+ public function __construct(public readonly object $p) {}
175+ public function f() {
176+ return $this->p;
177+ }
178+ }
179+ PHP,
180+ <<<'PHP'
181+ <?php class Foo {
182+ public function __construct(public object $p) {}
183+ public function f() {
184+ return $this->p;
185+ }
186+ }
187+ PHP,
188+ ];
189+
190+ yield 'property used as argument ' => [
191+ <<<'PHP'
192+ <?php class Foo {
193+ public function __construct(
194+ public readonly array $array,
195+ public readonly object $object,
196+ ) {}
197+ public function f() {
198+ bar($this->array[4], $this->object);
199+ baz(1, $this->array, 2);
200+ baz(3, $this->object, 4);
201+ }
202+ }
203+ PHP,
204+ <<<'PHP'
205+ <?php class Foo {
206+ public function __construct(
207+ public array $array,
208+ public object $object,
209+ ) {}
210+ public function f() {
211+ bar($this->array[4], $this->object);
212+ baz(1, $this->array, 2);
213+ baz(3, $this->object, 4);
214+ }
215+ }
216+ PHP,
217+ ];
218+
219+ yield 'property used in method call ' => [
220+ <<<'PHP'
221+ <?php class Foo {
222+ public function __construct(public readonly object $p) {}
223+ public function f() {
224+ $this->p->method();
225+ }
226+ }
227+ PHP,
228+ <<<'PHP'
229+ <?php class Foo {
230+ public function __construct(public object $p) {}
231+ public function f() {
232+ $this->p->method();
233+ }
234+ }
235+ PHP,
236+ ];
237+
238+ yield 'property used in null-safe method call ' => [
239+ <<<'PHP'
240+ <?php class Foo {
241+ public function __construct(public readonly object $p) {}
242+ public function f() {
243+ $this->p?->method();
244+ }
245+ }
246+ PHP,
247+ <<<'PHP'
248+ <?php class Foo {
249+ public function __construct(public object $p) {}
250+ public function f() {
251+ $this->p?->method();
252+ }
253+ }
254+ PHP,
255+ ];
256+
257+ yield 'property of property ' => [
258+ <<<'PHP'
259+ <?php class Foo {
260+ public function __construct(public readonly object $object) {}
261+ public function f() {
262+ $this->object->property = 3;
263+ }
264+ }
265+ PHP,
266+ <<<'PHP'
267+ <?php class Foo {
268+ public function __construct(public object $object) {}
269+ public function f() {
270+ $this->object->property = 3;
271+ }
272+ }
273+ PHP,
274+ ];
275+
276+ yield 'property used multiple times ' => [
277+ <<<'PHP'
278+ <?php class Foo {
279+ public function __construct(public readonly object $p) {}
280+ public function f() {
281+ $this->value1 = $this->p->method1();
282+ bar($this->p, $this->p->property1);
283+ $this->value2 = $this->p->property2;
284+ $this->p->method2();
285+ }
286+ }
287+ PHP,
288+ <<<'PHP'
289+ <?php class Foo {
290+ public function __construct(public object $p) {}
291+ public function f() {
292+ $this->value1 = $this->p->method1();
293+ bar($this->p, $this->p->property1);
294+ $this->value2 = $this->p->property2;
295+ $this->p->method2();
296+ }
297+ }
298+ PHP,
299+ ];
300+
301+ yield 'property used multiple times, including assignment ' => [
302+ <<<'PHP'
303+ <?php class Foo {
304+ public function __construct(public object $p) {}
305+ public function f() {
306+ $this->p->method1();
307+ $this->p = new Bar();
308+ $this->p->method2();
309+ }
310+ }
311+ PHP,
312+ ];
132313 }
133314
134315 /**
0 commit comments