final_class.rst 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 you aren't ready yet for serious OOP, go with
  15. FinalInternalClassFixer, it's fine.
  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 {}