self_accessor.rst 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. ======================
  2. Rule ``self_accessor``
  3. ======================
  4. Inside class or interface element ``self`` should be preferred to the class name
  5. itself.
  6. Warning
  7. -------
  8. Using this rule is risky
  9. ~~~~~~~~~~~~~~~~~~~~~~~~
  10. Risky when using dynamic calls like get_called_class() or late static binding.
  11. Examples
  12. --------
  13. Example #1
  14. ~~~~~~~~~~
  15. .. code-block:: diff
  16. --- Original
  17. +++ New
  18. <?php
  19. class Sample
  20. {
  21. const BAZ = 1;
  22. - const BAR = Sample::BAZ;
  23. + const BAR = self::BAZ;
  24. public function getBar()
  25. {
  26. - return Sample::BAR;
  27. + return self::BAR;
  28. }
  29. }
  30. Rule sets
  31. ---------
  32. The rule is part of the following rule sets:
  33. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  34. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_
  35. References
  36. ----------
  37. - Fixer class: `PhpCsFixer\\Fixer\\ClassNotation\\SelfAccessorFixer <./../../../src/Fixer/ClassNotation/SelfAccessorFixer.php>`_
  38. - Test class: `PhpCsFixer\\Tests\\Fixer\\ClassNotation\\SelfAccessorFixerTest <./../../../tests/Fixer/ClassNotation/SelfAccessorFixerTest.php>`_
  39. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.