PHP-CS-Fixer: Problem with rule no_unneeded_final_method

For configuration or updating questions please read the README and UPGRADE documentation, or visit: https://gitter.im/PHP-CS-Fixer

The rule no_unneeded_final_method causes problems. I could disable it, but this is a useful rule. In this example, i create a base class and forbid it to use the constructor and override in it. If the constructor is not marked as final, then it will be possible to override it and break the behavior of the class.

This is also due to the innovation in the PHPStan.

When reporting an issue (bug) please provide the following information:

The PHP version you are using ($ php -v):

PHP 7.3.13-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:48:32) ( NTS )

PHP CS Fixer version you are using ($ php-cs-fixer -V):

PHP CS Fixer 2.16.1 Yellow Bird by Fabien Potencier and Dariusz Ruminski

The command you use to run PHP CS Fixer:

./vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix -v

The configuration file you are using, if any:

<?php
declare(strict_types=1);

return PhpCsFixer\Config::create()
    ->setRules([
        '@Symfony' => true,
        'array_syntax' => ['syntax' => 'short'],
        'class_definition' => [
            'multi_line_extends_each_single_line' => true,
        ],
        'no_superfluous_phpdoc_tags' => false,
        'single_line_throw' => false,
        'blank_line_after_opening_tag' => false,
        'yoda_style' => false,
        'phpdoc_no_empty_return' => false,
        'ordered_imports' => [
            'sort_algorithm' => 'alpha',
        ],
        'list_syntax' => [
            'syntax' => 'short',
        ],
    ])
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in(__DIR__.'/src')
            ->in(__DIR__.'/tests')
    )
;

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
abstract class Foo
{
    final private function __construct()
    {
    }

    /**
     * @return static
     */
    public static function create()
    {
        return new static();
    }
}

class Bar extends Foo
{
}

$bar = Bar::create();
  • with unexpected changes applied when running PHP CS Fixer:

abstract class Foo
{
    private function __construct()
    {
    }

    /**
     * @return static
     */
    public static function create()
    {
        return new static();
    }
}

class Bar extends Foo
{
}

$bar = Bar::create();
-    final private function __construct()
+    private function __construct()
  • with the changes you expected instead:

don’t expect any changes

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

IIRC the reasoning was that private methods cannot be overridden/overloaded and therefore declaring these as final is never needed. As such, I asked for the motivation for when this would be needed and makes sense.

I still agree updating the description make sense as it is more precise on what the fixer currently does.

The fixer can be updated with an opt-out configuration option for the behavior for fixing private final methods (it would be the BC way). However reading the discussion and looking at the cases it won’t be on my todo-list.