usage.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. =====
  2. Usage
  3. =====
  4. The ``fix`` command tries to fix as much coding standards
  5. problems as possible on a given file or files in a given directory and its subdirectories:
  6. .. code-block:: console
  7. $ php php-cs-fixer.phar fix /path/to/dir
  8. $ php php-cs-fixer.phar fix /path/to/file
  9. By default ``--path-mode`` is set to ``override``, which means, that if you specify the path to a file or a directory via
  10. command arguments, then the paths provided to a ``Finder`` in config file will be ignored. You can use ``--path-mode=intersection``
  11. to merge paths from the config file and from the argument:
  12. .. code-block:: console
  13. $ php php-cs-fixer.phar fix --path-mode=intersection /path/to/dir
  14. The ``--format`` option for the output format. Supported formats are ``txt`` (default one), ``json``, ``xml``, ``checkstyle``, ``junit`` and ``gitlab``.
  15. NOTE: the output for the following formats are generated in accordance with XML schemas
  16. * ``checkstyle`` follows the common `"checkstyle" xml schema </doc/report-schema/checkstyle.xsd>`_
  17. * ``junit`` follows the `JUnit xml schema from Jenkins </doc/report-schema/junit-10.xsd>`_
  18. The ``--quiet`` Do not output any message.
  19. The ``--verbose`` option will show the applied rules. When using the ``txt`` format it will also display progress notifications.
  20. NOTE: if there is an error like "errors reported during linting after fixing", you can use this to be even more verbose for debugging purpose
  21. * ``--verbose=0`` or no option: normal
  22. * ``--verbose``, ``--verbose=1``, ``-v``: verbose
  23. * ``--verbose=2``, ``-vv``: very verbose
  24. * ``--verbose=3``, ``-vvv``: debug
  25. The ``--rules`` option limits the rules to apply to the
  26. project:
  27. .. code-block:: console
  28. $ php php-cs-fixer.phar fix /path/to/project --rules=@PSR2
  29. By default the ``PSR1`` and ``PSR2`` rules are used. If the ``--rules`` option is used rules from config files are ignored.
  30. The ``--rules`` option lets you choose the exact rules to apply (the rule names must be separated by a comma):
  31. .. code-block:: console
  32. $ php php-cs-fixer.phar fix /path/to/dir --rules=line_ending,full_opening_tag,indentation_type
  33. You can also exclude the rules you don't want by placing a dash in front of the rule name, if this is more convenient,
  34. using ``-name_of_fixer``:
  35. .. code-block:: console
  36. $ php php-cs-fixer.phar fix /path/to/dir --rules=-full_opening_tag,-indentation_type
  37. When using combinations of exact and exclude rules, applying exact rules along with above excluded results:
  38. .. code-block:: console
  39. $ php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_statement,strict_comparison
  40. Complete configuration for rules can be supplied using a ``json`` formatted string.
  41. .. code-block:: console
  42. $ php php-cs-fixer.phar fix /path/to/project --rules='{"concat_space": {"spacing": "none"}}'
  43. The ``--dry-run`` flag will run the fixer without making changes to your files.
  44. The ``--diff`` flag can be used to let the fixer output all the changes it makes.
  45. The ``--diff-format`` option allows to specify in which format the fixer should output the changes it makes:
  46. * ``udiff``: unified diff format;
  47. * ``sbd``: Sebastianbergmann/diff format (default when using `--diff` without specifying `diff-format`).
  48. The ``--allow-risky`` option (pass ``yes`` or ``no``) allows you to set whether risky rules may run. Default value is taken from config file.
  49. A rule is considered risky if it could change code behaviour. By default no risky rules are run.
  50. The ``--stop-on-violation`` flag stops the execution upon first file that needs to be fixed.
  51. The ``--show-progress`` option allows you to choose the way process progress is rendered:
  52. * ``none``: disables progress output;
  53. * ``run-in``: [deprecated] simple single-line progress output;
  54. * ``estimating``: [deprecated] multiline progress output with number of files and percentage on each line. Note that with this option, the files list is evaluated before processing to get the total number of files and then kept in memory to avoid using the file iterator twice. This has an impact on memory usage so using this option is not recommended on very large projects;
  55. * ``estimating-max``: [deprecated] same as ``dots``;
  56. * ``dots``: same as ``estimating`` but using all terminal columns instead of default 80.
  57. If the option is not provided, it defaults to ``run-in`` unless a config file that disables output is used, in which case it defaults to ``none``. This option has no effect if the verbosity of the command is less than ``verbose``.
  58. .. code-block:: console
  59. $ php php-cs-fixer.phar fix --verbose --show-progress=estimating
  60. The command can also read from standard input, in which case it won't
  61. automatically fix anything:
  62. .. code-block:: console
  63. $ cat foo.php | php php-cs-fixer.phar fix --diff -
  64. Finally, if you don't need BC kept on CLI level, you might use `PHP_CS_FIXER_FUTURE_MODE` to start using options that
  65. would be default in next MAJOR release (unified differ, estimating, full-width progress indicator):
  66. .. code-block:: console
  67. $ PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer.phar fix -v --diff
  68. The ``--dry-run`` option displays the files that need to be
  69. fixed but without actually modifying them:
  70. .. code-block:: console
  71. $ php php-cs-fixer.phar fix /path/to/code --dry-run
  72. By using ``--using-cache`` option with ``yes`` or ``no`` you can set if the caching
  73. mechanism should be used.
  74. Rule descriptions
  75. -----------------
  76. Use the following command to quickly understand what a rule will do to your code:
  77. .. code-block:: console
  78. $ php php-cs-fixer.phar describe align_multiline_comment
  79. To visualize all the rules that belong to a ruleset:
  80. .. code-block:: console
  81. $ php php-cs-fixer.phar describe @PSR2
  82. Caching
  83. -------
  84. The caching mechanism is enabled by default. This will speed up further runs by
  85. fixing only files that were modified since the last run. The tool will fix all
  86. files if the tool version has changed or the list of rules has changed.
  87. Cache is supported only for tool downloaded as phar file or installed via
  88. composer.
  89. Cache can be disabled via ``--using-cache`` option or config file:
  90. .. code-block:: php
  91. <?php
  92. return PhpCsFixer\Config::create()
  93. ->setUsingCache(false)
  94. ;
  95. Cache file can be specified via ``--cache-file`` option or config file:
  96. .. code-block:: php
  97. <?php
  98. return PhpCsFixer\Config::create()
  99. ->setCacheFile(__DIR__.'/.php_cs.cache')
  100. ;
  101. Using PHP CS Fixer on CI
  102. ------------------------
  103. Require ``friendsofphp/php-cs-fixer`` as a ``dev`` dependency:
  104. .. code-block:: console
  105. $ ./composer.phar require --dev friendsofphp/php-cs-fixer
  106. Then, add the following command to your CI:
  107. .. code-block:: console
  108. $ IFS='
  109. $ '
  110. $ CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}")
  111. $ if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi
  112. $ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no ${EXTRA_ARGS}
  113. Where ``$COMMIT_RANGE`` is your range of commits, e.g. ``$TRAVIS_COMMIT_RANGE`` or ``HEAD~..HEAD``.
  114. Exit code
  115. ---------
  116. Exit code of the ``fix`` command is built using following bit flags:
  117. * 0 - OK.
  118. * 1 - General error (or PHP minimal requirement not matched).
  119. * 4 - Some files have invalid syntax (only in dry-run mode).
  120. * 8 - Some files need fixing (only in dry-run mode).
  121. * 16 - Configuration error of the application.
  122. * 32 - Configuration error of a Fixer.
  123. * 64 - Exception raised within the application.