class_definition.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. =========================
  2. Rule ``class_definition``
  3. =========================
  4. Whitespace around the keywords of a class, trait or interfaces definition should
  5. 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. Examples
  30. --------
  31. Example #1
  32. ~~~~~~~~~~
  33. *Default* configuration.
  34. .. code-block:: diff
  35. --- Original
  36. +++ New
  37. <?php
  38. -class Foo extends Bar implements Baz, BarBaz
  39. +class Foo extends Bar implements Baz, BarBaz
  40. {
  41. }
  42. -final class Foo extends Bar implements Baz, BarBaz
  43. +final class Foo extends Bar implements Baz, BarBaz
  44. {
  45. }
  46. -trait Foo
  47. +trait Foo
  48. {
  49. }
  50. -$foo = new class extends Bar implements Baz, BarBaz {};
  51. +$foo = new class extends Bar implements Baz, BarBaz {};
  52. Example #2
  53. ~~~~~~~~~~
  54. With configuration: ``['single_line' => true]``.
  55. .. code-block:: diff
  56. --- Original
  57. +++ New
  58. <?php
  59. -class Foo
  60. -extends Bar
  61. -implements Baz, BarBaz
  62. +class Foo extends Bar implements Baz, BarBaz
  63. {}
  64. Example #3
  65. ~~~~~~~~~~
  66. With configuration: ``['single_item_single_line' => true]``.
  67. .. code-block:: diff
  68. --- Original
  69. +++ New
  70. <?php
  71. -class Foo
  72. -extends Bar
  73. -implements Baz
  74. +class Foo extends Bar implements Baz
  75. {}
  76. Example #4
  77. ~~~~~~~~~~
  78. With configuration: ``['multi_line_extends_each_single_line' => true]``.
  79. .. code-block:: diff
  80. --- Original
  81. +++ New
  82. <?php
  83. interface Bar extends
  84. - Bar, BarBaz, FooBarBaz
  85. + Bar,
  86. + BarBaz,
  87. + FooBarBaz
  88. {}
  89. Example #5
  90. ~~~~~~~~~~
  91. With configuration: ``['space_before_parenthesis' => true]``.
  92. .. code-block:: diff
  93. --- Original
  94. +++ New
  95. <?php
  96. -$foo = new class(){};
  97. +$foo = new class () {};
  98. Rule sets
  99. ---------
  100. The rule is part of the following rule sets:
  101. @PSR12
  102. Using the `@PSR12 <./../../ruleSets/PSR12.rst>`_ rule set will enable the ``class_definition`` rule with the config below:
  103. ``['space_before_parenthesis' => true]``
  104. @PSR2
  105. Using the `@PSR2 <./../../ruleSets/PSR2.rst>`_ rule set will enable the ``class_definition`` rule with the default config.
  106. @PhpCsFixer
  107. Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``class_definition`` rule with the config below:
  108. ``['single_line' => true]``
  109. @Symfony
  110. Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``class_definition`` rule with the config below:
  111. ``['single_line' => true]``