braces.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ===============
  2. Rule ``braces``
  3. ===============
  4. The body of each structure MUST be enclosed by braces. Braces should be properly
  5. placed. Body of braces should be properly indented.
  6. Configuration
  7. -------------
  8. ``allow_single_line_closure``
  9. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. Whether single line lambda notation should be allowed.
  11. Allowed types: ``bool``
  12. Default value: ``false``
  13. ``position_after_functions_and_oop_constructs``
  14. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15. whether the opening brace should be placed on "next" or "same" line after classy
  16. constructs (non-anonymous classes, interfaces, traits, methods and non-lambda
  17. functions).
  18. Allowed values: ``'next'``, ``'same'``
  19. Default value: ``'next'``
  20. ``position_after_control_structures``
  21. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. whether the opening brace should be placed on "next" or "same" line after
  23. control structures.
  24. Allowed values: ``'next'``, ``'same'``
  25. Default value: ``'same'``
  26. ``position_after_anonymous_constructs``
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. whether the opening brace should be placed on "next" or "same" line after
  29. anonymous constructs (anonymous classes and lambda functions).
  30. Allowed values: ``'next'``, ``'same'``
  31. Default value: ``'same'``
  32. Examples
  33. --------
  34. Example #1
  35. ~~~~~~~~~~
  36. *Default* configuration.
  37. .. code-block:: diff
  38. --- Original
  39. +++ New
  40. @@ -1,25 +1,27 @@
  41. <?php
  42. -class Foo {
  43. - public function bar($baz) {
  44. - if ($baz = 900) echo "Hello!";
  45. +class Foo
  46. +{
  47. + public function bar($baz)
  48. + {
  49. + if ($baz = 900) {
  50. + echo "Hello!";
  51. + }
  52. - if ($baz = 9000)
  53. + if ($baz = 9000) {
  54. echo "Wait!";
  55. + }
  56. - if ($baz == true)
  57. - {
  58. + if ($baz == true) {
  59. echo "Why?";
  60. - }
  61. - else
  62. - {
  63. + } else {
  64. echo "Ha?";
  65. }
  66. - if (is_array($baz))
  67. - foreach ($baz as $b)
  68. - {
  69. + if (is_array($baz)) {
  70. + foreach ($baz as $b) {
  71. echo $b;
  72. }
  73. + }
  74. }
  75. }
  76. Example #2
  77. ~~~~~~~~~~
  78. With configuration: ``['allow_single_line_closure' => true]``.
  79. .. code-block:: diff
  80. --- Original
  81. +++ New
  82. @@ -1,4 +1,5 @@
  83. <?php
  84. $positive = function ($item) { return $item >= 0; };
  85. $negative = function ($item) {
  86. - return $item < 0; };
  87. + return $item < 0;
  88. +};
  89. Example #3
  90. ~~~~~~~~~~
  91. With configuration: ``['position_after_functions_and_oop_constructs' => 'same']``.
  92. .. code-block:: diff
  93. --- Original
  94. +++ New
  95. @@ -1,27 +1,25 @@
  96. <?php
  97. -class Foo
  98. -{
  99. - public function bar($baz)
  100. - {
  101. - if ($baz = 900) echo "Hello!";
  102. +class Foo {
  103. + public function bar($baz) {
  104. + if ($baz = 900) {
  105. + echo "Hello!";
  106. + }
  107. - if ($baz = 9000)
  108. + if ($baz = 9000) {
  109. echo "Wait!";
  110. + }
  111. - if ($baz == true)
  112. - {
  113. + if ($baz == true) {
  114. echo "Why?";
  115. - }
  116. - else
  117. - {
  118. + } else {
  119. echo "Ha?";
  120. }
  121. - if (is_array($baz))
  122. - foreach ($baz as $b)
  123. - {
  124. + if (is_array($baz)) {
  125. + foreach ($baz as $b) {
  126. echo $b;
  127. }
  128. + }
  129. }
  130. }
  131. Rule sets
  132. ---------
  133. The rule is part of the following rule sets:
  134. @PSR2
  135. Using the ``@PSR2`` rule set will enable the ``braces`` rule with the default config.
  136. @Symfony
  137. Using the ``@Symfony`` rule set will enable the ``braces`` rule with the config below:
  138. ``['allow_single_line_closure' => true]``
  139. @PhpCsFixer
  140. Using the ``@PhpCsFixer`` rule set will enable the ``braces`` rule with the config below:
  141. ``['allow_single_line_closure' => true]``