Skip to content

Commit

Permalink
Merge branch '2.15' into 2.16
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Oct 27, 2020
2 parents ac98e77 + d07988b commit e1db0c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ version: 2
jobs:
build:
macos:
xcode: '11.7.0'
# https://circleci.com/docs/2.0/testing-ios/#supported-xcode-versions
xcode: '12.2.0'
steps:
- checkout

Expand Down
19 changes: 11 additions & 8 deletions src/Fixer/ClassNotation/NoUnneededFinalMethodFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
private function fixClass(Tokens $tokens, $classOpenIndex, $classIsFinal)
{
$tokensCount = \count($tokens);

for ($index = $classOpenIndex + 1; $index < $tokensCount; ++$index) {
// Class end
if ($tokens[$index]->equals('}')) {
Expand All @@ -107,15 +108,16 @@ private function fixClass(Tokens $tokens, $classOpenIndex, $classIsFinal)
continue;
}

if (!$classIsFinal && !$this->isPrivateMethod($tokens, $index, $classOpenIndex)) {
if (!$classIsFinal && !$this->isPrivateMethodOtherThanConstructor($tokens, $index, $classOpenIndex)) {
continue;
}

$tokens->clearAt($index);

$nextTokenIndex = $index + 1;
if ($tokens[$nextTokenIndex]->isWhitespace()) {
$tokens->clearAt($nextTokenIndex);
++$index;

if ($tokens[$index]->isWhitespace()) {
$tokens->clearAt($index);
}
}
}
Expand All @@ -126,18 +128,19 @@ private function fixClass(Tokens $tokens, $classOpenIndex, $classIsFinal)
*
* @return bool
*/
private function isPrivateMethod(Tokens $tokens, $index, $classOpenIndex)
private function isPrivateMethodOtherThanConstructor(Tokens $tokens, $index, $classOpenIndex)
{
$index = max($classOpenIndex + 1, $tokens->getPrevTokenOfKind($index, [';', '{', '}']));
$private = false;

while (!$tokens[$index]->isGivenKind(T_FUNCTION)) {
if ($tokens[$index]->isGivenKind(T_PRIVATE)) {
return true;
$private = true;
}

++$index;
$index = $tokens->getNextMeaningfulToken($index);
}

return false;
return $private && '__construct' !== strtolower($tokens[$tokens->getNextMeaningfulToken($index)]->getContent());
}
}
9 changes: 9 additions & 0 deletions tests/Fixer/ClassNotation/NoUnneededFinalMethodFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ private final function bar() {}
'trait' => [
'<php trait Foo { final public function foo() {} }',
],
'do not fix constructors' => [
'<?php
class Bar
{
final private function __construct()
{
}
}',
],
];
}

Expand Down

0 comments on commit e1db0c3

Please sign in to comment.