Skip to content

Commit d3f2590

Browse files
authored
ReadonlyPromotedPropertiesFixer - do not make poperty readonly if it has an assignment in class (#914)
1 parent 1d9ba55 commit d3f2590

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
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-3451-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3454-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/Fixer/ReadonlyPromotedPropertiesFixer.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,21 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
8383
continue;
8484
}
8585

86-
$openParenthesis = $tokens->getNextTokenOfKind($constructorAnalysis->getConstructorIndex(), ['(']);
87-
\assert(\is_int($openParenthesis));
88-
$closeParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis);
89-
90-
$this->fixParameters($tokens, $openParenthesis, $closeParenthesis);
86+
$classOpenBraceIndex = $tokens->getNextTokenOfKind($index, ['{']);
87+
\assert(\is_int($classOpenBraceIndex));
88+
$classCloseBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classOpenBraceIndex);
89+
90+
$constructorOpenParenthesisIndex = $tokens->getNextTokenOfKind($constructorAnalysis->getConstructorIndex(), ['(']);
91+
\assert(\is_int($constructorOpenParenthesisIndex));
92+
$constructorCloseParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $constructorOpenParenthesisIndex);
93+
94+
$this->fixParameters(
95+
$tokens,
96+
$classOpenBraceIndex,
97+
$classCloseBraceIndex,
98+
$constructorOpenParenthesisIndex,
99+
$constructorCloseParenthesisIndex,
100+
);
91101
}
92102
}
93103

@@ -101,9 +111,14 @@ private function isClassReadonly(Tokens $tokens, int $index): bool
101111
return $tokens[$index]->isGivenKind(\T_READONLY);
102112
}
103113

104-
private function fixParameters(Tokens $tokens, int $openParenthesis, int $closeParenthesis): void
105-
{
106-
for ($index = $closeParenthesis; $index > $openParenthesis; $index--) {
114+
private function fixParameters(
115+
Tokens $tokens,
116+
int $classOpenBraceIndex,
117+
int $classCloseBraceIndex,
118+
int $constructorOpenParenthesisIndex,
119+
int $constructorCloseParenthesisIndex
120+
): void {
121+
for ($index = $constructorCloseParenthesisIndex; $index > $constructorOpenParenthesisIndex; $index--) {
107122
if (
108123
!$tokens[$index]->isGivenKind([
109124
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
@@ -124,6 +139,22 @@ private function fixParameters(Tokens $tokens, int $openParenthesis, int $closeP
124139
continue;
125140
}
126141

142+
$propertyIndex = $tokens->getNextTokenOfKind($index, [[\T_VARIABLE]]);
143+
\assert(\is_int($propertyIndex));
144+
145+
$propertyAssignment = $tokens->findSequence(
146+
[
147+
[\T_VARIABLE, '$this'],
148+
[\T_OBJECT_OPERATOR],
149+
[\T_STRING, \substr($tokens[$propertyIndex]->getContent(), 1)],
150+
],
151+
$classOpenBraceIndex,
152+
$classCloseBraceIndex,
153+
);
154+
if ($propertyAssignment !== null) {
155+
continue;
156+
}
157+
127158
$tokens->insertAt(
128159
$index + 1,
129160
[

tests/Fixer/ReadonlyPromotedPropertiesFixerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ class Bar { public function notConstruct(int $x) {} }
100100
class Baz { public function __construct(public int $x) {} }
101101
',
102102
];
103+
104+
yield [
105+
'<?php class Foo {
106+
public function __construct(public int $x) {}
107+
public function doSomething() { $this->x = 42; }
108+
}',
109+
];
110+
111+
yield [
112+
'<?php class Foo {
113+
public function __construct(public readonly int $x) {}
114+
public function doSomething() { $object->x = 42; }
115+
}',
116+
'<?php class Foo {
117+
public function __construct(public int $x) {}
118+
public function doSomething() { $object->x = 42; }
119+
}',
120+
];
121+
122+
yield [
123+
'<?php class Foo {
124+
public function __construct(public int $x, public readonly int $y, public int $z) {}
125+
public function doSomething() { $this->x = 42; $this->z = 10; }
126+
}',
127+
'<?php class Foo {
128+
public function __construct(public int $x, public int $y, public int $z) {}
129+
public function doSomething() { $this->x = 42; $this->z = 10; }
130+
}',
131+
];
103132
}
104133

105134
/**

0 commit comments

Comments
 (0)