class_definition.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. =========================
  2. Rule ``class_definition``
  3. =========================
  4. Whitespace around the keywords of a class, trait, enum or interfaces definition
  5. should be one space.
  6. Configuration
  7. -------------
  8. ``multi_line_extends_each_single_line``
  9. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. Whether definitions should be multiline.
  11. Allowed types: ``bool``
  12. Default value: ``false``
  13. ``single_item_single_line``
  14. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15. Whether definitions should be single line when including a single item.
  16. Allowed types: ``bool``
  17. Default value: ``false``
  18. ``single_line``
  19. ~~~~~~~~~~~~~~~
  20. Whether definitions should be single line.
  21. Allowed types: ``bool``
  22. Default value: ``false``
  23. ``space_before_parenthesis``
  24. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. Whether there should be a single space after the parenthesis of anonymous class
  26. (PSR12) or not.
  27. Allowed types: ``bool``
  28. Default value: ``false``
  29. ``inline_constructor_arguments``
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. Whether constructor argument list in anonymous classes should be single line.
  32. Allowed types: ``bool``
  33. Default value: ``true``
  34. Examples
  35. --------
  36. Example #1
  37. ~~~~~~~~~~
  38. *Default* configuration.
  39. .. code-block:: diff
  40. --- Original
  41. +++ New
  42. <?php
  43. -class Foo extends Bar implements Baz, BarBaz
  44. +class Foo extends Bar implements Baz, BarBaz
  45. {
  46. }
  47. -final class Foo extends Bar implements Baz, BarBaz
  48. +final class Foo extends Bar implements Baz, BarBaz
  49. {
  50. }
  51. -trait Foo
  52. +trait Foo
  53. {
  54. }
  55. -$foo = new class extends Bar implements Baz, BarBaz {};
  56. +$foo = new class extends Bar implements Baz, BarBaz {};
  57. Example #2
  58. ~~~~~~~~~~
  59. With configuration: ``['single_line' => true]``.
  60. .. code-block:: diff
  61. --- Original
  62. +++ New
  63. <?php
  64. -class Foo
  65. -extends Bar
  66. -implements Baz, BarBaz
  67. +class Foo extends Bar implements Baz, BarBaz
  68. {}
  69. Example #3
  70. ~~~~~~~~~~
  71. With configuration: ``['single_item_single_line' => true]``.
  72. .. code-block:: diff
  73. --- Original
  74. +++ New
  75. <?php
  76. -class Foo
  77. -extends Bar
  78. -implements Baz
  79. +class Foo extends Bar implements Baz
  80. {}
  81. Example #4
  82. ~~~~~~~~~~
  83. With configuration: ``['multi_line_extends_each_single_line' => true]``.
  84. .. code-block:: diff
  85. --- Original
  86. +++ New
  87. <?php
  88. interface Bar extends
  89. - Bar, BarBaz, FooBarBaz
  90. + Bar,
  91. + BarBaz,
  92. + FooBarBaz
  93. {}
  94. Example #5
  95. ~~~~~~~~~~
  96. With configuration: ``['space_before_parenthesis' => true]``.
  97. .. code-block:: diff
  98. --- Original
  99. +++ New
  100. <?php
  101. -$foo = new class(){};
  102. +$foo = new class () {};
  103. Example #6
  104. ~~~~~~~~~~
  105. With configuration: ``['inline_constructor_arguments' => true]``.
  106. .. code-block:: diff
  107. --- Original
  108. +++ New
  109. <?php
  110. -$foo = new class(
  111. - $bar,
  112. - $baz
  113. -) {};
  114. +$foo = new class($bar, $baz) {};
  115. Rule sets
  116. ---------
  117. The rule is part of the following rule sets:
  118. - `@PER <./../../ruleSets/PER.rst>`_ with config:
  119. ``['inline_constructor_arguments' => false, 'space_before_parenthesis' => true]``
  120. - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
  121. ``['inline_constructor_arguments' => false, 'space_before_parenthesis' => true]``
  122. - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
  123. ``['inline_constructor_arguments' => false, 'space_before_parenthesis' => true]``
  124. - `@PSR2 <./../../ruleSets/PSR2.rst>`_
  125. - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
  126. ``['inline_constructor_arguments' => false, 'space_before_parenthesis' => true]``
  127. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:
  128. ``['single_line' => true]``
  129. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  130. ``['single_line' => true]``