ordered_attributes.rst 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ===========================
  2. Rule ``ordered_attributes``
  3. ===========================
  4. Sorts attributes using the configured sort algorithm.
  5. Configuration
  6. -------------
  7. ``order``
  8. ~~~~~~~~~
  9. A list of FQCNs of attributes defining the desired order used when custom
  10. sorting algorithm is configured.
  11. Allowed types: ``list<string>``
  12. Default value: ``[]``
  13. ``sort_algorithm``
  14. ~~~~~~~~~~~~~~~~~~
  15. How the attributes should be sorted.
  16. Allowed values: ``'alpha'`` and ``'custom'``
  17. Default value: ``'alpha'``
  18. Examples
  19. --------
  20. Example #1
  21. ~~~~~~~~~~
  22. *Default* configuration.
  23. .. code-block:: diff
  24. --- Original
  25. +++ New
  26. <?php
  27. +#[Bar(3)]
  28. +#[Corge(a: 'test')]
  29. #[Foo]
  30. -#[Bar(3)]
  31. #[Qux(new Bar(5))]
  32. -#[Corge(a: 'test')]
  33. class Sample1 {}
  34. #[
  35. + Bar(3),
  36. + Corge(a: 'test'),
  37. Foo,
  38. - Bar(3),
  39. Qux(new Bar(5)),
  40. - Corge(a: 'test'),
  41. ]
  42. class Sample2 {}
  43. Example #2
  44. ~~~~~~~~~~
  45. With configuration: ``['sort_algorithm' => 'custom', 'order' => ['A\\B\\Qux', 'A\\B\\Bar', 'A\\B\\Corge']]``.
  46. .. code-block:: diff
  47. --- Original
  48. +++ New
  49. <?php
  50. use A\B\Foo;
  51. use A\B\Bar as BarAlias;
  52. use A\B as AB;
  53. -#[Foo]
  54. +#[AB\Qux(new Bar(5))]
  55. #[BarAlias(3)]
  56. -#[AB\Qux(new Bar(5))]
  57. #[\A\B\Corge(a: 'test')]
  58. +#[Foo]
  59. class Sample1 {}
  60. References
  61. ----------
  62. - Fixer class: `PhpCsFixer\\Fixer\\AttributeNotation\\OrderedAttributesFixer <./../../../src/Fixer/AttributeNotation/OrderedAttributesFixer.php>`_
  63. - Test class: `PhpCsFixer\\Tests\\Fixer\\AttributeNotation\\OrderedAttributesFixerTest <./../../../tests/Fixer/AttributeNotation/OrderedAttributesFixerTest.php>`_
  64. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.