curly_braces_position.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ==============================
  2. Rule ``curly_braces_position``
  3. ==============================
  4. Curly braces must be placed as configured.
  5. Configuration
  6. -------------
  7. ``control_structures_opening_brace``
  8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. The position of the opening brace of control structures‘ body.
  10. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  11. Default value: ``'same_line'``
  12. ``functions_opening_brace``
  13. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. The position of the opening brace of functions‘ body.
  15. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  16. Default value: ``'next_line_unless_newline_at_signature_end'``
  17. ``anonymous_functions_opening_brace``
  18. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. The position of the opening brace of anonymous functions‘ body.
  20. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  21. Default value: ``'same_line'``
  22. ``classes_opening_brace``
  23. ~~~~~~~~~~~~~~~~~~~~~~~~~
  24. The position of the opening brace of classes‘ body.
  25. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  26. Default value: ``'next_line_unless_newline_at_signature_end'``
  27. ``anonymous_classes_opening_brace``
  28. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. The position of the opening brace of anonymous classes‘ body.
  30. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  31. Default value: ``'same_line'``
  32. ``allow_single_line_empty_anonymous_classes``
  33. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  34. Allow anonymous classes to have opening and closing braces on the same line.
  35. Allowed types: ``bool``
  36. Default value: ``true``
  37. ``allow_single_line_anonymous_functions``
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. Allow anonymous functions to have opening and closing braces on the same line.
  40. Allowed types: ``bool``
  41. Default value: ``true``
  42. Examples
  43. --------
  44. Example #1
  45. ~~~~~~~~~~
  46. *Default* configuration.
  47. .. code-block:: diff
  48. --- Original
  49. +++ New
  50. <?php
  51. -class Foo {
  52. +class Foo
  53. +{
  54. }
  55. -function foo() {
  56. +function foo()
  57. +{
  58. }
  59. -$foo = function()
  60. -{
  61. +$foo = function() {
  62. };
  63. -if (foo())
  64. -{
  65. +if (foo()) {
  66. bar();
  67. }
  68. -$foo = new class
  69. -{
  70. +$foo = new class {
  71. };
  72. Example #2
  73. ~~~~~~~~~~
  74. With configuration: ``['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end']``.
  75. .. code-block:: diff
  76. --- Original
  77. +++ New
  78. <?php
  79. -if (foo()) {
  80. +if (foo())
  81. +{
  82. bar();
  83. }
  84. Example #3
  85. ~~~~~~~~~~
  86. With configuration: ``['functions_opening_brace' => 'same_line']``.
  87. .. code-block:: diff
  88. --- Original
  89. +++ New
  90. <?php
  91. -function foo()
  92. -{
  93. +function foo() {
  94. }
  95. Example #4
  96. ~~~~~~~~~~
  97. With configuration: ``['anonymous_functions_opening_brace' => 'next_line_unless_newline_at_signature_end']``.
  98. .. code-block:: diff
  99. --- Original
  100. +++ New
  101. <?php
  102. -$foo = function () {
  103. +$foo = function ()
  104. +{
  105. };
  106. Example #5
  107. ~~~~~~~~~~
  108. With configuration: ``['classes_opening_brace' => 'same_line']``.
  109. .. code-block:: diff
  110. --- Original
  111. +++ New
  112. <?php
  113. -class Foo
  114. -{
  115. +class Foo {
  116. }
  117. Example #6
  118. ~~~~~~~~~~
  119. With configuration: ``['anonymous_classes_opening_brace' => 'next_line_unless_newline_at_signature_end']``.
  120. .. code-block:: diff
  121. --- Original
  122. +++ New
  123. <?php
  124. -$foo = new class {
  125. +$foo = new class
  126. +{
  127. };
  128. Example #7
  129. ~~~~~~~~~~
  130. With configuration: ``['allow_single_line_empty_anonymous_classes' => true]``.
  131. .. code-block:: diff
  132. --- Original
  133. +++ New
  134. <?php
  135. $foo = new class { };
  136. -$bar = new class { private $baz; };
  137. +$bar = new class {
  138. +private $baz;
  139. +};
  140. Example #8
  141. ~~~~~~~~~~
  142. With configuration: ``['allow_single_line_anonymous_functions' => true]``.
  143. .. code-block:: diff
  144. --- Original
  145. +++ New
  146. <?php
  147. $foo = function () { return true; };
  148. -$bar = function () { $result = true;
  149. - return $result; };
  150. +$bar = function () {
  151. +$result = true;
  152. + return $result;
  153. +};
  154. Rule sets
  155. ---------
  156. The rule is part of the following rule sets:
  157. - `@PER <./../../ruleSets/PER.rst>`_ with config:
  158. ``['allow_single_line_empty_anonymous_classes' => true]``
  159. - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
  160. ``['allow_single_line_empty_anonymous_classes' => true]``
  161. - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
  162. ``['allow_single_line_empty_anonymous_classes' => true]``
  163. - `@PSR2 <./../../ruleSets/PSR2.rst>`_
  164. - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
  165. ``['allow_single_line_empty_anonymous_classes' => true]``
  166. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:
  167. ``['allow_single_line_anonymous_functions' => true, 'allow_single_line_empty_anonymous_classes' => true]``
  168. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  169. ``['allow_single_line_anonymous_functions' => true, 'allow_single_line_empty_anonymous_classes' => true]``