Skip to content

Commit

Permalink
NoUnneededFinalMethodFixer - add option to disable fixing final priva…
Browse files Browse the repository at this point in the history
…te methods of non-final classes
  • Loading branch information
SpacePossum committed Feb 11, 2020
1 parent 7a0e3be commit 0e48a1b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,11 @@ Choose from the list of available rules:

*Risky rule: risky when child class overrides a ``private`` method.*

Configuration options:

- ``private_methods`` (``bool``): private methods of non-``final`` classes must not
be declared ``final``; defaults to ``true``

* **no_unreachable_default_argument_value** [@PhpCsFixer:risky]

In function arguments there must not be arguments with default values
Expand Down
34 changes: 32 additions & 2 deletions src/Fixer/ClassNotation/NoUnneededFinalMethodFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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.'
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Fixer/ClassNotation/NoUnneededFinalMethodFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,45 @@ final private function qux()
],
];
}

/**
* @param string $expected
* @param string $input
*
* @dataProvider provideFixConfigCases
*/
public function testFixConfig($expected, $input, array $config)
{
$this->fixer->configure($config);
$this->doTest($expected, $input);
}

public function provideFixConfigCases()
{
yield [
'<?php
final class Foo
{
private function baz() {}
}
class Bar
{
final private function bar1() {}
}
',
'<?php
final class Foo
{
final private function baz() {}
}
class Bar
{
final private function bar1() {}
}
',
['private_methods' => false],
];
}
}

0 comments on commit 0e48a1b

Please sign in to comment.