README.rst 24 KB

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