single_space_around_construct.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ======================================
  2. Rule ``single_space_around_construct``
  3. ======================================
  4. Ensures a single space after language constructs.
  5. Configuration
  6. -------------
  7. ``constructs_contain_a_single_space``
  8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. List of constructs which must contain a single space.
  10. Allowed values: a subset of ``['yield_from']``
  11. Default value: ``['yield_from']``
  12. ``constructs_followed_by_a_single_space``
  13. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. List of constructs which must be followed by a single space.
  15. Allowed values: a subset of ``['abstract', 'as', 'attribute', 'break', 'case', 'catch', 'class', 'clone', 'comment', 'const', 'const_import', 'continue', 'do', 'echo', 'else', 'elseif', 'enum', 'extends', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'match', 'named_argument', 'namespace', 'new', 'open_tag_with_echo', 'php_doc', 'php_open', 'print', 'private', 'protected', 'public', 'readonly', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'type_colon', 'use', 'use_lambda', 'use_trait', 'var', 'while', 'yield', 'yield_from']``
  16. Default value: ``['abstract', 'as', 'attribute', 'break', 'case', 'catch', 'class', 'clone', 'comment', 'const', 'const_import', 'continue', 'do', 'echo', 'else', 'elseif', 'enum', 'extends', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'match', 'named_argument', 'namespace', 'new', 'open_tag_with_echo', 'php_doc', 'php_open', 'print', 'private', 'protected', 'public', 'readonly', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'type_colon', 'use', 'use_lambda', 'use_trait', 'var', 'while', 'yield', 'yield_from']``
  17. ``constructs_preceded_by_a_single_space``
  18. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. List of constructs which must be preceded by a single space.
  20. Allowed values: a subset of ``['as', 'else', 'elseif', 'use_lambda']``
  21. Default value: ``['as', 'use_lambda']``
  22. Examples
  23. --------
  24. Example #1
  25. ~~~~~~~~~~
  26. *Default* configuration.
  27. .. code-block:: diff
  28. --- Original
  29. +++ New
  30. <?php
  31. -throw new \Exception();
  32. +throw new \Exception();
  33. Example #2
  34. ~~~~~~~~~~
  35. With configuration: ``['constructs_contain_a_single_space' => ['yield_from'], 'constructs_followed_by_a_single_space' => ['yield_from']]``.
  36. .. code-block:: diff
  37. --- Original
  38. +++ New
  39. <?php
  40. -function foo() { yield from baz(); }
  41. +function foo() { yield from baz(); }
  42. Example #3
  43. ~~~~~~~~~~
  44. With configuration: ``['constructs_preceded_by_a_single_space' => ['use_lambda'], 'constructs_followed_by_a_single_space' => ['use_lambda']]``.
  45. .. code-block:: diff
  46. --- Original
  47. +++ New
  48. <?php
  49. -$foo = function& ()use($bar) {
  50. +$foo = function& () use ($bar) {
  51. };
  52. Example #4
  53. ~~~~~~~~~~
  54. With configuration: ``['constructs_followed_by_a_single_space' => ['echo']]``.
  55. .. code-block:: diff
  56. --- Original
  57. +++ New
  58. <?php
  59. -echo "Hello!";
  60. +echo "Hello!";
  61. Example #5
  62. ~~~~~~~~~~
  63. With configuration: ``['constructs_followed_by_a_single_space' => ['yield_from']]``.
  64. .. code-block:: diff
  65. --- Original
  66. +++ New
  67. <?php
  68. -yield from baz();
  69. +yield from baz();
  70. Rule sets
  71. ---------
  72. The rule is part of the following rule sets:
  73. - `@PER <./../../ruleSets/PER.rst>`_ with config:
  74. ``['constructs_followed_by_a_single_space' => ['abstract', 'as', 'case', 'catch', 'class', 'const', 'const_import', 'do', 'else', 'elseif', 'enum', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'if', 'insteadof', 'interface', 'match', 'named_argument', 'namespace', 'new', 'private', 'protected', 'public', 'readonly', 'static', 'switch', 'trait', 'try', 'type_colon', 'use', 'use_lambda', 'while'], 'constructs_preceded_by_a_single_space' => ['as', 'else', 'elseif', 'use_lambda']]``
  75. - `@PER-CS <./../../ruleSets/PER-CS.rst>`_ with config:
  76. ``['constructs_followed_by_a_single_space' => ['abstract', 'as', 'case', 'catch', 'class', 'const', 'const_import', 'do', 'else', 'elseif', 'enum', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'if', 'insteadof', 'interface', 'match', 'named_argument', 'namespace', 'new', 'private', 'protected', 'public', 'readonly', 'static', 'switch', 'trait', 'try', 'type_colon', 'use', 'use_lambda', 'while'], 'constructs_preceded_by_a_single_space' => ['as', 'else', 'elseif', 'use_lambda']]``
  77. - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
  78. ``['constructs_followed_by_a_single_space' => ['abstract', 'as', 'case', 'catch', 'class', 'const_import', 'do', 'else', 'elseif', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'if', 'insteadof', 'interface', 'namespace', 'new', 'private', 'protected', 'public', 'static', 'switch', 'trait', 'try', 'use', 'use_lambda', 'while'], 'constructs_preceded_by_a_single_space' => ['as', 'else', 'elseif', 'use_lambda']]``
  79. - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
  80. ``['constructs_followed_by_a_single_space' => ['abstract', 'as', 'case', 'catch', 'class', 'const', 'const_import', 'do', 'else', 'elseif', 'enum', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'if', 'insteadof', 'interface', 'match', 'named_argument', 'namespace', 'new', 'private', 'protected', 'public', 'readonly', 'static', 'switch', 'trait', 'try', 'type_colon', 'use', 'use_lambda', 'while'], 'constructs_preceded_by_a_single_space' => ['as', 'else', 'elseif', 'use_lambda']]``
  81. - `@PSR2 <./../../ruleSets/PSR2.rst>`_ with config:
  82. ``['constructs_followed_by_a_single_space' => ['abstract', 'as', 'case', 'catch', 'class', 'do', 'else', 'elseif', 'final', 'for', 'foreach', 'function', 'if', 'interface', 'namespace', 'private', 'protected', 'public', 'static', 'switch', 'trait', 'try', 'use_lambda', 'while'], 'constructs_preceded_by_a_single_space' => ['as', 'else', 'elseif', 'use_lambda']]``
  83. - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
  84. ``['constructs_followed_by_a_single_space' => ['abstract', 'as', 'case', 'catch', 'class', 'const_import', 'do', 'else', 'elseif', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'if', 'insteadof', 'interface', 'namespace', 'new', 'private', 'protected', 'public', 'static', 'switch', 'trait', 'try', 'use', 'use_lambda', 'while'], 'constructs_preceded_by_a_single_space' => ['as', 'else', 'elseif', 'use_lambda']]``
  85. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
  86. - `@Symfony <./../../ruleSets/Symfony.rst>`_
  87. References
  88. ----------
  89. - Fixer class: `PhpCsFixer\\Fixer\\LanguageConstruct\\SingleSpaceAroundConstructFixer <./../../../src/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixer.php>`_
  90. - Test class: `PhpCsFixer\\Tests\\Fixer\\LanguageConstruct\\SingleSpaceAroundConstructFixerTest <./../../../tests/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixerTest.php>`_
  91. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.