Skip to content

Commit e1db0c3

Browse files
committed
Merge branch '2.15' into 2.16
2 parents ac98e77 + d07988b commit e1db0c3

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

.circleci/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ version: 2
33
jobs:
44
build:
55
macos:
6-
xcode: '11.7.0'
6+
# https://circleci.com/docs/2.0/testing-ios/#supported-xcode-versions
7+
xcode: '12.2.0'
78
steps:
89
- checkout
910

src/Fixer/ClassNotation/NoUnneededFinalMethodFixer.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
9090
private function fixClass(Tokens $tokens, $classOpenIndex, $classIsFinal)
9191
{
9292
$tokensCount = \count($tokens);
93+
9394
for ($index = $classOpenIndex + 1; $index < $tokensCount; ++$index) {
9495
// Class end
9596
if ($tokens[$index]->equals('}')) {
@@ -107,15 +108,16 @@ private function fixClass(Tokens $tokens, $classOpenIndex, $classIsFinal)
107108
continue;
108109
}
109110

110-
if (!$classIsFinal && !$this->isPrivateMethod($tokens, $index, $classOpenIndex)) {
111+
if (!$classIsFinal && !$this->isPrivateMethodOtherThanConstructor($tokens, $index, $classOpenIndex)) {
111112
continue;
112113
}
113114

114115
$tokens->clearAt($index);
115116

116-
$nextTokenIndex = $index + 1;
117-
if ($tokens[$nextTokenIndex]->isWhitespace()) {
118-
$tokens->clearAt($nextTokenIndex);
117+
++$index;
118+
119+
if ($tokens[$index]->isWhitespace()) {
120+
$tokens->clearAt($index);
119121
}
120122
}
121123
}
@@ -126,18 +128,19 @@ private function fixClass(Tokens $tokens, $classOpenIndex, $classIsFinal)
126128
*
127129
* @return bool
128130
*/
129-
private function isPrivateMethod(Tokens $tokens, $index, $classOpenIndex)
131+
private function isPrivateMethodOtherThanConstructor(Tokens $tokens, $index, $classOpenIndex)
130132
{
131133
$index = max($classOpenIndex + 1, $tokens->getPrevTokenOfKind($index, [';', '{', '}']));
134+
$private = false;
132135

133136
while (!$tokens[$index]->isGivenKind(T_FUNCTION)) {
134137
if ($tokens[$index]->isGivenKind(T_PRIVATE)) {
135-
return true;
138+
$private = true;
136139
}
137140

138-
++$index;
141+
$index = $tokens->getNextMeaningfulToken($index);
139142
}
140143

141-
return false;
144+
return $private && '__construct' !== strtolower($tokens[$tokens->getNextMeaningfulToken($index)]->getContent());
142145
}
143146
}

tests/Fixer/ClassNotation/NoUnneededFinalMethodFixerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ private final function bar() {}
217217
'trait' => [
218218
'<php trait Foo { final public function foo() {} }',
219219
],
220+
'do not fix constructors' => [
221+
'<?php
222+
class Bar
223+
{
224+
final private function __construct()
225+
{
226+
}
227+
}',
228+
],
220229
];
221230
}
222231

0 commit comments

Comments
 (0)