Skip to content

Commit a5d2ce1

Browse files
authored
PromotedConstructorPropertyFixer - do not crash on variable property (#733)
1 parent 90338e0 commit a5d2ce1

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3364-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3366-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

src/Analyzer/Analysis/ConstructorAnalysis.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ private function getPropertyIndex(int $index, int $openBrace): ?int
136136
}
137137

138138
$propertyIndex = $this->tokens->getPrevMeaningfulToken($assignmentIndex);
139+
if (!$this->tokens[$propertyIndex]->isGivenKind(\T_STRING)) {
140+
return null;
141+
}
139142
\assert(\is_int($propertyIndex));
140143

141144
$objectOperatorIndex = $this->tokens->getPrevMeaningfulToken($propertyIndex);

tests/Analyzer/Analysis/ConstructorAnalysisTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,14 @@ public function __construct($x, $y, $z) {
149149
}
150150
}',
151151
];
152+
153+
yield 'variable property assignment' => [
154+
[],
155+
'<?php class Foo {
156+
public function __construct($x) {
157+
$this->$x = $x;
158+
}
159+
}',
160+
];
152161
}
153162
}

tests/Fixer/PromotedConstructorPropertyFixerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ public function __construct(string $x, string $y)
131131
}',
132132
];
133133

134+
yield 'do not promote variable property' => [
135+
'<?php class Foo {
136+
private string $bar;
137+
public function __construct(string $bar) {
138+
$this->$bar = $bar;
139+
}
140+
}
141+
',
142+
];
143+
134144
yield 'var keywords are not promoted' => [
135145
'<?php class Foo {
136146
public function __construct(public int $x) {

0 commit comments

Comments
 (0)