Skip to content

Commit 1d9ba55

Browse files
authored
ReadonlyPromotedPropertiesFixer - do not make readonly properies if class is already readonly (#913)
1 parent 5f06d9d commit 1d9ba55

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-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-3445-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3451-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7474
continue;
7575
}
7676

77+
if ($this->isClassReadonly($tokens, $index)) {
78+
continue;
79+
}
80+
7781
$constructorAnalysis = $constructorAnalyzer->findNonAbstractConstructor($tokens, $index);
7882
if ($constructorAnalysis === null) {
7983
continue;
@@ -87,6 +91,16 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
8791
}
8892
}
8993

94+
private function isClassReadonly(Tokens $tokens, int $index): bool
95+
{
96+
do {
97+
$index = $tokens->getPrevMeaningfulToken($index);
98+
\assert(\is_int($index));
99+
} while ($tokens[$index]->isGivenKind([\T_ABSTRACT, \T_FINAL]));
100+
101+
return $tokens[$index]->isGivenKind(\T_READONLY);
102+
}
103+
90104
private function fixParameters(Tokens $tokens, int $openParenthesis, int $closeParenthesis): void
91105
{
92106
for ($index = $closeParenthesis; $index > $openParenthesis; $index--) {

tests/Fixer/ReadonlyPromotedPropertiesFixerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function __construct(
8787
) {}
8888
}',
8989
];
90+
9091
yield [
9192
'<?php
9293
class Foo { public function __construct(public readonly int $x) {} }
@@ -100,4 +101,47 @@ class Baz { public function __construct(public int $x) {} }
100101
',
101102
];
102103
}
104+
105+
/**
106+
* @dataProvider provideFix82Cases
107+
*
108+
* @requires PHP 8.2
109+
*/
110+
public function testFix82(string $expected, ?string $input = null): void
111+
{
112+
$this->doTest($expected, $input);
113+
}
114+
115+
/**
116+
* @return iterable<array<string>>
117+
*/
118+
public static function provideFix82Cases(): iterable
119+
{
120+
$template = '<?php %s class C1 { public function __construct(public int $x) {} }';
121+
122+
foreach (
123+
[
124+
'readonly',
125+
'abstract readonly',
126+
'final readonly',
127+
'readonly final',
128+
'readonly abstract',
129+
] as $classModifiers
130+
) {
131+
yield [\sprintf($template, $classModifiers)];
132+
}
133+
134+
yield [
135+
'<?php
136+
class Foo { public function __construct(public readonly int $x) {} }
137+
readonly class Bar { public function __construct(int $x) {} }
138+
class Baz { public function __construct(public readonly int $x) {} }
139+
',
140+
'<?php
141+
class Foo { public function __construct(public int $x) {} }
142+
readonly class Bar { public function __construct(int $x) {} }
143+
class Baz { public function __construct(public int $x) {} }
144+
',
145+
];
146+
}
103147
}

0 commit comments

Comments
 (0)