usage.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. =====
  2. Usage
  3. =====
  4. The ``fix`` command
  5. -------------------
  6. The ``fix`` command tries to fix as much coding standards
  7. problems as possible on a given file or files in a given directory and its subdirectories:
  8. .. code-block:: console
  9. $ php php-cs-fixer.phar fix /path/to/dir
  10. $ php php-cs-fixer.phar fix /path/to/file
  11. By default ``--path-mode`` is set to ``override``, which means, that if you specify the path to a file or a directory via
  12. command arguments, then the paths provided to a ``Finder`` in config file will be ignored. You can also use ``--path-mode=intersection``,
  13. which will use the intersection of the paths from the config file and from the argument:
  14. .. code-block:: console
  15. $ php php-cs-fixer.phar fix --path-mode=intersection /path/to/dir
  16. The ``--format`` option for the output format. Supported formats are ``txt`` (default one), ``json``, ``xml``, ``checkstyle``, ``junit`` and ``gitlab``.
  17. NOTE: the output for the following formats are generated in accordance with schemas
  18. * ``checkstyle`` follows the common `"checkstyle" XML schema </doc/schemas/fix/checkstyle.xsd>`_
  19. * ``json`` follows the `own JSON schema </doc/schemas/fix/schema.json>`_
  20. * ``junit`` follows the `JUnit XML schema from Jenkins </doc/schemas/fix/junit-10.xsd>`_
  21. * ``xml`` follows the `own XML schema </doc/schemas/fix/xml.xsd>`_
  22. The ``--quiet`` Do not output any message.
  23. The ``--verbose`` option will show the applied rules. When using the ``txt`` format it will also display progress notifications.
  24. 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
  25. * ``-v``: verbose
  26. * ``-vv``: very verbose
  27. * ``-vvv``: debug
  28. The ``--rules`` option limits the rules to apply to the
  29. project:
  30. .. code-block:: console
  31. $ php php-cs-fixer.phar fix /path/to/project --rules=@PSR12
  32. By default the ``PSR12`` rules are used. If the ``--rules`` option is used rules from config files are ignored.
  33. The ``--rules`` option lets you choose the exact rules to apply (the rule names must be separated by a comma):
  34. .. code-block:: console
  35. $ php php-cs-fixer.phar fix /path/to/dir --rules=line_ending,full_opening_tag,indentation_type
  36. 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,
  37. using ``-name_of_fixer``:
  38. .. code-block:: console
  39. $ php php-cs-fixer.phar fix /path/to/dir --rules=-full_opening_tag,-indentation_type
  40. When using combinations of exact and exclude rules, applying exact rules along with above excluded results:
  41. .. code-block:: console
  42. $ php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_statement,strict_comparison
  43. Complete configuration for rules can be supplied using a ``json`` formatted string.
  44. .. code-block:: console
  45. $ php php-cs-fixer.phar fix /path/to/project --rules='{"concat_space": {"spacing": "none"}}'
  46. The ``--dry-run`` flag will run the fixer without making changes to your files.
  47. The ``--diff`` flag can be used to let the fixer output all the changes it makes in ``udiff`` 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. * ``dots``: same as ``estimating`` but using all terminal columns instead of default 80.
  54. If the option is not provided, it defaults to ``dots`` 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``.
  55. .. code-block:: console
  56. $ php php-cs-fixer.phar fix --verbose --show-progress=estimating
  57. The command can also read from standard input, in which case it won't
  58. automatically fix anything:
  59. .. code-block:: console
  60. $ cat foo.php | php php-cs-fixer.phar fix --diff -
  61. Finally, if you don't need BC kept on CLI level, you might use `PHP_CS_FIXER_FUTURE_MODE` to start using options that
  62. would be default in next MAJOR release and to forbid using deprecated configuration:
  63. .. code-block:: console
  64. $ PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer.phar fix -v --diff
  65. The ``--dry-run`` option displays the files that need to be
  66. fixed but without actually modifying them:
  67. .. code-block:: console
  68. $ php php-cs-fixer.phar fix /path/to/code --dry-run
  69. By using ``--using-cache`` option with ``yes`` or ``no`` you can set if the caching
  70. mechanism should be used.
  71. The ``list-files`` command
  72. --------------------------
  73. The ``list-files`` command will list all files which need fixing.
  74. .. code-block:: console
  75. $ php php-cs-fixer.phar list-files
  76. The ``--config`` option can be used, like in the ``fix`` command, to tell from which path a config file should be loaded.
  77. .. code-block:: console
  78. $ php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php
  79. The output is built in a form that its easy to use in combination with ``xargs`` command in a linux pipe.
  80. This can be useful e.g. in situations where the caching mechanism might not be available (CI, Docker) and distribute
  81. fixing across several processes might speedup the process.
  82. Note: You need to pass the config to the ``fix`` command, in order to make it work with several files being passed by ``list-files``.
  83. .. code-block:: console
  84. $ php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php | xargs -n 10 -P 8 php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php --path-mode intersection -v
  85. * ``-n`` defines how many files a single subprocess process
  86. * ``-P`` defines how many subprocesses the shell is allowed to spawn for parallel processing (usually similar to the number of CPUs your system has)
  87. Rule descriptions
  88. -----------------
  89. Use the following command to quickly understand what a rule will do to your code:
  90. .. code-block:: console
  91. $ php php-cs-fixer.phar describe align_multiline_comment
  92. To visualize all the rules that belong to a ruleset:
  93. .. code-block:: console
  94. $ php php-cs-fixer.phar describe @PSR2
  95. Caching
  96. -------
  97. The caching mechanism is enabled by default. This will speed up further runs by
  98. fixing only files that were modified since the last run. The tool will fix all
  99. files if the tool version has changed or the list of rules has changed.
  100. Cache is supported only for tool downloaded as phar file or installed via
  101. composer.
  102. Cache can be disabled via ``--using-cache`` option or config file:
  103. .. code-block:: php
  104. <?php
  105. $config = new PhpCsFixer\Config();
  106. return $config->setUsingCache(false);
  107. Cache file can be specified via ``--cache-file`` option or config file:
  108. .. code-block:: php
  109. <?php
  110. $config = new PhpCsFixer\Config();
  111. return $config->setCacheFile(__DIR__.'/.php-cs-fixer.cache');
  112. Using PHP CS Fixer on CI
  113. ------------------------
  114. Require ``friendsofphp/php-cs-fixer`` as a ``dev`` dependency:
  115. .. code-block:: console
  116. $ ./composer.phar require --dev friendsofphp/php-cs-fixer
  117. Then, add the following command to your CI:
  118. .. code-block:: console
  119. $ IFS='
  120. $ '
  121. $ CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}")
  122. $ if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php-cs-fixer(\\.dist)?\\.php|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi
  123. $ vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no ${EXTRA_ARGS}
  124. Where ``$COMMIT_RANGE`` is your range of commits, e.g. ``$TRAVIS_COMMIT_RANGE`` or ``HEAD~..HEAD``.
  125. Environment options
  126. -------------------
  127. The ``PHP_CS_FIXER_IGNORE_ENV`` environment variable can be used to ignore any environment requirements.
  128. This includes requirements like missing PHP extensions, unsupported PHP versions or by using HHVM.
  129. NOTE: Execution may be unstable when used.
  130. .. code-block:: console
  131. $ PHP_CS_FIXER_IGNORE_ENV=1 php php-cs-fixer.phar fix /path/to/dir
  132. Exit code
  133. ---------
  134. Exit code of the ``fix`` command is built using following bit flags:
  135. * 0 - OK.
  136. * 1 - General error (or PHP minimal requirement not matched).
  137. * 4 - Some files have invalid syntax (only in dry-run mode).
  138. * 8 - Some files need fixing (only in dry-run mode).
  139. * 16 - Configuration error of the application.
  140. * 32 - Configuration error of a Fixer.
  141. * 64 - Exception raised within the application.