Skip to content

Commit

Permalink
minor #5184 [FinalStaticAccessFixer] Handle new static() in final cla…
Browse files Browse the repository at this point in the history
…ss (localheinz)

This PR was merged into the 2.16 branch.

Discussion
----------

[FinalStaticAccessFixer] Handle new static() in final class

This PR

* [x] handles `new static()` in `final` classes

💁‍♂️ Not sure, should I target the `2.15` branch instead?

Commits
-------

9b36058 Enhancement: Handle new static() in final class
  • Loading branch information
SpacePossum committed Oct 19, 2020
2 parents 5728b26 + 9b36058 commit d0f9be5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Fixer/ClassNotation/FinalStaticAccessFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ private function replaceStaticAccessWithSelfAccessBetween(
continue;
}

$newIndex = $tokens->getPrevMeaningfulToken($index);
$doubleColonIndex = $tokens->getNextMeaningfulToken($index);

if (!$tokens[$doubleColonIndex]->isGivenKind(T_DOUBLE_COLON)) {
if (!$tokens[$newIndex]->isGivenKind(T_NEW) && !$tokens[$doubleColonIndex]->isGivenKind(T_DOUBLE_COLON)) {
continue;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Fixer/ClassNotation/FinalStaticAccessFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public function provideFixCases()
'<?php final class A { public function b() { echo self::C; } }',
'<?php final class A { public function b() { echo static::C; } }',
],
'in method as new' => [
'<?php final class A { public static function b() { return new self(); } }',
'<?php final class A { public static function b() { return new static(); } }',
],
'in method as new with comments' => [
'<?php final class A { public static function b() { return new /* hmm */ self(); } }',
'<?php final class A { public static function b() { return new /* hmm */ static(); } }',
],
'in method as new without parentheses' => [
'<?php final class A { public static function b() { return new self; } }',
'<?php final class A { public static function b() { return new static; } }',
],
'does not change non-final classes' => [
'<?php class A { public function b() { echo static::c(); } }',
],
Expand Down

0 comments on commit d0f9be5

Please sign in to comment.