-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NoUnneededFinalMethodFixer - add option to disable fixing final priva…
…te methods of non-final classes
- Loading branch information
1 parent
7a0e3be
commit 0e48a1b
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,14 +13,17 @@ | |
namespace PhpCsFixer\Fixer\ClassNotation; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; | ||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Filippo Tessarotto <[email protected]> | ||
*/ | ||
final class NoUnneededFinalMethodFixer extends AbstractFixer | ||
final class NoUnneededFinalMethodFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
|
@@ -45,6 +48,20 @@ final private function bar1() {} | |
} | ||
' | ||
), | ||
new CodeSample( | ||
'<?php | ||
final class Foo | ||
{ | ||
final private function baz() {} | ||
} | ||
class Bar | ||
{ | ||
final private function bar1() {} | ||
} | ||
', | ||
['private_methods' => false] | ||
), | ||
], | ||
null, | ||
'Risky when child class overrides a `private` method.' | ||
|
@@ -83,6 +100,19 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) | |
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createConfigurationDefinition() | ||
{ | ||
return new FixerConfigurationResolver([ | ||
(new FixerOptionBuilder('private_methods', 'Private methods of non-`final` classes must not be declared `final`.')) | ||
->setAllowedTypes(['bool']) | ||
->setDefault(true) | ||
->getOption(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param int $classOpenIndex | ||
* @param bool $classIsFinal | ||
|
@@ -107,7 +137,7 @@ private function fixClass(Tokens $tokens, $classOpenIndex, $classIsFinal) | |
continue; | ||
} | ||
|
||
if (!$classIsFinal && !$this->isPrivateMethod($tokens, $index, $classOpenIndex)) { | ||
if (!$classIsFinal && (!$this->isPrivateMethod($tokens, $index, $classOpenIndex) || !$this->configuration['private_methods'])) { | ||
continue; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters