usage.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. =====
  2. Usage
  3. =====
  4. The ``fix`` command
  5. -------------------
  6. The ``fix`` command tries to fix as much coding standards
  7. problems as possible.
  8. With config file created, you can run command as easy as:
  9. .. code-block:: console
  10. php php-cs-fixer.phar fix
  11. If you do not have config file, you can run following command to fix non-hidden, non-vendor/ PHP files with default ruleset @PSR12:
  12. .. code-block:: console
  13. php php-cs-fixer.phar fix .
  14. You can also fix files in parallel, utilising more CPU cores. You can do this by using config class that implements ``PhpCsFixer\Runner\Parallel\ParallelConfig\ParallelAwareConfigInterface``, and use ``setParallelConfig()`` method. Recommended way is to utilise auto-detecting parallel configuration:
  15. .. code-block:: php
  16. <?php
  17. return (new PhpCsFixer\Config())
  18. ->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
  19. ;
  20. However, in some case you may want to fine-tune parallelisation with explicit values (e.g. in environments where auto-detection does not work properly and suggests more cores than it should):
  21. .. code-block:: php
  22. <?php
  23. return (new PhpCsFixer\Config())
  24. ->setParallelConfig(new PhpCsFixer\Runner\Parallel\ParallelConfig(4, 20))
  25. ;
  26. You can limit process to given file or files in a given directory and its subdirectories:
  27. .. code-block:: console
  28. php php-cs-fixer.phar fix /path/to/dir
  29. php php-cs-fixer.phar fix /path/to/file
  30. By default ``--path-mode`` is set to ``override``, which means, that if you specify the path to a file or a directory via
  31. command arguments, then the paths provided to a ``Finder`` in config file will be ignored. You can also use ``--path-mode=intersection``,
  32. which will use the intersection of the paths from the config file and from the argument:
  33. .. code-block:: console
  34. php php-cs-fixer.phar fix --path-mode=intersection /path/to/dir
  35. The ``--format`` option for the output format. Supported formats are ``txt`` (default one), ``checkstyle``, ``gitlab``, ``json``, ``junit`` and ``xml``.
  36. NOTE: the output for the following formats are generated in accordance with schemas
  37. * ``checkstyle`` follows the common `"checkstyle" XML schema </doc/schemas/fix/checkstyle.xsd>`_
  38. * ``gitlab`` follows the `codeclimate JSON schema </doc/schemas/fix/codeclimate.json>`_
  39. * ``json`` follows the `own JSON schema </doc/schemas/fix/schema.json>`_
  40. * ``junit`` follows the `JUnit XML schema from Jenkins </doc/schemas/fix/junit-10.xsd>`_
  41. * ``xml`` follows the `own XML schema </doc/schemas/fix/xml.xsd>`_
  42. The ``--quiet`` Do not output any message.
  43. The ``--verbose`` option will show the applied rules. When using the ``txt`` format it will also display progress output (progress bar by default, but can be changed using ``--show-progress`` option).
  44. 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
  45. * ``-v``: verbose
  46. * ``-vv``: very verbose
  47. * ``-vvv``: debug
  48. The ``--rules`` option limits the rules to apply to the
  49. project:
  50. .. code-block:: console
  51. php php-cs-fixer.phar fix /path/to/project --rules=@PSR12
  52. By default the ``PSR12`` rules are used. If the ``--rules`` option is used rules from config files are ignored.
  53. The ``--rules`` option lets you choose the exact rules to apply (the rule names must be separated by a comma):
  54. .. code-block:: console
  55. php php-cs-fixer.phar fix /path/to/dir --rules=line_ending,full_opening_tag,indentation_type
  56. 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,
  57. using ``-name_of_fixer``:
  58. .. code-block:: console
  59. php php-cs-fixer.phar fix /path/to/dir --rules=-full_opening_tag,-indentation_type
  60. When using combinations of exact and exclude rules, applying exact rules along with above excluded results:
  61. .. code-block:: console
  62. php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_statement,strict_comparison
  63. Complete configuration for rules can be supplied using a ``json`` formatted string.
  64. .. code-block:: console
  65. php php-cs-fixer.phar fix /path/to/project --rules='{"concat_space": {"spacing": "none"}}'
  66. The ``--dry-run`` flag will run the fixer without making changes to your files (implicitly set when you use ``check`` command).
  67. The ``--sequential`` flag will enforce sequential analysis even if parallel config is provided.
  68. The ``--diff`` flag can be used to let the fixer output all the changes it makes in ``udiff`` format.
  69. The ``--allow-risky`` option (pass ``yes`` or ``no``) allows you to set whether risky rules may run. Default value is taken from config file.
  70. A rule is considered risky if it could change code behaviour. By default no risky rules are run.
  71. The ``--stop-on-violation`` flag stops the execution upon first file that needs to be fixed.
  72. The ``--show-progress`` option allows you to choose the way process progress is rendered:
  73. * ``none``: disables progress output;
  74. * ``dots``: 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;
  75. * ``bar``: single line progress output with number of files and calculated percentage. Similar to ``dots`` output, it has to evaluate files list twice;
  76. If the option is not provided, it defaults to ``bar`` unless a config file that disables output, or non-txt reporter is used, then it defaults to ``none``.
  77. .. code-block:: console
  78. php php-cs-fixer.phar fix --verbose --show-progress=dots
  79. The command can also read from standard input, in which case it won't
  80. automatically fix anything:
  81. .. code-block:: console
  82. cat foo.php | php php-cs-fixer.phar fix --diff -
  83. Finally, if you don't need BC kept on CLI level, you might use ``PHP_CS_FIXER_FUTURE_MODE`` to start using options that
  84. would be default in next MAJOR release and to forbid using deprecated configuration:
  85. .. code-block:: console
  86. PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer.phar fix -v --diff
  87. The ``--dry-run`` option displays the files that need to be
  88. fixed but without actually modifying them:
  89. .. code-block:: console
  90. php php-cs-fixer.phar fix /path/to/code --dry-run
  91. By using ``--using-cache`` option with ``yes`` or ``no`` you can set if the caching
  92. mechanism should be used.
  93. The ``check`` command
  94. ---------------------
  95. This command is a shorthand for ``fix --dry-run`` and offers all the options and arguments as ``fix`` command.
  96. The only difference is that ``check`` command won't apply any changes, but will only print analysis result.
  97. The ``list-files`` command
  98. --------------------------
  99. The ``list-files`` command will list all files which need fixing.
  100. .. code-block:: console
  101. php php-cs-fixer.phar list-files
  102. The ``--config`` option can be used, like in the ``fix`` command, to tell from which path a config file should be loaded.
  103. .. code-block:: console
  104. php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php
  105. The output is built in a form that its easy to use in combination with ``xargs`` command in a linux pipe.
  106. This can be useful e.g. in situations where the caching mechanism might not be available (CI, Docker) and distribute
  107. fixing across several processes might speedup the process.
  108. 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``.
  109. .. code-block:: console
  110. php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php | xargs -n 50 -P 8 php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php --path-mode intersection -v
  111. * ``-n`` defines how many files a single subprocess process
  112. * ``-P`` defines how many subprocesses the shell is allowed to spawn for parallel processing (usually similar to the number of CPUs your system has)
  113. Rule descriptions
  114. -----------------
  115. Use the following command to quickly understand what a rule will do to your code:
  116. .. code-block:: console
  117. php php-cs-fixer.phar describe align_multiline_comment
  118. To visualize all the rules that belong to a ruleset:
  119. .. code-block:: console
  120. php php-cs-fixer.phar describe @PSR2
  121. Caching
  122. -------
  123. The caching mechanism is enabled by default. This will speed up further runs by fixing only files that were modified
  124. since the last run. The tool will fix all files if the tool version has changed or the list of rules has changed.
  125. The cache is supported only when the tool was downloaded as a PHAR file, executed within pre-built Docker image
  126. or installed via Composer. The cache is written to the drive progressively, so do not be afraid of interruption -
  127. rerun the command and start where you left. The cache mechanism also supports executing the command in parallel.
  128. Cache can be disabled via ``--using-cache`` option or config file:
  129. .. code-block:: php
  130. <?php
  131. $config = new PhpCsFixer\Config();
  132. return $config->setUsingCache(false);
  133. Cache file can be specified via ``--cache-file`` option or config file:
  134. .. code-block:: php
  135. <?php
  136. $config = new PhpCsFixer\Config();
  137. return $config->setCacheFile(__DIR__.'/.php-cs-fixer.cache');
  138. Using PHP CS Fixer on CI
  139. ------------------------
  140. Require ``friendsofphp/php-cs-fixer`` as a ``dev`` dependency:
  141. .. code-block:: console
  142. ./composer.phar require --dev friendsofphp/php-cs-fixer
  143. Then, add the following command to your CI:
  144. .. code-block:: console
  145. IFS='
  146. '
  147. CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}")
  148. 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
  149. vendor/bin/php-cs-fixer check --config=.php-cs-fixer.dist.php -v --stop-on-violation --using-cache=no ${EXTRA_ARGS}
  150. Where ``$COMMIT_RANGE`` is your range of commits, e.g. ``${{github.event.before}}...${{github.event.after}}`` or ``HEAD~..HEAD``.
  151. GitLab Code Quality Integration
  152. ###############################
  153. If you want to integrate with GitLab's Code Quality feature, in order for report to contain correct line numbers, you
  154. will need to use both ``--format=gitlab`` and ``--diff`` arguments.
  155. Environment options
  156. -------------------
  157. The ``PHP_CS_FIXER_IGNORE_ENV`` environment variable can be used to ignore any environment requirements.
  158. This includes requirements like missing PHP extensions, unsupported PHP versions or by using HHVM.
  159. NOTE: Execution may be unstable when used.
  160. .. code-block:: console
  161. PHP_CS_FIXER_IGNORE_ENV=1 php php-cs-fixer.phar fix /path/to/dir
  162. Exit code
  163. ---------
  164. Exit code of the ``fix`` command is built using following bit flags:
  165. * 0 - OK.
  166. * 1 - General error (or PHP minimal requirement not matched).
  167. * 4 - Some files have invalid syntax (only in dry-run mode).
  168. * 8 - Some files need fixing (only in dry-run mode).
  169. * 16 - Configuration error of the application.
  170. * 32 - Configuration error of a Fixer.
  171. * 64 - Exception raised within the application.