config.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ===========
  2. Config file
  3. ===========
  4. No config
  5. ---------
  6. It is possible to *not* have the config file. In that case, the default rule set (@PSR12) will be applied, yet the path will need to be provided via CLI.
  7. .. code-block:: console
  8. php php-cs-fixer.phar fix .
  9. It is also possible to provide command line options to customize rules, yet instead of using them,
  10. it's recommended to save the project configuration in a ``.php-cs-fixer.dist.php`` file in the root directory of your project.
  11. The file must return an instance of `PhpCsFixer\\ConfigInterface <./../src/ConfigInterface.php>`_
  12. which lets you configure the rules, the files and directories that
  13. need to be analyzed. You may also create ``.php-cs-fixer.php`` file, which is
  14. the local configuration that will be used instead of the project configuration. It
  15. is a good practice to add that file into your ``.gitignore`` file.
  16. With the ``--config`` option you can specify the path to the config file.
  17. The simplest config
  18. -------------------
  19. The simplest config declares paths under control and rules to apply/check:
  20. .. code-block:: php
  21. <?php
  22. $finder = (new PhpCsFixer\Finder())
  23. ->in(__DIR__)
  24. ;
  25. return (new PhpCsFixer\Config())
  26. ->setRules([
  27. '@PER-CS' => true,
  28. '@PHP82Migration' => true,
  29. ])
  30. ->setFinder($finder)
  31. ;
  32. Default finder ignores ``__DIR__ . "/vendor"`` dir, "hidden" paths (ones starting with a dot) and VCS paths (e.g. ``.git``), and filter only for ``*.php`` files.
  33. Configuring paths
  34. -----------------
  35. The example below will manipulate which paths to fix or not:
  36. .. code-block:: php
  37. <?php
  38. $finder = (new PhpCsFixer\Finder())
  39. ->in(__DIR__)
  40. ->exclude([
  41. 'autogenerated_content',
  42. 'tests/fixtures',
  43. ])
  44. ->notPath([
  45. 'dump.php',
  46. 'src/exception_file.php',
  47. ])
  48. ;
  49. return (new PhpCsFixer\Config())
  50. ->setRules([
  51. '@PhpCsFixer' => true,
  52. ])
  53. ->setFinder($finder)
  54. ;
  55. Note that ``exclude`` will work only for directories, so if you need to exclude a file, use ``notPath``.
  56. Both ``exclude`` and ``notPath`` methods accept only relative paths to the ones defined with the ``in`` method, can be called multiple times and accept string or array of them.
  57. See `Symfony\\Finder <https://symfony.com/doc/current/components/finder.html#location>`_
  58. online documentation for other ``Finder`` methods.
  59. Configuring rules
  60. -----------------
  61. The example below will add two rules to the default list of PSR12 set rules:
  62. .. code-block:: php
  63. <?php
  64. $finder = (new PhpCsFixer\Finder())
  65. ->in(__DIR__)
  66. ;
  67. return (new PhpCsFixer\Config())
  68. ->setRules([
  69. '@PSR12' => true,
  70. 'strict_param' => true,
  71. 'array_syntax' => ['syntax' => 'short'],
  72. ])
  73. ->setFinder($finder)
  74. ;
  75. You may also use an exclude list for the rules instead of the above shown include approach.
  76. The following example shows how to use all ``PhpCsFixer`` rules but without the ``align_multiline_comment`` rule.
  77. .. code-block:: php
  78. <?php
  79. $finder = (new PhpCsFixer\Finder())
  80. ->in(__DIR__)
  81. ;
  82. return (new PhpCsFixer\Config())
  83. ->setRules([
  84. '@PhpCsFixer' => true,
  85. 'align_multiline_comment' => false,
  86. ])
  87. ->setFinder($finder)
  88. ;
  89. Configuring whitespaces
  90. -----------------------
  91. You may want to use non-linux whitespaces in your project. Then you need to
  92. configure them in your config file.
  93. .. code-block:: php
  94. <?php
  95. $finder = (new PhpCsFixer\Finder())
  96. ->in(__DIR__)
  97. ;
  98. return (new PhpCsFixer\Config())
  99. ->setRules([
  100. '@Symfony' => true,
  101. ])
  102. ->setFinder($finder)
  103. ->setIndent("\t")
  104. ->setLineEnding("\r\n")
  105. ;