README.rst 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. PHP Coding Standards Fixer
  2. ==========================
  3. The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards;
  4. whether you want to follow PHP coding standards as defined in the PSR-1, PSR-2, etc.,
  5. or other community driven ones like the Symfony one.
  6. You can **also** define your (teams) style through configuration.
  7. It can modernize your code (like converting the ``pow`` function to the ``**`` operator on PHP 5.6)
  8. and (micro) optimize it.
  9. If you are already using a linter to identify coding standards problems in your
  10. code, you know that fixing them by hand is tedious, especially on large
  11. projects. This tool does not only detect them, but also fixes them for you.
  12. The PHP CS Fixer is maintained on GitHub at https://github.com/FriendsOfPHP/PHP-CS-Fixer
  13. bug reports and ideas about new features are welcome there.
  14. You can talk to us at https://gitter.im/PHP-CS-Fixer/Lobby about the project,
  15. configuration, possible improvements, ideas and questions, please visit us!
  16. Requirements
  17. ------------
  18. PHP needs to be a minimum version of PHP 5.3.6.
  19. Installation
  20. ------------
  21. Locally
  22. ~~~~~~~
  23. Download the `php-cs-fixer.phar`_ file and store it somewhere on your computer.
  24. Globally (manual)
  25. ~~~~~~~~~~~~~~~~~
  26. You can run these commands to easily access latest ``php-cs-fixer`` from anywhere on
  27. your system:
  28. .. code-block:: bash
  29. $ wget http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -O php-cs-fixer
  30. or with specified version:
  31. .. code-block:: bash
  32. $ wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.2.16/php-cs-fixer.phar -O php-cs-fixer
  33. or with curl:
  34. .. code-block:: bash
  35. $ curl -L http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -o php-cs-fixer
  36. then:
  37. .. code-block:: bash
  38. $ sudo chmod a+x php-cs-fixer
  39. $ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
  40. Then, just run ``php-cs-fixer``.
  41. Globally (Composer)
  42. ~~~~~~~~~~~~~~~~~~~
  43. To install PHP CS Fixer, `install Composer <https://getcomposer.org/download/>`_ and issue the following command:
  44. .. code-block:: bash
  45. $ composer global require friendsofphp/php-cs-fixer
  46. Then make sure you have the global Composer binaries directory in your ``PATH``. This directory is platform-dependent, see `Composer documentation <https://getcomposer.org/doc/03-cli.md#composer-home>`_ for details. Example for some Unix systems:
  47. .. code-block:: bash
  48. $ export PATH="$PATH:$HOME/.composer/vendor/bin"
  49. Globally (homebrew)
  50. ~~~~~~~~~~~~~~~~~~~
  51. PHP-CS-Fixer is part of the homebrew-php project. Follow the installation
  52. instructions at https://github.com/homebrew/homebrew-php if you don't
  53. already have it.
  54. .. code-block:: bash
  55. $ brew install homebrew/php/php-cs-fixer
  56. Update
  57. ------
  58. Locally
  59. ~~~~~~~
  60. The ``self-update`` command tries to update ``php-cs-fixer`` itself:
  61. .. code-block:: bash
  62. $ php php-cs-fixer.phar self-update
  63. Globally (manual)
  64. ~~~~~~~~~~~~~~~~~
  65. You can update ``php-cs-fixer`` through this command:
  66. .. code-block:: bash
  67. $ sudo php-cs-fixer self-update
  68. Globally (Composer)
  69. ~~~~~~~~~~~~~~~~~~~
  70. You can update ``php-cs-fixer`` through this command:
  71. .. code-block:: bash
  72. $ ./composer.phar global update friendsofphp/php-cs-fixer
  73. Globally (homebrew)
  74. ~~~~~~~~~~~~~~~~~~~
  75. You can update ``php-cs-fixer`` through this command:
  76. .. code-block:: bash
  77. $ brew upgrade php-cs-fixer
  78. Usage
  79. -----
  80. The ``fix`` command tries to fix as much coding standards
  81. problems as possible on a given file or files in a given directory and its subdirectories:
  82. .. code-block:: bash
  83. $ php php-cs-fixer.phar fix /path/to/dir
  84. $ php php-cs-fixer.phar fix /path/to/file
  85. By default ``--path-mode`` is set to ``override``, which means, that if you specify the path to a file or a directory via
  86. command arguments, then the paths provided to a ``Finder`` in config file will be ignored. You can use ``--path-mode=intersection``
  87. to merge paths from the config file and from the argument:
  88. .. code-block:: bash
  89. $ php php-cs-fixer.phar fix --path-mode=intersection /path/to/dir
  90. The ``--format`` option for the output format. Supported formats are ``txt`` (default one), ``json``, ``xml`` and ``junit``.
  91. NOTE: When using ``junit`` format report generates in accordance with JUnit xml schema from Jenkins (see docs/junit-10.xsd).
  92. The ``--verbose`` option will show the applied rules. When using the ``txt`` format it will also displays progress notifications.
  93. The ``--rules`` option limits the rules to apply on the
  94. project:
  95. .. code-block:: bash
  96. $ php php-cs-fixer.phar fix /path/to/project --rules=@PSR2
  97. By default the PSR1 and PSR2 rules are used.
  98. The ``--rules`` option lets you choose the exact rules to
  99. apply (the rule names must be separated by a comma):
  100. .. code-block:: bash
  101. $ php php-cs-fixer.phar fix /path/to/dir --rules=line_ending,full_opening_tag,indentation_type
  102. You can also blacklist the rules you don't want by placing a dash in front of the rule name, if this is more convenient,
  103. using ``-name_of_fixer``:
  104. .. code-block:: bash
  105. $ php php-cs-fixer.phar fix /path/to/dir --rules=-full_opening_tag,-indentation_type
  106. When using combinations of exact and blacklist rules, applying exact rules along with above blacklisted results:
  107. .. code-block:: bash
  108. $ php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_return,strict_comparison
  109. Complete configuration for rules can be supplied using a ``json`` formatted string.
  110. .. code-block:: bash
  111. $ php php-cs-fixer.phar fix /path/to/project --rules='{"concat_space": {"spacing": "none"}}'
  112. A combination of ``--dry-run`` and ``--diff`` will
  113. display a summary of proposed fixes, leaving your files unchanged.
  114. The ``--allow-risky`` option (pass ``yes`` or ``no``) allows you to set whether risky rules may run. Default value is taken from config file.
  115. Risky rule is a rule, which could change code behaviour. By default no risky rules are run.
  116. The ``--stop-on-violation`` flag stops execution upon first file that needs to be fixed.
  117. The ``--show-progress`` option allows you to choose the way process progress is rendered:
  118. * ``none``: disables progress output;
  119. * ``run-in``: simple single-line progress output;
  120. * ``estimating``: 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.
  121. 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``.
  122. .. code-block:: bash
  123. $ php php-cs-fixer.phar fix --verbose --show-progress=estimating
  124. The command can also read from standard input, in which case it won't
  125. automatically fix anything:
  126. .. code-block:: bash
  127. $ cat foo.php | php php-cs-fixer.phar fix --diff -
  128. Choose from the list of available rules:
  129. * **array_syntax**
  130. PHP arrays should be declared using the configured syntax (requires PHP
  131. >= 5.4 for short syntax).
  132. Configuration options:
  133. - ``syntax`` (``'long'``, ``'short'``): whether to use the ``long`` or ``short`` array
  134. syntax; defaults to ``'long'``
  135. * **binary_operator_spaces** [@Symfony]
  136. Binary operators should be surrounded by at least one space.
  137. Configuration options:
  138. - ``align_double_arrow`` (``false``, ``null``, ``true``): whether to apply, remove or
  139. ignore double arrows alignment; defaults to ``false``
  140. - ``align_equals`` (``false``, ``null``, ``true``): whether to apply, remove or ignore
  141. equals alignment; defaults to ``false``
  142. * **blank_line_after_namespace** [@PSR2, @Symfony]
  143. There MUST be one blank line after the namespace declaration.
  144. * **blank_line_after_opening_tag** [@Symfony]
  145. Ensure there is no code on the same line as the PHP open tag and it is
  146. followed by a blank line.
  147. * **blank_line_before_return** [@Symfony]
  148. An empty line feed should precede a return statement.
  149. * **braces** [@PSR2, @Symfony]
  150. The body of each structure MUST be enclosed by braces. Braces should be
  151. properly placed. Body of braces should be properly indented.
  152. Configuration options:
  153. - ``allow_single_line_closure`` (``bool``): whether single line lambda notation
  154. should be allowed; defaults to ``false``
  155. - ``position_after_functions_and_oop_constructs`` (``'next'``, ``'same'``): whether
  156. the opening brace should be placed on "next" or "same" line after
  157. classy constructs (non-anonymous classes, interfaces, traits, methods
  158. and non-lambda functions); defaults to ``'next'``
  159. * **cast_spaces** [@Symfony]
  160. A single space should be between cast and variable.
  161. * **class_definition** [@PSR2, @Symfony]
  162. Whitespace around the keywords of a class, trait or interfaces
  163. definition should be one space.
  164. Configuration options:
  165. - ``multiLineExtendsEachSingleLine`` (``bool``): whether definitions should be
  166. multiline; defaults to ``false``
  167. - ``singleItemSingleLine`` (``bool``): whether definitions should be single line
  168. when including a single item; defaults to ``false``
  169. - ``singleLine`` (``bool``): whether definitions should be single line; defaults
  170. to ``false``
  171. * **class_keyword_remove**
  172. Converts ``::class`` keywords to FQCN strings. Requires PHP >= 5.5.
  173. * **combine_consecutive_unsets**
  174. Calling ``unset`` on multiple items should be done in one call.
  175. * **concat_space** [@Symfony]
  176. Concatenation should be spaced according configuration.
  177. Configuration options:
  178. - ``spacing`` (``'none'``, ``'one'``): spacing to apply around concatenation operator;
  179. defaults to ``'none'``
  180. * **declare_equal_normalize** [@Symfony]
  181. Equal sign in declare statement should be surrounded by spaces or not
  182. following configuration.
  183. Configuration options:
  184. - ``space`` (``'none'``, ``'single'``): spacing to apply around the equal sign;
  185. defaults to ``'none'``
  186. * **declare_strict_types** [@PHP70Migration:risky]
  187. Force strict types declaration in all files. Requires PHP >= 7.0.
  188. *Risky rule: forcing strict types will stop non strict code from working.*
  189. * **dir_constant** [@Symfony:risky]
  190. Replaces ``dirname(__FILE__)`` expression with equivalent ``__DIR__``
  191. constant.
  192. *Risky rule: risky when the function ``dirname`` is overridden.*
  193. * **doctrine_annotation_braces**
  194. Doctrine annotations without arguments must use the configured syntax.
  195. Configuration options:
  196. - ``ignored_tags`` (``array``): list of tags that must not be treated as Doctrine
  197. Annotations; defaults to ``['abstract', 'access', 'code', 'deprec',
  198. 'encode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc',
  199. 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar',
  200. 'staticVar', 'throw', 'api', 'author', 'category', 'copyright',
  201. 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal',
  202. 'license', 'link', 'method', 'package', 'param', 'property',
  203. 'property-read', 'property-write', 'return', 'see', 'since', 'source',
  204. 'subpackage', 'throws', 'todo', 'TODO', 'usedBy', 'uses', 'var',
  205. 'version', 'after', 'afterClass', 'backupGlobals',
  206. 'backupStaticAttributes', 'before', 'beforeClass',
  207. 'codeCoverageIgnore', 'codeCoverageIgnoreStart',
  208. 'codeCoverageIgnoreEnd', 'covers', 'coversDefaultClass',
  209. 'coversNothing', 'dataProvider', 'depends', 'expectedException',
  210. 'expectedExceptionCode', 'expectedExceptionMessage',
  211. 'expectedExceptionMessageRegExp', 'group', 'large', 'medium',
  212. 'preserveGlobalState', 'requires', 'runTestsInSeparateProcesses',
  213. 'runInSeparateProcess', 'small', 'test', 'testdox', 'ticket', 'uses',
  214. 'SuppressWarnings', 'noinspection', 'package_version', 'enduml',
  215. 'startuml', 'fix', 'FIXME', 'fixme', 'override']``
  216. - ``syntax`` (``'with_braces'``, ``'without_braces'``): whether to add or remove
  217. braces; defaults to ``'without_braces'``
  218. * **doctrine_annotation_indentation**
  219. Doctrine annotations must be indented with four spaces.
  220. Configuration options:
  221. - ``ignored_tags`` (``array``): list of tags that must not be treated as Doctrine
  222. Annotations; defaults to ``['abstract', 'access', 'code', 'deprec',
  223. 'encode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc',
  224. 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar',
  225. 'staticVar', 'throw', 'api', 'author', 'category', 'copyright',
  226. 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal',
  227. 'license', 'link', 'method', 'package', 'param', 'property',
  228. 'property-read', 'property-write', 'return', 'see', 'since', 'source',
  229. 'subpackage', 'throws', 'todo', 'TODO', 'usedBy', 'uses', 'var',
  230. 'version', 'after', 'afterClass', 'backupGlobals',
  231. 'backupStaticAttributes', 'before', 'beforeClass',
  232. 'codeCoverageIgnore', 'codeCoverageIgnoreStart',
  233. 'codeCoverageIgnoreEnd', 'covers', 'coversDefaultClass',
  234. 'coversNothing', 'dataProvider', 'depends', 'expectedException',
  235. 'expectedExceptionCode', 'expectedExceptionMessage',
  236. 'expectedExceptionMessageRegExp', 'group', 'large', 'medium',
  237. 'preserveGlobalState', 'requires', 'runTestsInSeparateProcesses',
  238. 'runInSeparateProcess', 'small', 'test', 'testdox', 'ticket', 'uses',
  239. 'SuppressWarnings', 'noinspection', 'package_version', 'enduml',
  240. 'startuml', 'fix', 'FIXME', 'fixme', 'override']``
  241. * **doctrine_annotation_spaces**
  242. Fixes spaces in Doctrine annotations.
  243. Configuration options:
  244. - ``around_argument_assignments`` (``bool``): whether to fix spaces around
  245. argument assignment operator; defaults to ``true``
  246. - ``around_array_assignments`` (``bool``): whether to fix spaces around array
  247. assignment operators; defaults to ``true``
  248. - ``around_commas`` (``bool``): whether to fix spaces around commas; defaults to
  249. ``true``
  250. - ``around_parentheses`` (``bool``): whether to fix spaces around parentheses;
  251. defaults to ``true``
  252. - ``ignored_tags`` (``array``): list of tags that must not be treated as Doctrine
  253. Annotations; defaults to ``['abstract', 'access', 'code', 'deprec',
  254. 'encode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc',
  255. 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar',
  256. 'staticVar', 'throw', 'api', 'author', 'category', 'copyright',
  257. 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal',
  258. 'license', 'link', 'method', 'package', 'param', 'property',
  259. 'property-read', 'property-write', 'return', 'see', 'since', 'source',
  260. 'subpackage', 'throws', 'todo', 'TODO', 'usedBy', 'uses', 'var',
  261. 'version', 'after', 'afterClass', 'backupGlobals',
  262. 'backupStaticAttributes', 'before', 'beforeClass',
  263. 'codeCoverageIgnore', 'codeCoverageIgnoreStart',
  264. 'codeCoverageIgnoreEnd', 'covers', 'coversDefaultClass',
  265. 'coversNothing', 'dataProvider', 'depends', 'expectedException',
  266. 'expectedExceptionCode', 'expectedExceptionMessage',
  267. 'expectedExceptionMessageRegExp', 'group', 'large', 'medium',
  268. 'preserveGlobalState', 'requires', 'runTestsInSeparateProcesses',
  269. 'runInSeparateProcess', 'small', 'test', 'testdox', 'ticket', 'uses',
  270. 'SuppressWarnings', 'noinspection', 'package_version', 'enduml',
  271. 'startuml', 'fix', 'FIXME', 'fixme', 'override']``
  272. * **elseif** [@PSR2, @Symfony]
  273. The keyword ``elseif`` should be used instead of ``else if`` so that all
  274. control keywords look like single words.
  275. * **encoding** [@PSR1, @PSR2, @Symfony]
  276. PHP code MUST use only UTF-8 without BOM (remove BOM).
  277. * **ereg_to_preg** [@Symfony:risky]
  278. Replace deprecated ``ereg`` regular expression functions with preg.
  279. *Risky rule: risky if the ``ereg`` function is overridden.*
  280. * **full_opening_tag** [@PSR1, @PSR2, @Symfony]
  281. PHP code must use the long ``<?php`` tags or short-echo ``<?=`` tags and not
  282. other tag variations.
  283. * **function_declaration** [@PSR2, @Symfony]
  284. Spaces should be properly placed in a function declaration.
  285. Configuration options:
  286. - ``closure_function_spacing`` (``'none'``, ``'one'``): spacing to use before open
  287. parenthesis for closures; defaults to ``'one'``
  288. * **function_to_constant** [@Symfony:risky]
  289. Replace core functions calls returning constants with the constants.
  290. *Risky rule: risky when any of the configured functions to replace are overridden.*
  291. Configuration options:
  292. - ``functions`` (``array``): list of function names to fix; defaults to
  293. ``['phpversion', 'php_sapi_name', 'pi']``
  294. * **function_typehint_space** [@Symfony]
  295. Add missing space between function's argument and its typehint.
  296. * **general_phpdoc_annotation_remove**
  297. Configured annotations should be omitted from phpdocs.
  298. Configuration options:
  299. - ``annotations`` (``array``): list of annotations to remove, e.g. ``["author"]``;
  300. defaults to ``[]``
  301. * **hash_to_slash_comment** [@Symfony]
  302. Single line comments should use double slashes ``//`` and not hash ``#``.
  303. * **header_comment**
  304. Add, replace or remove header comment.
  305. Configuration options:
  306. - ``commentType`` (``'comment'``, ``'PHPDoc'``): comment syntax type; defaults to
  307. ``'comment'``
  308. - ``header`` (``string``): proper header content; required
  309. - ``location`` (``'after_declare_strict'``, ``'after_open'``): the location of the
  310. inserted header; defaults to ``'after_declare_strict'``
  311. - ``separate`` (``'both'``, ``'bottom'``, ``'none'``, ``'top'``): whether the header should be
  312. separated from the file content with a new line; defaults to ``'both'``
  313. * **heredoc_to_nowdoc**
  314. Convert ``heredoc`` to ``nowdoc`` where possible.
  315. * **include** [@Symfony]
  316. Include/Require and file path should be divided with a single space.
  317. File path should not be placed under brackets.
  318. * **indentation_type** [@PSR2, @Symfony]
  319. Code MUST use configured indentation type.
  320. * **is_null** [@Symfony:risky]
  321. Replaces ``is_null($var)`` expression with ``null === $var``.
  322. *Risky rule: risky when the function ``is_null`` is overridden.*
  323. Configuration options:
  324. - ``use_yoda_style`` (``bool``): whether Yoda style conditions should be used;
  325. defaults to ``true``
  326. * **line_ending** [@PSR2, @Symfony]
  327. All PHP files must use same line ending.
  328. * **linebreak_after_opening_tag**
  329. Ensure there is no code on the same line as the PHP open tag.
  330. * **lowercase_cast** [@Symfony]
  331. Cast should be written in lower case.
  332. * **lowercase_constants** [@PSR2, @Symfony]
  333. The PHP constants ``true``, ``false``, and ``null`` MUST be in lower case.
  334. * **lowercase_keywords** [@PSR2, @Symfony]
  335. PHP keywords MUST be in lower case.
  336. * **magic_constant_casing** [@Symfony]
  337. Magic constants should be referred to using the correct casing.
  338. * **mb_str_functions**
  339. Replace non multibyte-safe functions with corresponding mb function.
  340. *Risky rule: risky when any of the functions are overridden.*
  341. * **method_argument_space** [@PSR2, @Symfony]
  342. In method arguments and method call, there MUST NOT be a space before
  343. each comma and there MUST be one space after each comma.
  344. Configuration options:
  345. - ``keep_multiple_spaces_after_comma`` (``bool``): whether keep multiple spaces
  346. after comma; defaults to ``false``
  347. * **method_separation** [@Symfony]
  348. Methods must be separated with one blank line.
  349. * **modernize_types_casting** [@Symfony:risky]
  350. Replaces ``intval``, ``floatval``, ``doubleval``, ``strval`` and ``boolval``
  351. function calls with according type casting operator.
  352. *Risky rule: risky if any of the functions ``intval``, ``floatval``, ``doubleval``, ``strval`` or ``boolval`` are overridden.*
  353. * **native_function_casing** [@Symfony]
  354. Function defined by PHP should be called using the correct casing.
  355. * **native_function_invocation**
  356. Add leading ``\`` before function invocation of internal function to speed
  357. up resolving.
  358. *Risky rule: risky when any of the functions are overridden.*
  359. Configuration options:
  360. - ``exclude`` (``array``): list of functions to ignore; defaults to ``[]``
  361. * **new_with_braces** [@Symfony]
  362. All instances created with new keyword must be followed by braces.
  363. * **no_alias_functions** [@Symfony:risky]
  364. Master functions shall be used instead of aliases.
  365. *Risky rule: risky when any of the alias functions are overridden.*
  366. * **no_blank_lines_after_class_opening** [@Symfony]
  367. There should be no empty lines after class opening brace.
  368. * **no_blank_lines_after_phpdoc** [@Symfony]
  369. There should not be blank lines between docblock and the documented
  370. element.
  371. * **no_blank_lines_before_namespace**
  372. There should be no blank lines before a namespace declaration.
  373. * **no_closing_tag** [@PSR2, @Symfony]
  374. The closing ``?>`` tag MUST be omitted from files containing only PHP.
  375. * **no_empty_comment** [@Symfony]
  376. There should not be any empty comments.
  377. * **no_empty_phpdoc** [@Symfony]
  378. There should not be empty PHPDoc blocks.
  379. * **no_empty_statement** [@Symfony]
  380. Remove useless semicolon statements.
  381. * **no_extra_consecutive_blank_lines** [@Symfony]
  382. Removes extra blank lines and/or blank lines following configuration.
  383. Configuration options:
  384. - ``tokens`` (``array``): list of tokens to fix; defaults to ``['extra']``
  385. * **no_leading_import_slash** [@Symfony]
  386. Remove leading slashes in use clauses.
  387. * **no_leading_namespace_whitespace** [@Symfony]
  388. The namespace declaration line shouldn't contain leading whitespace.
  389. * **no_mixed_echo_print** [@Symfony]
  390. Either language construct ``print`` or ``echo`` should be used.
  391. Configuration options:
  392. - ``use`` (``'echo'``, ``'print'``): the desired language construct; defaults to
  393. ``'echo'``
  394. * **no_multiline_whitespace_around_double_arrow** [@Symfony]
  395. Operator ``=>`` should not be surrounded by multi-line whitespaces.
  396. * **no_multiline_whitespace_before_semicolons**
  397. Multi-line whitespace before closing semicolon are prohibited.
  398. * **no_php4_constructor**
  399. Convert PHP4-style constructors to ``__construct``.
  400. *Risky rule: risky when old style constructor being fixed is overridden or overrides parent one.*
  401. * **no_short_bool_cast** [@Symfony]
  402. Short cast ``bool`` using double exclamation mark should not be used.
  403. * **no_short_echo_tag**
  404. Replace short-echo ``<?=`` with long format ``<?php echo`` syntax.
  405. * **no_singleline_whitespace_before_semicolons** [@Symfony]
  406. Single-line whitespace before closing semicolon are prohibited.
  407. * **no_spaces_after_function_name** [@PSR2, @Symfony]
  408. When making a method or function call, there MUST NOT be a space between
  409. the method or function name and the opening parenthesis.
  410. * **no_spaces_around_offset** [@Symfony]
  411. There MUST NOT be spaces around offset braces.
  412. Configuration options:
  413. - ``positions`` (``array``): whether spacing should be fixed inside and/or outside
  414. the offset braces; defaults to ``['inside', 'outside']``
  415. * **no_spaces_inside_parenthesis** [@PSR2, @Symfony]
  416. There MUST NOT be a space after the opening parenthesis. There MUST NOT
  417. be a space before the closing parenthesis.
  418. * **no_trailing_comma_in_list_call** [@Symfony]
  419. Remove trailing commas in list function calls.
  420. * **no_trailing_comma_in_singleline_array** [@Symfony]
  421. PHP single-line arrays should not have trailing comma.
  422. * **no_trailing_whitespace** [@PSR2, @Symfony]
  423. Remove trailing whitespace at the end of non-blank lines.
  424. * **no_trailing_whitespace_in_comment** [@PSR2, @Symfony]
  425. There MUST be no trailing spaces inside comments and phpdocs.
  426. * **no_unneeded_control_parentheses** [@Symfony]
  427. Removes unneeded parentheses around control statements.
  428. Configuration options:
  429. - ``statements`` (``array``): list of control statements to fix; defaults to
  430. ``['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case',
  431. 'yield']``
  432. * **no_unreachable_default_argument_value**
  433. In function arguments there must not be arguments with default values
  434. before non-default ones.
  435. *Risky rule: modifies the signature of functions; therefore risky when using systems (such as some Symfony components) that rely on those (for example through reflection).*
  436. * **no_unused_imports** [@Symfony]
  437. Unused use statements must be removed.
  438. * **no_useless_else**
  439. There should not be useless ``else`` cases.
  440. * **no_useless_return**
  441. There should not be an empty return statement at the end of a function.
  442. * **no_whitespace_before_comma_in_array** [@Symfony]
  443. In array declaration, there MUST NOT be a whitespace before each comma.
  444. * **no_whitespace_in_blank_line** [@Symfony]
  445. Remove trailing whitespace at the end of blank lines.
  446. * **non_printable_character** [@Symfony:risky]
  447. Remove Zero-width space (ZWSP), Non-breaking space (NBSP) and other
  448. invisible unicode symbols.
  449. *Risky rule: risky when strings contain intended invisible characters.*
  450. * **normalize_index_brace** [@Symfony]
  451. Array index should always be written by using square braces.
  452. * **not_operator_with_space**
  453. Logical NOT operators (``!``) should have leading and trailing
  454. whitespaces.
  455. * **not_operator_with_successor_space**
  456. Logical NOT operators (``!``) should have one trailing whitespace.
  457. * **object_operator_without_whitespace** [@Symfony]
  458. There should not be space before or after object ``T_OBJECT_OPERATOR``
  459. ``->``.
  460. * **ordered_class_elements**
  461. Orders the elements of classes/interfaces/traits.
  462. Configuration options:
  463. - ``order`` (``array``): list of strings defining order of elements; defaults to
  464. ``['use_trait', 'constant_public', 'constant_protected',
  465. 'constant_private', 'property_public', 'property_protected',
  466. 'property_private', 'construct', 'destruct', 'magic', 'phpunit',
  467. 'method_public', 'method_protected', 'method_private']``
  468. * **ordered_imports**
  469. Ordering use statements.
  470. Configuration options:
  471. - ``importsOrder`` (``array``, ``null``): defines the order of import types; defaults
  472. to ``null``
  473. - ``sortAlgorithm`` (``'alpha'``, ``'length'``): whether the statements should be
  474. sorted alphabetically or by length; defaults to ``'alpha'``
  475. * **php_unit_construct** [@Symfony:risky]
  476. PHPUnit assertion method calls like "->assertSame(true, $foo)" should be
  477. written with dedicated method like "->assertTrue($foo)".
  478. *Risky rule: fixer could be risky if one is overriding PHPUnit's native methods.*
  479. Configuration options:
  480. - ``assertions`` (``array``): list of assertion methods to fix; defaults to
  481. ``['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame']``
  482. * **php_unit_dedicate_assert** [@Symfony:risky]
  483. PHPUnit assertions like "assertInternalType", "assertFileExists", should
  484. be used over "assertTrue".
  485. *Risky rule: fixer could be risky if one is overriding PHPUnit's native methods.*
  486. Configuration options:
  487. - ``functions`` (``array``): list of assertions to fix; defaults to
  488. ``['array_key_exists', 'empty', 'file_exists', 'is_infinite', 'is_nan',
  489. 'is_null', 'is_array', 'is_bool', 'is_callable', 'is_double',
  490. 'is_float', 'is_int', 'is_integer', 'is_long', 'is_numeric',
  491. 'is_object', 'is_real', 'is_resource', 'is_scalar', 'is_string']``
  492. * **php_unit_fqcn_annotation** [@Symfony]
  493. PHPUnit annotations should be a FQCNs including a root namespace.
  494. * **php_unit_strict**
  495. PHPUnit methods like ``assertSame`` should be used instead of
  496. ``assertEquals``.
  497. *Risky rule: risky when any of the functions are overridden.*
  498. Configuration options:
  499. - ``assertions`` (``array``): list of assertion methods to fix; defaults to
  500. ``['assertAttributeEquals', 'assertAttributeNotEquals', 'assertEquals',
  501. 'assertNotEquals']``
  502. * **phpdoc_add_missing_param_annotation**
  503. Phpdoc should contain @param for all params.
  504. Configuration options:
  505. - ``only_untyped`` (``bool``): whether to add missing ``@param`` annotations for
  506. untyped parameters only; defaults to ``true``
  507. * **phpdoc_align** [@Symfony]
  508. All items of the @param, @throws, @return, @var, and @type phpdoc tags
  509. must be aligned vertically.
  510. * **phpdoc_annotation_without_dot** [@Symfony]
  511. Phpdocs annotation descriptions should not be a sentence.
  512. * **phpdoc_indent** [@Symfony]
  513. Docblocks should have the same indentation as the documented subject.
  514. * **phpdoc_inline_tag** [@Symfony]
  515. Fix phpdoc inline tags, make inheritdoc always inline.
  516. * **phpdoc_no_access** [@Symfony]
  517. @access annotations should be omitted from phpdocs.
  518. * **phpdoc_no_alias_tag** [@Symfony]
  519. No alias PHPDoc tags should be used.
  520. Configuration options:
  521. - ``replacements`` (``array``): mapping between replaced annotations with new
  522. ones; defaults to ``['property-read' => 'property', 'property-write' =>
  523. 'property', 'type' => 'var', 'link' => 'see']``
  524. * **phpdoc_no_empty_return** [@Symfony]
  525. @return void and @return null annotations should be omitted from
  526. phpdocs.
  527. * **phpdoc_no_package** [@Symfony]
  528. @package and @subpackage annotations should be omitted from phpdocs.
  529. * **phpdoc_no_useless_inheritdoc** [@Symfony]
  530. Classy that does not inherit must not have inheritdoc tags.
  531. * **phpdoc_order**
  532. Annotations in phpdocs should be ordered so that param annotations come
  533. first, then throws annotations, then return annotations.
  534. * **phpdoc_return_self_reference** [@Symfony]
  535. The type of ``@return`` annotations of methods returning a reference to
  536. itself must the configured one.
  537. Configuration options:
  538. - ``replacements`` (``array``): mapping between replaced return types with new
  539. ones; defaults to ``['this' => '$this', '@this' => '$this', '$self' =>
  540. 'self', '@self' => 'self', '$static' => 'static', '@static' =>
  541. 'static']``
  542. * **phpdoc_scalar** [@Symfony]
  543. Scalar types should always be written in the same form. ``int`` not
  544. ``integer``, ``bool`` not ``boolean``, ``float`` not ``real`` or ``double``.
  545. * **phpdoc_separation** [@Symfony]
  546. Annotations in phpdocs should be grouped together so that annotations of
  547. the same type immediately follow each other, and annotations of a
  548. different type are separated by a single blank line.
  549. * **phpdoc_single_line_var_spacing** [@Symfony]
  550. Single line @var PHPDoc should have proper spacing.
  551. * **phpdoc_summary** [@Symfony]
  552. Phpdocs summary should end in either a full stop, exclamation mark, or
  553. question mark.
  554. * **phpdoc_to_comment** [@Symfony]
  555. Docblocks should only be used on structural elements.
  556. * **phpdoc_trim** [@Symfony]
  557. Phpdocs should start and end with content, excluding the very first and
  558. last line of the docblocks.
  559. * **phpdoc_types** [@Symfony]
  560. The correct case must be used for standard PHP types in phpdoc.
  561. * **phpdoc_var_without_name** [@Symfony]
  562. @var and @type annotations should not contain the variable name.
  563. * **pow_to_exponentiation** [@PHP56Migration:risky, @PHP70Migration:risky]
  564. Converts ``pow`` to the ``**`` operator. Requires PHP >= 5.6.
  565. *Risky rule: risky when the function ``pow`` is overridden.*
  566. * **pre_increment** [@Symfony]
  567. Pre incrementation/decrementation should be used if possible.
  568. * **protected_to_private** [@Symfony]
  569. Converts ``protected`` variables and methods to ``private`` where possible.
  570. * **psr0**
  571. Classes must be in a path that matches their namespace, be at least one
  572. namespace deep and the class name should match the file name.
  573. *Risky rule: this fixer may change your class name, which will break the code that is depended on old name.*
  574. Configuration options:
  575. - ``dir`` (``string``): the directory where the project code is placed; defaults
  576. to ``''``
  577. * **psr4** [@Symfony:risky]
  578. Class names should match the file name.
  579. *Risky rule: this fixer may change your class name, which will break the code that is depended on old name.*
  580. * **random_api_migration** [@PHP70Migration:risky]
  581. Replaces ``rand``, ``srand``, ``getrandmax`` functions calls with their ``mt_*``
  582. analogs.
  583. *Risky rule: risky when the configured functions are overridden.*
  584. Configuration options:
  585. - ``replacements`` (``array``): mapping between replaced functions with the new
  586. ones; defaults to ``['getrandmax' => 'mt_getrandmax', 'rand' =>
  587. 'mt_rand', 'srand' => 'mt_srand']``
  588. * **return_type_declaration** [@Symfony]
  589. There should be one or no space before colon, and one space after it in
  590. return type declarations, according to configuration.
  591. Configuration options:
  592. - ``space_before`` (``'none'``, ``'one'``): spacing to apply before colon; defaults to
  593. ``'none'``
  594. * **self_accessor** [@Symfony]
  595. Inside class or interface element "self" should be preferred to the
  596. class name itself.
  597. * **semicolon_after_instruction**
  598. Instructions must be terminated with a semicolon.
  599. * **short_scalar_cast** [@Symfony]
  600. Cast ``(boolean)`` and ``(integer)`` should be written as ``(bool)`` and
  601. ``(int)``, ``(double)`` and ``(real)`` as ``(float)``.
  602. * **silenced_deprecation_error** [@Symfony:risky]
  603. Ensures deprecation notices are silenced.
  604. *Risky rule: silencing of deprecation errors might cause changes to code behaviour.*
  605. * **simplified_null_return**
  606. A return statement wishing to return ``void`` should not return ``null``.
  607. * **single_blank_line_at_eof** [@PSR2, @Symfony]
  608. A PHP file without end tag must always end with a single empty line
  609. feed.
  610. * **single_blank_line_before_namespace** [@Symfony]
  611. There should be exactly one blank line before a namespace declaration.
  612. * **single_class_element_per_statement** [@PSR2, @Symfony]
  613. There MUST NOT be more than one property or constant declared per
  614. statement.
  615. Configuration options:
  616. - ``elements`` (``array``): list of strings which element should be modified;
  617. defaults to ``['const', 'property']``
  618. * **single_import_per_statement** [@PSR2, @Symfony]
  619. There MUST be one use keyword per declaration.
  620. * **single_line_after_imports** [@PSR2, @Symfony]
  621. Each namespace use MUST go on its own line and there MUST be one blank
  622. line after the use statements block.
  623. * **single_quote** [@Symfony]
  624. Convert double quotes to single quotes for simple strings.
  625. * **space_after_semicolon** [@Symfony]
  626. Fix whitespace after a semicolon.
  627. * **standardize_not_equals** [@Symfony]
  628. Replace all ``<>`` with ``!=``.
  629. * **strict_comparison**
  630. Comparisons should be strict.
  631. *Risky rule: changing comparisons to strict might change code behavior.*
  632. * **strict_param**
  633. Functions should be used with ``$strict`` param set to ``true``.
  634. *Risky rule: risky when the fixed function is overridden or if the code relies on non-strict usage.*
  635. * **switch_case_semicolon_to_colon** [@PSR2, @Symfony]
  636. A case should be followed by a colon and not a semicolon.
  637. * **switch_case_space** [@PSR2, @Symfony]
  638. Removes extra spaces between colon and case value.
  639. * **ternary_operator_spaces** [@Symfony]
  640. Standardize spaces around ternary operator.
  641. * **ternary_to_null_coalescing** [@PHP70Migration, @PHP71Migration]
  642. Use ``null`` coalescing operator ``??`` where possible. Requires PHP >= 7.0.
  643. * **trailing_comma_in_multiline_array** [@Symfony]
  644. PHP multi-line arrays should have a trailing comma.
  645. * **trim_array_spaces** [@Symfony]
  646. Arrays should be formatted like function/method arguments, without
  647. leading or trailing single line space.
  648. * **unary_operator_spaces** [@Symfony]
  649. Unary operators should be placed adjacent to their operands.
  650. * **visibility_required** [@PSR2, @Symfony, @PHP71Migration]
  651. Visibility MUST be declared on all properties and methods; abstract and
  652. final MUST be declared before the visibility; static MUST be declared
  653. after the visibility.
  654. Configuration options:
  655. - ``elements`` (``array``): the structural elements to fix (PHP >= 7.1 required
  656. for ``const``); defaults to ``['property', 'method']``
  657. * **whitespace_after_comma_in_array** [@Symfony]
  658. In array declaration, there MUST be a whitespace after each comma.
  659. The ``--dry-run`` option displays the files that need to be
  660. fixed but without actually modifying them:
  661. .. code-block:: bash
  662. $ php php-cs-fixer.phar fix /path/to/code --dry-run
  663. Config file
  664. -----------
  665. Instead of using command line options to customize the rule, you can save the
  666. project configuration in a ``.php_cs.dist`` file in the root directory of your project.
  667. The file must return an instance of `PhpCsFixer\\ConfigInterface <https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/v2.2.16/src/ConfigInterface.php>`_
  668. which lets you configure the rules, the files and directories that
  669. need to be analyzed. You may also create ``.php_cs`` file, which is
  670. the local configuration that will be used instead of the project configuration. It
  671. is a good practice to add that file into your ``.gitignore`` file.
  672. With the ``--config`` option you can specify the path to the
  673. ``.php_cs`` file.
  674. The example below will add two rules to the default list of PSR2 set rules:
  675. .. code-block:: php
  676. <?php
  677. $finder = PhpCsFixer\Finder::create()
  678. ->exclude('somedir')
  679. ->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php')
  680. ->in(__DIR__)
  681. ;
  682. return PhpCsFixer\Config::create()
  683. ->setRules(array(
  684. '@PSR2' => true,
  685. 'strict_param' => true,
  686. 'array_syntax' => array('syntax' => 'short'),
  687. ))
  688. ->setFinder($finder)
  689. ;
  690. **NOTE**: ``exclude`` will work only for directories, so if you need to exclude file, try ``notPath``.
  691. See `Symfony\\Finder <http://symfony.com/doc/current/components/finder.html>`_
  692. online documentation for other `Finder` methods.
  693. You may also use a blacklist for the rules instead of the above shown whitelist approach.
  694. The following example shows how to use all ``Symfony`` rules but the ``full_opening_tag`` rule.
  695. .. code-block:: php
  696. <?php
  697. $finder = PhpCsFixer\Finder::create()
  698. ->exclude('somedir')
  699. ->in(__DIR__)
  700. ;
  701. return PhpCsFixer\Config::create()
  702. ->setRules(array(
  703. '@Symfony' => true,
  704. 'full_opening_tag' => false,
  705. ))
  706. ->setFinder($finder)
  707. ;
  708. You may want to use non-linux whitespaces in your project. Then you need to
  709. configure them in your config file.
  710. .. code-block:: php
  711. <?php
  712. return PhpCsFixer\Config::create()
  713. ->setIndent("\t")
  714. ->setLineEnding("\r\n")
  715. ;
  716. By using ``--using-cache`` option with ``yes`` or ``no`` you can set if the caching
  717. mechanism should be used.
  718. Caching
  719. -------
  720. The caching mechanism is enabled by default. This will speed up further runs by
  721. fixing only files that were modified since the last run. The tool will fix all
  722. files if the tool version has changed or the list of rules has changed.
  723. Cache is supported only for tool downloaded as phar file or installed via
  724. composer.
  725. Cache can be disabled via ``--using-cache`` option or config file:
  726. .. code-block:: php
  727. <?php
  728. return PhpCsFixer\Config::create()
  729. ->setUsingCache(false)
  730. ;
  731. Cache file can be specified via ``--cache-file`` option or config file:
  732. .. code-block:: php
  733. <?php
  734. return PhpCsFixer\Config::create()
  735. ->setCacheFile(__DIR__.'/.php_cs.cache')
  736. ;
  737. Using PHP CS Fixer on CI
  738. ------------------------
  739. Require ``friendsofphp/php-cs-fixer`` as a ``dev`` dependency:
  740. .. code-block:: bash
  741. $ ./composer.phar require --dev friendsofphp/php-cs-fixer
  742. Then, add the following command to your CI:
  743. .. code-block:: bash
  744. $ CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}")
  745. $ if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then IFS=$'\n' EXTRA_ARGS=('--path-mode=intersection' '--' ${CHANGED_FILES[@]}); fi
  746. $ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no "${EXTRA_ARGS[@]}"
  747. Where ``$COMMIT_RANGE`` is your range of commits, eg ``$TRAVIS_COMMIT_RANGE`` or ``HEAD~..HEAD``.
  748. Exit codes
  749. ----------
  750. Exit code is built using following bit flags:
  751. * 0 OK.
  752. * 1 General error (or PHP/HHVM minimal requirement not matched).
  753. * 4 Some files have invalid syntax (only in dry-run mode).
  754. * 8 Some files need fixing (only in dry-run mode).
  755. * 16 Configuration error of the application.
  756. * 32 Configuration error of a Fixer.
  757. * 64 Exception raised within the application.
  758. (applies to exit codes of the `fix` command only)
  759. Helpers
  760. -------
  761. Dedicated plugins exist for:
  762. * `Atom`_
  763. * `NetBeans`_
  764. * `PhpStorm`_
  765. * `Sublime Text`_
  766. * `Vim`_
  767. Contribute
  768. ----------
  769. The tool comes with quite a few built-in fixers, but everyone is more than
  770. welcome to `contribute`_ more of them.
  771. Fixers
  772. ~~~~~~
  773. A *fixer* is a class that tries to fix one CS issue (a ``Fixer`` class must
  774. implement ``FixerInterface``).
  775. Configs
  776. ~~~~~~~
  777. A *config* knows about the CS rules and the files and directories that must be
  778. scanned by the tool when run in the directory of your project. It is useful for
  779. projects that follow a well-known directory structures (like for Symfony
  780. projects for instance).
  781. .. _php-cs-fixer.phar: http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar
  782. .. _Atom: https://github.com/Glavin001/atom-beautify
  783. .. _NetBeans: http://plugins.netbeans.org/plugin/49042/php-cs-fixer
  784. .. _PhpStorm: https://medium.com/@valeryan/how-to-configure-phpstorm-to-use-php-cs-fixer-1844991e521f
  785. .. _Sublime Text: https://github.com/benmatselby/sublime-phpcs
  786. .. _Vim: https://github.com/stephpy/vim-php-cs-fixer
  787. .. _contribute: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/CONTRIBUTING.md