README.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. PHP Coding Standards Fixer
  2. ==========================
  3. The PHP Coding Standards Fixer tool fixes *most* issues in your code when you
  4. want to follow the PHP coding standards as defined in the PSR-1 and PSR-2
  5. documents and many more.
  6. If you are already using a linter to identify coding standards problems in your
  7. code, you know that fixing them by hand is tedious, especially on large
  8. projects. This tool does not only detect them, but also fixes them for you.
  9. Requirements
  10. ------------
  11. PHP needs to be a minimum version of PHP 5.3.6.
  12. Installation
  13. ------------
  14. Locally
  15. ~~~~~~~
  16. Download the `php-cs-fixer.phar`_ file and store it somewhere on your computer.
  17. Globally (manual)
  18. ~~~~~~~~~~~~~~~~~
  19. You can run these commands to easily access latest ``php-cs-fixer`` from anywhere on
  20. your system:
  21. .. code-block:: bash
  22. $ wget http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -O php-cs-fixer
  23. or with specified version:
  24. .. code-block:: bash
  25. $ wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.1.0/php-cs-fixer.phar -O php-cs-fixer
  26. or with curl:
  27. .. code-block:: bash
  28. $ curl -L http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -o php-cs-fixer
  29. then:
  30. .. code-block:: bash
  31. $ sudo chmod a+x php-cs-fixer
  32. $ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
  33. Then, just run ``php-cs-fixer``.
  34. Globally (Composer)
  35. ~~~~~~~~~~~~~~~~~~~
  36. To install PHP CS Fixer, install Composer and issue the following command:
  37. .. code-block:: bash
  38. $ ./composer.phar global require friendsofphp/php-cs-fixer
  39. Then make sure you have ``~/.composer/vendor/bin`` in your ``PATH`` and
  40. you're good to go:
  41. .. code-block:: bash
  42. $ export PATH="$PATH:$HOME/.composer/vendor/bin"
  43. Globally (homebrew)
  44. ~~~~~~~~~~~~~~~~~~~
  45. PHP-CS-Fixer is part of the homebrew-php project. Follow the installation
  46. instructions at https://github.com/homebrew/homebrew-php if you don't
  47. already have it.
  48. .. code-block:: bash
  49. $ brew install homebrew/php/php-cs-fixer
  50. Update
  51. ------
  52. Locally
  53. ~~~~~~~
  54. The ``self-update`` command tries to update ``php-cs-fixer`` itself:
  55. .. code-block:: bash
  56. $ php php-cs-fixer.phar self-update
  57. Globally (manual)
  58. ~~~~~~~~~~~~~~~~~
  59. You can update ``php-cs-fixer`` through this command:
  60. .. code-block:: bash
  61. $ sudo php-cs-fixer self-update
  62. Globally (Composer)
  63. ~~~~~~~~~~~~~~~~~~~
  64. You can update ``php-cs-fixer`` through this command:
  65. .. code-block:: bash
  66. $ ./composer.phar global update friendsofphp/php-cs-fixer
  67. Globally (homebrew)
  68. ~~~~~~~~~~~~~~~~~~~
  69. You can update ``php-cs-fixer`` through this command:
  70. .. code-block:: bash
  71. $ brew upgrade php-cs-fixer
  72. Usage
  73. -----
  74. The ``fix`` command tries to fix as much coding standards
  75. problems as possible on a given file or files in a given directory and its subdirectories:
  76. .. code-block:: bash
  77. $ php php-cs-fixer.phar fix /path/to/dir
  78. $ php php-cs-fixer.phar fix /path/to/file
  79. By default ``--path-mode`` is set to ``override``, which means, that if you specify the path to a file or a directory via
  80. command arguments, then the paths provided to a ``Finder`` in config file will be ignored. You can use ``--path-mode=intersection``
  81. to merge paths from the config file and from the argument:
  82. .. code-block:: bash
  83. $ php php-cs-fixer.phar fix --path-mode=intersection /path/to/dir
  84. The ``--format`` option for the output format. Supported formats are ``txt`` (default one), ``json``, ``xml`` and ``junit``.
  85. NOTE: When using ``junit`` format report generates in accordance with JUnit xml schema from Jenkins (see docs/junit-10.xsd).
  86. The ``--verbose`` option will show the applied rules. When using the ``txt`` format it will also displays progress notifications.
  87. The ``--rules`` option limits the rules to apply on the
  88. project:
  89. .. code-block:: bash
  90. $ php php-cs-fixer.phar fix /path/to/project --rules=@PSR2
  91. By default the PSR1 and PSR2 rules are used.
  92. The ``--rules`` option lets you choose the exact rules to
  93. apply (the rule names must be separated by a comma):
  94. .. code-block:: bash
  95. $ php php-cs-fixer.phar fix /path/to/dir --rules=line_ending,full_opening_tag,indentation_type
  96. 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,
  97. using ``-name_of_fixer``:
  98. .. code-block:: bash
  99. $ php php-cs-fixer.phar fix /path/to/dir --rules=-full_opening_tag,-indentation_type
  100. When using combinations of exact and blacklist rules, applying exact rules along with above blacklisted results:
  101. .. code-block:: bash
  102. $ php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_return,strict_comparison
  103. Complete configuration for rules can be supplied using a ``json`` formatted string.
  104. .. code-block:: bash
  105. $ php php-cs-fixer.phar fix /path/to/project --rules='{"concat_space": {"spacing": "none"}}'
  106. A combination of ``--dry-run`` and ``--diff`` will
  107. display a summary of proposed fixes, leaving your files unchanged.
  108. The ``--allow-risky`` option allows you to set whether risky rules may run. Default value is taken from config file.
  109. Risky rule is a rule, which could change code behaviour. By default no risky rules are run.
  110. The command can also read from standard input, in which case it won't
  111. automatically fix anything:
  112. .. code-block:: bash
  113. $ cat foo.php | php php-cs-fixer.phar fix --diff -
  114. Choose from the list of available rules:
  115. * **array_syntax**
  116. | PHP arrays should be declared using the configured syntax (requires PHP
  117. | >= 5.4 for short syntax).
  118. | *Rule is: configurable.*
  119. * **binary_operator_spaces** [@Symfony]
  120. | Binary operators should be surrounded by at least one space.
  121. | *Rule is: configurable.*
  122. * **blank_line_after_namespace** [@PSR2, @Symfony]
  123. | There MUST be one blank line after the namespace declaration.
  124. * **blank_line_after_opening_tag** [@Symfony]
  125. | Ensure there is no code on the same line as the PHP open tag and it is
  126. | followed by a blank line.
  127. * **blank_line_before_return** [@Symfony]
  128. | An empty line feed should precede a return statement.
  129. * **braces** [@PSR2, @Symfony]
  130. | The body of each structure MUST be enclosed by braces. Braces should be
  131. | properly placed. Body of braces should be properly indented.
  132. | *Rule is: configurable.*
  133. * **cast_spaces** [@Symfony]
  134. | A single space should be between cast and variable.
  135. * **class_definition** [@PSR2, @Symfony]
  136. | Whitespace around the keywords of a class, trait or interfaces
  137. | definition should be one space.
  138. | *Rule is: configurable.*
  139. * **class_keyword_remove**
  140. | Converts ``::class`` keywords to FQCN strings. Requires PHP >= 5.5.
  141. * **combine_consecutive_unsets**
  142. | Calling ``unset`` on multiple items should be done in one call.
  143. * **concat_space** [@Symfony]
  144. | Concatenation should be spaced according configuration.
  145. | *Rule is: configurable.*
  146. * **declare_equal_normalize** [@Symfony]
  147. | Equal sign in declare statement should be surrounded by spaces or not
  148. | following configuration.
  149. | *Rule is: configurable.*
  150. * **declare_strict_types**
  151. | Force strict types declaration in all files. Requires PHP >= 7.0.
  152. | *Rule is: risky.*
  153. * **dir_constant**
  154. | Replaces ``dirname(__FILE__)`` expression with equivalent ``__DIR__``
  155. | constant.
  156. | *Rule is: risky.*
  157. * **elseif** [@PSR2, @Symfony]
  158. | The keyword ``elseif`` should be used instead of ``else if`` so that all
  159. | control keywords look like single words.
  160. * **encoding** [@PSR1, @PSR2, @Symfony]
  161. | PHP code MUST use only UTF-8 without BOM (remove BOM).
  162. * **ereg_to_preg**
  163. | Replace deprecated ``ereg`` regular expression functions with preg.
  164. | *Rule is: risky.*
  165. * **full_opening_tag** [@PSR1, @PSR2, @Symfony]
  166. | PHP code must use the long ``<?php`` tags or short-echo ``<?=`` tags and not
  167. | other tag variations.
  168. * **function_declaration** [@PSR2, @Symfony]
  169. | Spaces should be properly placed in a function declaration.
  170. * **function_typehint_space** [@Symfony]
  171. | Add missing space between function's argument and its typehint.
  172. * **general_phpdoc_annotation_remove**
  173. | Configured annotations should be omitted from phpdocs.
  174. | *Rule is: configurable.*
  175. * **hash_to_slash_comment** [@Symfony]
  176. | Single line comments should use double slashes ``//`` and not hash ``#``.
  177. * **header_comment**
  178. | Add, replace or remove header comment.
  179. | *Rule is: configurable.*
  180. * **heredoc_to_nowdoc**
  181. | Convert ``heredoc`` to ``nowdoc`` where possible.
  182. * **include** [@Symfony]
  183. | Include/Require and file path should be divided with a single space.
  184. | File path should not be placed under brackets.
  185. * **indentation_type** [@PSR2, @Symfony]
  186. | Code MUST use configured indentation type.
  187. * **is_null** [@Symfony:risky]
  188. | Replaces is_null(parameter) expression with ``null === parameter``.
  189. | *Rule is: configurable, risky.*
  190. * **line_ending** [@PSR2, @Symfony]
  191. | All PHP files must use same line ending.
  192. * **linebreak_after_opening_tag**
  193. | Ensure there is no code on the same line as the PHP open tag.
  194. * **lowercase_cast** [@Symfony]
  195. | Cast should be written in lower case.
  196. * **lowercase_constants** [@PSR2, @Symfony]
  197. | The PHP constants ``true``, ``false``, and ``null`` MUST be in lower case.
  198. * **lowercase_keywords** [@PSR2, @Symfony]
  199. | PHP keywords MUST be in lower case.
  200. * **mb_str_functions**
  201. | Replace non multibyte-safe functions with corresponding mb function.
  202. | *Rule is: risky.*
  203. * **method_argument_space** [@PSR2, @Symfony]
  204. | In method arguments and method call, there MUST NOT be a space before
  205. | each comma and there MUST be one space after each comma.
  206. * **method_separation** [@Symfony]
  207. | Methods must be separated with one blank line.
  208. * **modernize_types_casting**
  209. | Replaces ``intval``, ``floatval``, ``doubleval``, ``strval`` and ``boolval``
  210. | function calls with according type casting operator.
  211. | *Rule is: risky.*
  212. * **native_function_casing** [@Symfony]
  213. | Function defined by PHP should be called using the correct casing.
  214. * **native_function_invocation**
  215. | Add leading ``\`` before function invocation of internal function to speed
  216. | up resolving.
  217. | *Rule is: configurable, risky.*
  218. * **new_with_braces** [@Symfony]
  219. | All instances created with new keyword must be followed by braces.
  220. * **no_alias_functions** [@Symfony:risky]
  221. | Master functions shall be used instead of aliases.
  222. | *Rule is: risky.*
  223. * **no_blank_lines_after_class_opening** [@Symfony]
  224. | There should be no empty lines after class opening brace.
  225. * **no_blank_lines_after_phpdoc** [@Symfony]
  226. | There should not be blank lines between docblock and the documented
  227. | element.
  228. * **no_blank_lines_before_namespace**
  229. | There should be no blank lines before a namespace declaration.
  230. * **no_closing_tag** [@PSR2, @Symfony]
  231. | The closing ``?>`` tag MUST be omitted from files containing only PHP.
  232. * **no_empty_comment** [@Symfony]
  233. | There should not be any empty comments.
  234. * **no_empty_phpdoc** [@Symfony]
  235. | There should not be empty PHPDoc blocks.
  236. * **no_empty_statement** [@Symfony]
  237. | Remove useless semicolon statements.
  238. * **no_extra_consecutive_blank_lines** [@Symfony]
  239. | Removes extra blank lines and/or blank lines following configuration.
  240. | *Rule is: configurable.*
  241. * **no_leading_import_slash** [@Symfony]
  242. | Remove leading slashes in use clauses.
  243. * **no_leading_namespace_whitespace** [@Symfony]
  244. | The namespace declaration line shouldn't contain leading whitespace.
  245. * **no_mixed_echo_print** [@Symfony]
  246. | Either language construct ``print`` or ``echo`` should be used.
  247. | *Rule is: configurable.*
  248. * **no_multiline_whitespace_around_double_arrow** [@Symfony]
  249. | Operator ``=>`` should not be surrounded by multi-line whitespaces.
  250. * **no_multiline_whitespace_before_semicolons**
  251. | Multi-line whitespace before closing semicolon are prohibited.
  252. * **no_php4_constructor**
  253. | Convert PHP4-style constructors to ``__construct``.
  254. | *Rule is: risky.*
  255. * **no_short_bool_cast** [@Symfony]
  256. | Short cast ``bool`` using double exclamation mark should not be used.
  257. * **no_short_echo_tag**
  258. | Replace short-echo ``<?=`` with long format ``<?php echo`` syntax.
  259. * **no_singleline_whitespace_before_semicolons** [@Symfony]
  260. | Single-line whitespace before closing semicolon are prohibited.
  261. * **no_spaces_after_function_name** [@PSR2, @Symfony]
  262. | When making a method or function call, there MUST NOT be a space between
  263. | the method or function name and the opening parenthesis.
  264. * **no_spaces_around_offset** [@Symfony]
  265. | There MUST NOT be spaces around offset braces.
  266. | *Rule is: configurable.*
  267. * **no_spaces_inside_parenthesis** [@PSR2, @Symfony]
  268. | There MUST NOT be a space after the opening parenthesis. There MUST NOT
  269. | be a space before the closing parenthesis.
  270. * **no_trailing_comma_in_list_call** [@Symfony]
  271. | Remove trailing commas in list function calls.
  272. * **no_trailing_comma_in_singleline_array** [@Symfony]
  273. | PHP single-line arrays should not have trailing comma.
  274. * **no_trailing_whitespace** [@PSR2, @Symfony]
  275. | Remove trailing whitespace at the end of non-blank lines.
  276. * **no_trailing_whitespace_in_comment** [@PSR2, @Symfony]
  277. | There MUST be no trailing spaces inside comments and phpdocs.
  278. * **no_unneeded_control_parentheses** [@Symfony]
  279. | Removes unneeded parentheses around control statements.
  280. | *Rule is: configurable.*
  281. * **no_unreachable_default_argument_value**
  282. | In function arguments there must not be arguments with default values
  283. | before non-default ones.
  284. | *Rule is: risky.*
  285. * **no_unused_imports** [@Symfony]
  286. | Unused use statements must be removed.
  287. * **no_useless_else**
  288. | There should not be useless ``else`` cases.
  289. * **no_useless_return**
  290. | There should not be an empty return statement at the end of a function.
  291. * **no_whitespace_before_comma_in_array** [@Symfony]
  292. | In array declaration, there MUST NOT be a whitespace before each comma.
  293. * **no_whitespace_in_blank_line** [@Symfony]
  294. | Remove trailing whitespace at the end of blank lines.
  295. * **normalize_index_brace** [@Symfony]
  296. | Array index should always be written by using square braces.
  297. * **not_operator_with_space**
  298. | Logical NOT operators (``!``) should have leading and trailing
  299. | whitespaces.
  300. * **not_operator_with_successor_space**
  301. | Logical NOT operators (``!``) should have one trailing whitespace.
  302. * **object_operator_without_whitespace** [@Symfony]
  303. | There should not be space before or after object ``T_OBJECT_OPERATOR``
  304. | ``->``.
  305. * **ordered_class_elements**
  306. | Orders the elements of classes/interfaces/traits.
  307. | *Rule is: configurable.*
  308. * **ordered_imports**
  309. | Ordering use statements.
  310. | *Rule is: configurable.*
  311. * **php_unit_construct** [@Symfony:risky]
  312. | PHPUnit assertion method calls like "->assertSame(true, $foo)" should be
  313. | written with dedicated method like "->assertTrue($foo)".
  314. | *Rule is: configurable, risky.*
  315. * **php_unit_dedicate_assert** [@Symfony:risky]
  316. | PHPUnit assertions like "assertInternalType", "assertFileExists", should
  317. | be used over "assertTrue".
  318. | *Rule is: configurable, risky.*
  319. * **php_unit_fqcn_annotation** [@Symfony]
  320. | PHPUnit annotations should be a FQCNs including a root namespace.
  321. * **php_unit_strict**
  322. | PHPUnit methods like ``assertSame`` should be used instead of
  323. | ``assertEquals``.
  324. | *Rule is: configurable, risky.*
  325. * **phpdoc_add_missing_param_annotation**
  326. | Phpdoc should contain @param for all params.
  327. | *Rule is: configurable.*
  328. * **phpdoc_align** [@Symfony]
  329. | All items of the @param, @throws, @return, @var, and @type phpdoc tags
  330. | must be aligned vertically.
  331. * **phpdoc_annotation_without_dot** [@Symfony]
  332. | Phpdocs annotation descriptions should not be a sentence.
  333. * **phpdoc_indent** [@Symfony]
  334. | Docblocks should have the same indentation as the documented subject.
  335. * **phpdoc_inline_tag** [@Symfony]
  336. | Fix phpdoc inline tags, make inheritdoc always inline.
  337. * **phpdoc_no_access** [@Symfony]
  338. | @access annotations should be omitted from phpdocs.
  339. * **phpdoc_no_alias_tag** [@Symfony]
  340. | No alias PHPDoc tags should be used.
  341. | *Rule is: configurable.*
  342. * **phpdoc_no_empty_return** [@Symfony]
  343. | @return void and @return null annotations should be omitted from
  344. | phpdocs.
  345. * **phpdoc_no_package** [@Symfony]
  346. | @package and @subpackage annotations should be omitted from phpdocs.
  347. * **phpdoc_no_useless_inheritdoc** [@Symfony]
  348. | Classy that does not inherit must not have inheritdoc tags.
  349. * **phpdoc_order**
  350. | Annotations in phpdocs should be ordered so that param annotations come
  351. | first, then throws annotations, then return annotations.
  352. * **phpdoc_return_self_reference** [@Symfony]
  353. | The type of ``@return`` annotations of methods returning a reference to
  354. | itself must the configured one.
  355. | *Rule is: configurable.*
  356. * **phpdoc_scalar** [@Symfony]
  357. | Scalar types should always be written in the same form. ``int`` not
  358. | ``integer``, ``bool`` not ``boolean``, ``float`` not ``real`` or ``double``.
  359. * **phpdoc_separation** [@Symfony]
  360. | Annotations in phpdocs should be grouped together so that annotations of
  361. | the same type immediately follow each other, and annotations of a
  362. | different type are separated by a single blank line.
  363. * **phpdoc_single_line_var_spacing** [@Symfony]
  364. | Single line @var PHPDoc should have proper spacing.
  365. * **phpdoc_summary** [@Symfony]
  366. | Phpdocs summary should end in either a full stop, exclamation mark, or
  367. | question mark.
  368. * **phpdoc_to_comment** [@Symfony]
  369. | Docblocks should only be used on structural elements.
  370. * **phpdoc_trim** [@Symfony]
  371. | Phpdocs should start and end with content, excluding the very first and
  372. | last line of the docblocks.
  373. * **phpdoc_types** [@Symfony]
  374. | The correct case must be used for standard PHP types in phpdoc.
  375. * **phpdoc_var_without_name** [@Symfony]
  376. | @var and @type annotations should not contain the variable name.
  377. * **pow_to_exponentiation** [@PHP56Migration, @PHP70Migration, @PHP71Migration]
  378. | Converts ``pow()`` to the ``**`` operator. Requires PHP >= 5.6.
  379. | *Rule is: risky.*
  380. * **pre_increment** [@Symfony]
  381. | Pre incrementation/decrementation should be used if possible.
  382. * **protected_to_private**
  383. | Converts ``protected`` variables and methods to ``private`` where possible.
  384. * **psr0**
  385. | Classes must be in a path that matches their namespace, be at least one
  386. | namespace deep and the class name should match the file name.
  387. | *Rule is: configurable, risky.*
  388. * **psr4**
  389. | Class names should match the file name.
  390. | *Rule is: risky.*
  391. * **random_api_migration** [@PHP70Migration, @PHP71Migration]
  392. | Replaces ``rand``, ``mt_rand``, ``srand``, ``getrandmax`` functions calls with
  393. | their ``mt_*`` analogs.
  394. | *Rule is: configurable, risky.*
  395. * **return_type_declaration** [@Symfony]
  396. | There should be one or no space before colon, and one space after it in
  397. | return type declarations, according to configuration.
  398. | *Rule is: configurable.*
  399. * **self_accessor** [@Symfony]
  400. | Inside a classy element "self" should be preferred to the class name
  401. | itself.
  402. * **semicolon_after_instruction**
  403. | Instructions must be terminated with a semicolon.
  404. * **short_scalar_cast** [@Symfony]
  405. | Cast ``(boolean)`` and ``(integer)`` should be written as ``(bool)`` and
  406. | ``(int)``, ``(double)`` and ``(real)`` as ``(float)``.
  407. * **silenced_deprecation_error** [@Symfony:risky]
  408. | Ensures deprecation notices are silenced.
  409. | *Rule is: risky.*
  410. * **simplified_null_return**
  411. | A return statement wishing to return ``void`` should not return ``null``.
  412. | *Rule is: risky.*
  413. * **single_blank_line_at_eof** [@PSR2, @Symfony]
  414. | A PHP file without end tag must always end with a single empty line
  415. | feed.
  416. * **single_blank_line_before_namespace** [@Symfony]
  417. | There should be exactly one blank line before a namespace declaration.
  418. * **single_class_element_per_statement** [@PSR2, @Symfony]
  419. | There MUST NOT be more than one property or constant declared per
  420. | statement.
  421. | *Rule is: configurable.*
  422. * **single_import_per_statement** [@PSR2, @Symfony]
  423. | There MUST be one use keyword per declaration.
  424. * **single_line_after_imports** [@PSR2, @Symfony]
  425. | Each namespace use MUST go on its own line and there MUST be one blank
  426. | line after the use statements block.
  427. * **single_quote** [@Symfony]
  428. | Convert double quotes to single quotes for simple strings.
  429. * **space_after_semicolon** [@Symfony]
  430. | Fix whitespace after a semicolon.
  431. * **standardize_not_equals** [@Symfony]
  432. | Replace all ``<>`` with ``!=``.
  433. * **strict_comparison**
  434. | Comparisons should be strict.
  435. | *Rule is: risky.*
  436. * **strict_param**
  437. | Functions should be used with ``$strict`` param set to ``true``.
  438. | *Rule is: risky.*
  439. * **switch_case_semicolon_to_colon** [@PSR2, @Symfony]
  440. | A case should be followed by a colon and not a semicolon.
  441. * **switch_case_space** [@PSR2, @Symfony]
  442. | Removes extra spaces between colon and case value.
  443. * **ternary_operator_spaces** [@Symfony]
  444. | Standardize spaces around ternary operator.
  445. * **ternary_to_null_coalescing**
  446. | Use ``null`` coalescing operator ``??`` where possible.
  447. * **trailing_comma_in_multiline_array** [@Symfony]
  448. | PHP multi-line arrays should have a trailing comma.
  449. * **trim_array_spaces** [@Symfony]
  450. | Arrays should be formatted like function/method arguments, without
  451. | leading or trailing single line space.
  452. * **unary_operator_spaces** [@Symfony]
  453. | Unary operators should be placed adjacent to their operands.
  454. * **visibility_required** [@PSR2, @Symfony, @PHP71Migration]
  455. | Visibility MUST be declared on all properties and methods; abstract and
  456. | final MUST be declared before the visibility; static MUST be declared
  457. | after the visibility.
  458. | *Rule is: configurable.*
  459. * **whitespace_after_comma_in_array** [@Symfony]
  460. | In array declaration, there MUST be a whitespace after each comma.
  461. The ``--dry-run`` option displays the files that need to be
  462. fixed but without actually modifying them:
  463. .. code-block:: bash
  464. $ php php-cs-fixer.phar fix /path/to/code --dry-run
  465. Instead of using command line options to customize the rule, you can save the
  466. project configuration in a ``.php_cs.dist`` file in the root directory
  467. of your project. The file must return an instance of ``PhpCsFixer\ConfigInterface``,
  468. which lets you configure the rules, the files and directories that
  469. need to be analyzed. You may also create ``.php_cs`` file, which is
  470. the local configuration that will be used instead of the project configuration. It
  471. is a good practice to add that file into your ``.gitignore`` file.
  472. With the ``--config`` option you can specify the path to the
  473. ``.php_cs`` file.
  474. The example below will add two rules to the default list of PSR2 set rules:
  475. .. code-block:: php
  476. <?php
  477. $finder = PhpCsFixer\Finder::create()
  478. ->exclude('somedir')
  479. ->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php')
  480. ->in(__DIR__)
  481. ;
  482. return PhpCsFixer\Config::create()
  483. ->setRules(array(
  484. '@PSR2' => true,
  485. 'strict_param' => true,
  486. 'array_syntax' => array('syntax' => 'short'),
  487. ))
  488. ->setFinder($finder)
  489. ;
  490. **NOTE**: ``exclude`` will work only for directories, so if you need to exclude file, try ``notPath``.
  491. See `Symfony\\Finder <http://symfony.com/doc/current/components/finder.html>`_
  492. online documentation for other `Finder` methods.
  493. You may also use a blacklist for the rules instead of the above shown whitelist approach.
  494. The following example shows how to use all ``Symfony`` rules but the ``full_opening_tag`` rule.
  495. .. code-block:: php
  496. <?php
  497. $finder = PhpCsFixer\Finder::create()
  498. ->exclude('somedir')
  499. ->in(__DIR__)
  500. ;
  501. return PhpCsFixer\Config::create()
  502. ->setRules(array(
  503. '@Symfony' => true,
  504. 'full_opening_tag' => false,
  505. ))
  506. ->setFinder($finder)
  507. ;
  508. You may want to use non-linux whitespaces in your project. Then you need to
  509. configure them in your config file. Please be aware that this feature is
  510. experimental.
  511. .. code-block:: php
  512. <?php
  513. return PhpCsFixer\Config::create()
  514. ->setIndent("\t")
  515. ->setLineEnding("\r\n")
  516. ;
  517. By using ``--using-cache`` option with yes or no you can set if the caching
  518. mechanism should be used.
  519. Caching
  520. -------
  521. The caching mechanism is enabled by default. This will speed up further runs by
  522. fixing only files that were modified since the last run. The tool will fix all
  523. files if the tool version has changed or the list of rules has changed.
  524. Cache is supported only for tool downloaded as phar file or installed via
  525. composer.
  526. Cache can be disabled via ``--using-cache`` option or config file:
  527. .. code-block:: php
  528. <?php
  529. return PhpCsFixer\Config::create()
  530. ->setUsingCache(false)
  531. ;
  532. Cache file can be specified via ``--cache-file`` option or config file:
  533. .. code-block:: php
  534. <?php
  535. return PhpCsFixer\Config::create()
  536. ->setCacheFile(__DIR__.'/.php_cs.cache')
  537. ;
  538. Using PHP CS Fixer on CI
  539. ------------------------
  540. Require ``friendsofphp/php-cs-fixer`` as a ``dev`` dependency:
  541. .. code-block:: bash
  542. $ ./composer.phar require --dev friendsofphp/php-cs-fixer
  543. Then, add the following command to your CI:
  544. .. code-block:: bash
  545. $ IFS=$'\n'; COMMIT_SCA_FILES=($(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}")); unset IFS
  546. $ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --using-cache=no --path-mode=intersection "${COMMIT_SCA_FILES[@]}"
  547. Where ``$COMMIT_RANGE`` is your range of commits, eg ``$TRAVIS_COMMIT_RANGE`` or ``HEAD~..HEAD``.
  548. Exit codes
  549. ----------
  550. Exit code is build using following bit flags:
  551. * 0 OK.
  552. * 1 General error (or PHP/HHVM minimal requirement not matched).
  553. * 4 Some files have invalid syntax (only in dry-run mode).
  554. * 8 Some files need fixing (only in dry-run mode).
  555. * 16 Configuration error of the application.
  556. * 32 Configuration error of a Fixer.
  557. * 64 Exception raised within the application.
  558. (applies to exit codes of the `fix` command only)
  559. Helpers
  560. -------
  561. Dedicated plugins exist for:
  562. * `Atom`_
  563. * `NetBeans`_
  564. * `PhpStorm`_
  565. * `Sublime Text`_
  566. * `Vim`_
  567. Contribute
  568. ----------
  569. The tool comes with quite a few built-in fixers, but everyone is more than
  570. welcome to `contribute`_ more of them.
  571. Fixers
  572. ~~~~~~
  573. A *fixer* is a class that tries to fix one CS issue (a ``Fixer`` class must
  574. implement ``FixerInterface``).
  575. Configs
  576. ~~~~~~~
  577. A *config* knows about the CS rules and the files and directories that must be
  578. scanned by the tool when run in the directory of your project. It is useful for
  579. projects that follow a well-known directory structures (like for Symfony
  580. projects for instance).
  581. .. _php-cs-fixer.phar: http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar
  582. .. _Atom: https://github.com/Glavin001/atom-beautify
  583. .. _NetBeans: http://plugins.netbeans.org/plugin/49042/php-cs-fixer
  584. .. _PhpStorm: http://tzfrs.de/2015/01/automatically-format-code-to-match-psr-standards-with-phpstorm
  585. .. _Sublime Text: https://github.com/benmatselby/sublime-phpcs
  586. .. _Vim: https://github.com/stephpy/vim-php-cs-fixer
  587. .. _contribute: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/CONTRIBUTING.md