config.rst 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.dist`` 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`` 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`` file.
  13. The example below will add two rules to the default list of PSR2 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. return PhpCsFixer\Config::create()
  22. ->setRules([
  23. '@PSR2' => 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. return PhpCsFixer\Config::create()
  42. ->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. return PhpCsFixer\Config::create()
  53. ->setIndent("\t")
  54. ->setLineEnding("\r\n")
  55. ;
  56. By using ``--using-cache`` option with ``yes`` or ``no`` you can set if the caching
  57. mechanism should be used.