config.rst 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ===========
  2. Config file
  3. ===========
  4. Instead of using command line options to customize rules and rule sets, you can save the
  5. project configuration in a ``.php-cs-fixer.dist.php`` file in the root directory of your project.
  6. The file must return an instance of `PhpCsFixer\\ConfigInterface <../src/ConfigInterface.php>`_
  7. which lets you configure the rules, the files and directories that
  8. need to be analyzed. You may also create ``.php-cs-fixer.php`` file, which is
  9. the local configuration that will be used instead of the project configuration. It
  10. is a good practice to add that file into your ``.gitignore`` file.
  11. With the ``--config`` option you can specify the path to the
  12. ``.php-cs-fixer.php`` file.
  13. The example below will add two rules to the default list of PSR12 set rules:
  14. .. code-block:: php
  15. <?php
  16. $finder = PhpCsFixer\Finder::create()
  17. ->exclude('somedir')
  18. ->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php')
  19. ->in(__DIR__)
  20. ;
  21. $config = new PhpCsFixer\Config();
  22. return $config->setRules([
  23. '@PSR12' => true,
  24. 'strict_param' => true,
  25. 'array_syntax' => ['syntax' => 'short'],
  26. ])
  27. ->setFinder($finder)
  28. ;
  29. **NOTE**: ``exclude`` will work only for directories, so if you need to exclude file, try ``notPath``.
  30. Both ``exclude`` and ``notPath`` methods accept only relative paths to the ones defined with the ``in`` method.
  31. See `Symfony\\Finder <https://symfony.com/doc/current/components/finder.html>`_
  32. online documentation for other `Finder` methods.
  33. You may also use an exclude list for the rules instead of the above shown include approach.
  34. The following example shows how to use all ``Symfony`` rules but the ``full_opening_tag`` rule.
  35. .. code-block:: php
  36. <?php
  37. $finder = PhpCsFixer\Finder::create()
  38. ->in(__DIR__)
  39. ->exclude('somedir')
  40. ;
  41. $config = new PhpCsFixer\Config();
  42. return $config->setRules([
  43. '@Symfony' => true,
  44. 'full_opening_tag' => false,
  45. ])
  46. ->setFinder($finder)
  47. ;
  48. You may want to use non-linux whitespaces in your project. Then you need to
  49. configure them in your config file.
  50. .. code-block:: php
  51. <?php
  52. $config = new PhpCsFixer\Config();
  53. return $config
  54. ->setIndent("\t")
  55. ->setLineEnding("\r\n")
  56. ;