Skip to content

Commit

Permalink
bug: FinalPublicMethodForAbstractClassFixer - fix for readonly clas…
Browse files Browse the repository at this point in the history
…ses (#7123)
  • Loading branch information
kubawerlos committed Jul 5, 2023
1 parent 6db6a98 commit 082bb26
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function start()

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAllTokenKindsFound([T_CLASS, T_ABSTRACT, T_PUBLIC, T_FUNCTION]);
return $tokens->isAllTokenKindsFound([T_ABSTRACT, T_PUBLIC, T_FUNCTION]);
}

public function isRisky(): bool
Expand All @@ -81,11 +81,11 @@ public function isRisky(): bool

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$classes = array_keys($tokens->findGivenKind(T_CLASS));
$abstracts = array_keys($tokens->findGivenKind(T_ABSTRACT));

while ($classIndex = array_pop($classes)) {
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($classIndex)];
if (!$prevToken->isGivenKind(T_ABSTRACT)) {
while ($abstractIndex = array_pop($abstracts)) {
$classIndex = $tokens->getNextTokenOfKind($abstractIndex, [[T_CLASS], [T_FUNCTION]]);
if (!$tokens[$classIndex]->isGivenKind(T_CLASS)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function testFix(string $expected, ?string $input = null): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
$original = $fixed = self::getClassElementStubs();
Expand Down Expand Up @@ -116,6 +119,52 @@ abstract public static function bar();
];
}

/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFix82Cases(): iterable
{
yield 'abstract keyword after readonly/public keywords' => [
'<?php readonly abstract class Foo {
public abstract function bar();
}',
];

yield 'abstract keyword before readonly/public keywords' => [
'<?php abstract readonly class Foo {
abstract public function bar();
}',
];

yield 'abstract readonly class' => [
'<?php abstract readonly class Foo {
final public function bar() {}
}',
'<?php abstract readonly class Foo {
public function bar() {}
}',
];

yield 'readonly abstract class' => [
'<?php readonly abstract class Foo {
final public function bar() {}
}',
'<?php readonly abstract class Foo {
public function bar() {}
}',
];
}

private static function getClassElementStubs(): string
{
return '
Expand Down

0 comments on commit 082bb26

Please sign in to comment.