Browse Source

NoUnneededFinalMethodFixer - mark as risky

SpacePossum 5 years ago
parent
commit
e9e586f748

+ 4 - 2
README.rst

@@ -1157,11 +1157,13 @@ Choose from the list of available rules:
   - ``namespaces`` (``bool``): remove unneeded curly braces from bracketed
     namespaces; defaults to ``false``
 
-* **no_unneeded_final_method** [@Symfony, @PhpCsFixer]
+* **no_unneeded_final_method** [@Symfony:risky, @PhpCsFixer:risky]
 
-  A ``final`` class must not have ``final`` methods and ``private`` method must
+  A ``final`` class must not have ``final`` methods and ``private`` methods must
   not be ``final``.
 
+  *Risky rule: risky when child class overrides a ``private`` method.*
+
 * **no_unreachable_default_argument_value** [@PhpCsFixer:risky]
 
   In function arguments there must not be arguments with default values

+ 16 - 10
src/Fixer/ClassNotation/NoUnneededFinalMethodFixer.php

@@ -28,25 +28,26 @@ final class NoUnneededFinalMethodFixer extends AbstractFixer
     public function getDefinition()
     {
         return new FixerDefinition(
-            'A `final` class must not have `final` methods and `private` method must not be `final`.',
+            'A `final` class must not have `final` methods and `private` methods must not be `final`.',
             [
                 new CodeSample(
                     '<?php
-final class Foo {
-    final public function foo() {}
+final class Foo
+{
+    final public function foo1() {}
     final protected function bar() {}
     final private function baz() {}
 }
-'
-                ),
-                new CodeSample(
-                    '<?php
-class Foo {
-    final private function bar() {}
+
+class Bar
+{
+    final private function bar1() {}
 }
 '
                 ),
-            ]
+            ],
+            null,
+            'Risky when child class overrides a `private` method.'
         );
     }
 
@@ -58,6 +59,11 @@ class Foo {
         return $tokens->isAllTokenKindsFound([T_CLASS, T_FINAL]);
     }
 
+    public function isRisky()
+    {
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */

+ 1 - 1
src/RuleSet.php

@@ -108,7 +108,6 @@ final class RuleSet implements RuleSetInterface
             'no_trailing_comma_in_singleline_array' => true,
             'no_unneeded_control_parentheses' => true,
             'no_unneeded_curly_braces' => ['namespaces' => true],
-            'no_unneeded_final_method' => true,
             'no_unused_imports' => true,
             'no_whitespace_before_comma_in_array' => true,
             'no_whitespace_in_blank_line' => true,
@@ -206,6 +205,7 @@ final class RuleSet implements RuleSetInterface
             ],
             'no_alias_functions' => true,
             'no_homoglyph_names' => true,
+            'no_unneeded_final_method' => true,
             'non_printable_character' => true,
             'php_unit_construct' => true,
             'php_unit_mock_short_will_return' => true,

+ 4 - 0
tests/Fixtures/Integration/set/@Symfony-risky.test

@@ -0,0 +1,4 @@
+--TEST--
+Integration of @Symfony:risky.
+--RULESET--
+{"@Symfony:risky": true}

+ 19 - 0
tests/Fixtures/Integration/set/@Symfony-risky.test-in.php

@@ -0,0 +1,19 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Acme;
+
+declare(ticks=1);
+
+final class FinalClass
+{
+    final function finalMethod(){}
+}

+ 19 - 0
tests/Fixtures/Integration/set/@Symfony-risky.test-out.php

@@ -0,0 +1,19 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Acme;
+
+declare(ticks=1);
+
+final class FinalClass
+{
+    function finalMethod(){}
+}

+ 1 - 1
tests/Fixtures/Integration/set/@Symfony.test-in.php

@@ -112,7 +112,7 @@ implements
     }
 }
 
-final class finalClass
+final class FinalClass
 {
     final function finalMethod(){}
 }

+ 2 - 2
tests/Fixtures/Integration/set/@Symfony.test-out.php

@@ -107,9 +107,9 @@ class FooBarTest extends \PHPUnit_Framework_TestCase implements Test1Interface,
     }
 }
 
-final class finalClass
+final class FinalClass
 {
-    public function finalMethod()
+    final public function finalMethod()
     {
     }
 }

+ 1 - 1
tests/Fixtures/Integration/set/@Symfony_whitespaces.test-in.php

@@ -112,7 +112,7 @@ implements
     }
 }
 
-final class finalClass
+final class FinalClass
 {
     final function finalMethod(){}
 }

+ 2 - 2
tests/Fixtures/Integration/set/@Symfony_whitespaces.test-out.php

@@ -107,9 +107,9 @@ class FooBarTest extends \PHPUnit_Framework_TestCase implements Test1Interface,
 	}
 }
 
-final class finalClass
+final class FinalClass
 {
-	public function finalMethod()
+	final public function finalMethod()
 	{
 	}
 }

Some files were not shown because too many files changed in this diff