blank_line_before_statement.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ====================================
  2. Rule ``blank_line_before_statement``
  3. ====================================
  4. An empty line feed must precede any configured statement.
  5. Configuration
  6. -------------
  7. ``statements``
  8. ~~~~~~~~~~~~~~
  9. List of statements which must be preceded by an empty line.
  10. Allowed values: a subset of ``['break', 'case', 'continue', 'declare', 'default', 'do', 'exit', 'for', 'foreach', 'goto', 'if', 'include', 'include_once', 'phpdoc', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', 'yield', 'yield_from']``
  11. Default value: ``['break', 'continue', 'declare', 'return', 'throw', 'try']``
  12. Examples
  13. --------
  14. Example #1
  15. ~~~~~~~~~~
  16. *Default* configuration.
  17. .. code-block:: diff
  18. --- Original
  19. +++ New
  20. <?php
  21. function A() {
  22. echo 1;
  23. +
  24. return 1;
  25. }
  26. Example #2
  27. ~~~~~~~~~~
  28. With configuration: ``['statements' => ['break']]``.
  29. .. code-block:: diff
  30. --- Original
  31. +++ New
  32. <?php
  33. switch ($foo) {
  34. case 42:
  35. $bar->process();
  36. +
  37. break;
  38. case 44:
  39. break;
  40. }
  41. Example #3
  42. ~~~~~~~~~~
  43. With configuration: ``['statements' => ['continue']]``.
  44. .. code-block:: diff
  45. --- Original
  46. +++ New
  47. <?php
  48. foreach ($foo as $bar) {
  49. if ($bar->isTired()) {
  50. $bar->sleep();
  51. +
  52. continue;
  53. }
  54. }
  55. Example #4
  56. ~~~~~~~~~~
  57. With configuration: ``['statements' => ['do']]``.
  58. .. code-block:: diff
  59. --- Original
  60. +++ New
  61. <?php
  62. $i = 0;
  63. +
  64. do {
  65. echo $i;
  66. } while ($i > 0);
  67. Example #5
  68. ~~~~~~~~~~
  69. With configuration: ``['statements' => ['exit']]``.
  70. .. code-block:: diff
  71. --- Original
  72. +++ New
  73. <?php
  74. if ($foo === false) {
  75. exit(0);
  76. } else {
  77. $bar = 9000;
  78. +
  79. exit(1);
  80. }
  81. Example #6
  82. ~~~~~~~~~~
  83. With configuration: ``['statements' => ['goto']]``.
  84. .. code-block:: diff
  85. --- Original
  86. +++ New
  87. <?php
  88. a:
  89. if ($foo === false) {
  90. goto a;
  91. } else {
  92. $bar = 9000;
  93. +
  94. goto b;
  95. }
  96. Example #7
  97. ~~~~~~~~~~
  98. With configuration: ``['statements' => ['if']]``.
  99. .. code-block:: diff
  100. --- Original
  101. +++ New
  102. <?php
  103. $a = 9000;
  104. +
  105. if (true) {
  106. $foo = $bar;
  107. }
  108. Example #8
  109. ~~~~~~~~~~
  110. With configuration: ``['statements' => ['return']]``.
  111. .. code-block:: diff
  112. --- Original
  113. +++ New
  114. <?php
  115. if (true) {
  116. $foo = $bar;
  117. +
  118. return;
  119. }
  120. Example #9
  121. ~~~~~~~~~~
  122. With configuration: ``['statements' => ['switch']]``.
  123. .. code-block:: diff
  124. --- Original
  125. +++ New
  126. <?php
  127. $a = 9000;
  128. +
  129. switch ($a) {
  130. case 42:
  131. break;
  132. }
  133. Example #10
  134. ~~~~~~~~~~~
  135. With configuration: ``['statements' => ['throw']]``.
  136. .. code-block:: diff
  137. --- Original
  138. +++ New
  139. <?php
  140. if (null === $a) {
  141. $foo->bar();
  142. +
  143. throw new \UnexpectedValueException("A cannot be null.");
  144. }
  145. Example #11
  146. ~~~~~~~~~~~
  147. With configuration: ``['statements' => ['try']]``.
  148. .. code-block:: diff
  149. --- Original
  150. +++ New
  151. <?php
  152. $a = 9000;
  153. +
  154. try {
  155. $foo->bar();
  156. } catch (\Exception $exception) {
  157. $a = -1;
  158. }
  159. Example #12
  160. ~~~~~~~~~~~
  161. With configuration: ``['statements' => ['yield']]``.
  162. .. code-block:: diff
  163. --- Original
  164. +++ New
  165. <?php
  166. function getValues() {
  167. yield 1;
  168. +
  169. yield 2;
  170. +
  171. // comment
  172. yield 3;
  173. }
  174. Rule sets
  175. ---------
  176. The rule is part of the following rule sets:
  177. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:
  178. ``['statements' => ['break', 'case', 'continue', 'declare', 'default', 'exit', 'goto', 'include', 'include_once', 'phpdoc', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'yield', 'yield_from']]``
  179. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  180. ``['statements' => ['return']]``
  181. References
  182. ----------
  183. - Fixer class: `PhpCsFixer\\Fixer\\Whitespace\\BlankLineBeforeStatementFixer <./../../../src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php>`_
  184. - Test class: `PhpCsFixer\\Tests\\Fixer\\Whitespace\\BlankLineBeforeStatementFixerTest <./../../../tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php>`_
  185. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.