README.rst 25 KB

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