final_class.rst 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ====================
  2. Rule ``final_class``
  3. ====================
  4. All classes must be final, except abstract ones and Doctrine entities.
  5. Description
  6. -----------
  7. No exception and no configuration are intentional. Beside Doctrine entities and
  8. of course abstract classes, there is no single reason not to declare all classes
  9. final. If you want to subclass a class, mark the parent class as abstract and
  10. create two child classes, one empty if necessary: you'll gain much more fine
  11. grained type-hinting. If you need to mock a standalone class, create an
  12. interface, or maybe it's a value-object that shouldn't be mocked at all. If you
  13. need to extend a standalone class, create an interface and use the Composite
  14. pattern. If these rules are too strict for you, you can use
  15. ``FinalInternalClassFixer`` instead.
  16. Warning
  17. -------
  18. Using this rule is risky
  19. ~~~~~~~~~~~~~~~~~~~~~~~~
  20. Risky when subclassing non-abstract classes.
  21. Examples
  22. --------
  23. Example #1
  24. ~~~~~~~~~~
  25. .. code-block:: diff
  26. --- Original
  27. +++ New
  28. <?php
  29. -class MyApp {}
  30. +final class MyApp {}
  31. References
  32. ----------
  33. - Fixer class: `PhpCsFixer\\Fixer\\ClassNotation\\FinalClassFixer <./../../../src/Fixer/ClassNotation/FinalClassFixer.php>`_
  34. - Test class: `PhpCsFixer\\Tests\\Fixer\\ClassNotation\\FinalClassFixerTest <./../../../tests/Fixer/ClassNotation/FinalClassFixerTest.php>`_
  35. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.