ReadmeCommand.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Console\Command;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. */
  21. final class ReadmeCommand extends Command
  22. {
  23. const COMMAND_NAME = 'readme';
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName(self::COMMAND_NAME)
  31. ->setDescription('Generates the README content, based on the fix command help.')
  32. ;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $header = <<<'EOF'
  40. PHP Coding Standards Fixer
  41. ==========================
  42. The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards;
  43. whether you want to follow PHP coding standards as defined in the PSR-1, PSR-2, etc.,
  44. or other community driven ones like the Symfony one.
  45. You can **also** define your (teams) style through configuration.
  46. It can modernize your code (like converting the `pow` function to the `**` operator on PHP 5.6)
  47. and (micro) optimize it.
  48. If you are already using a linter to identify coding standards problems in your
  49. code, you know that fixing them by hand is tedious, especially on large
  50. projects. This tool does not only detect them, but also fixes them for you.
  51. The PHP CS Fixer is maintained on GitHub at https://github.com/FriendsOfPHP/PHP-CS-Fixer
  52. bug reports and ideas about new features are welcome there.
  53. You can talk to us at https://gitter.im/PHP-CS-Fixer/Lobby about the project,
  54. configuration, possible improvements, ideas and questions, please visit us!
  55. Requirements
  56. ------------
  57. PHP needs to be a minimum version of PHP 5.6.0.
  58. Installation
  59. ------------
  60. Locally
  61. ~~~~~~~
  62. Download the `php-cs-fixer.phar`_ file and store it somewhere on your computer.
  63. Globally (manual)
  64. ~~~~~~~~~~~~~~~~~
  65. You can run these commands to easily access latest ``php-cs-fixer`` from anywhere on
  66. your system:
  67. .. code-block:: bash
  68. $ wget %download.url% -O php-cs-fixer
  69. or with specified version:
  70. .. code-block:: bash
  71. $ wget %download.version_url% -O php-cs-fixer
  72. or with curl:
  73. .. code-block:: bash
  74. $ curl -L %download.url% -o php-cs-fixer
  75. then:
  76. .. code-block:: bash
  77. $ sudo chmod a+x php-cs-fixer
  78. $ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
  79. Then, just run ``php-cs-fixer``.
  80. Globally (Composer)
  81. ~~~~~~~~~~~~~~~~~~~
  82. To install PHP CS Fixer, `install Composer <https://getcomposer.org/download/>`_ and issue the following command:
  83. .. code-block:: bash
  84. $ composer global require friendsofphp/php-cs-fixer
  85. 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:
  86. .. code-block:: bash
  87. $ export PATH="$PATH:$HOME/.composer/vendor/bin"
  88. Globally (homebrew)
  89. ~~~~~~~~~~~~~~~~~~~
  90. PHP-CS-Fixer is part of the homebrew-php project. Follow the installation
  91. instructions at https://github.com/homebrew/homebrew-php if you don't
  92. already have it.
  93. .. code-block:: bash
  94. $ brew install homebrew/php/php-cs-fixer
  95. Update
  96. ------
  97. Locally
  98. ~~~~~~~
  99. The ``self-update`` command tries to update ``php-cs-fixer`` itself:
  100. .. code-block:: bash
  101. $ php php-cs-fixer.phar self-update
  102. Globally (manual)
  103. ~~~~~~~~~~~~~~~~~
  104. You can update ``php-cs-fixer`` through this command:
  105. .. code-block:: bash
  106. $ sudo php-cs-fixer self-update
  107. Globally (Composer)
  108. ~~~~~~~~~~~~~~~~~~~
  109. You can update ``php-cs-fixer`` through this command:
  110. .. code-block:: bash
  111. $ ./composer.phar global update friendsofphp/php-cs-fixer
  112. Globally (homebrew)
  113. ~~~~~~~~~~~~~~~~~~~
  114. You can update ``php-cs-fixer`` through this command:
  115. .. code-block:: bash
  116. $ brew upgrade php-cs-fixer
  117. Usage
  118. -----
  119. EOF;
  120. $footer = <<<'EOF'
  121. Helpers
  122. -------
  123. Dedicated plugins exist for:
  124. * `Atom`_
  125. * `NetBeans`_
  126. * `PhpStorm`_
  127. * `Sublime Text`_
  128. * `Vim`_
  129. Contribute
  130. ----------
  131. The tool comes with quite a few built-in fixers, but everyone is more than
  132. welcome to `contribute`_ more of them.
  133. Fixers
  134. ~~~~~~
  135. A *fixer* is a class that tries to fix one CS issue (a ``Fixer`` class must
  136. implement ``FixerInterface``).
  137. Configs
  138. ~~~~~~~
  139. A *config* knows about the CS rules and the files and directories that must be
  140. scanned by the tool when run in the directory of your project. It is useful for
  141. projects that follow a well-known directory structures (like for Symfony
  142. projects for instance).
  143. .. _php-cs-fixer.phar: %download.url%
  144. .. _Atom: https://github.com/Glavin001/atom-beautify
  145. .. _NetBeans: http://plugins.netbeans.org/plugin/49042/php-cs-fixer
  146. .. _PhpStorm: https://medium.com/@valeryan/how-to-configure-phpstorm-to-use-php-cs-fixer-1844991e521f
  147. .. _Sublime Text: https://github.com/benmatselby/sublime-phpcs
  148. .. _Vim: https://github.com/stephpy/vim-php-cs-fixer
  149. .. _contribute: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/CONTRIBUTING.md
  150. EOF;
  151. $command = $this->getApplication()->get('fix');
  152. $help = $command->getHelp();
  153. $help = str_replace('%command.full_name%', 'php-cs-fixer.phar '.$command->getName(), $help);
  154. $help = str_replace('%command.name%', $command->getName(), $help);
  155. $help = preg_replace('#</?(comment|info)>#', '``', $help);
  156. $help = preg_replace('#`(``.+?``)`#', '$1', $help);
  157. $help = preg_replace('#^(\s+)``(.+)``$#m', '$1$2', $help);
  158. $help = preg_replace('#^ \* ``(.+)``(.*?\n)#m', "* **$1**$2\n", $help);
  159. $help = preg_replace('#^ \\| #m', ' ', $help);
  160. $help = preg_replace('#^ \\|#m', '', $help);
  161. $help = preg_replace('#^(?= \\*Risky rule: )#m', "\n", $help);
  162. $help = preg_replace("#^( Configuration options:\n)( - )#m", "$1\n$2", $help);
  163. $help = preg_replace("#^\n( +\\$ )#m", "\n.. code-block:: bash\n\n$1", $help);
  164. $help = preg_replace("#^\n( +<\\?php)#m", "\n.. code-block:: php\n\n$1", $help);
  165. $help = preg_replace_callback(
  166. "#^\s*<\?(\w+).*?\?>#ms",
  167. function ($matches) {
  168. $result = preg_replace("#^\.\. code-block:: bash\n\n#m", '', $matches[0]);
  169. if ('php' !== $matches[1]) {
  170. $result = preg_replace("#<\?{$matches[1]}\s*#", '', $result);
  171. }
  172. $result = preg_replace("#\n\n +\?>#", '', $result);
  173. return $result;
  174. },
  175. $help
  176. );
  177. // Transform links
  178. // In the console output these have the form
  179. // `description` (<url>http://...</url>)
  180. // Make to RST http://www.sphinx-doc.org/en/stable/rest.html#hyperlinks
  181. // `description <http://...>`_
  182. $help = preg_replace_callback(
  183. '#`(.+)`\s?\(<url>(.+)<\/url>\)#',
  184. function (array $matches) {
  185. return sprintf('`%s <%s>`_', str_replace('\\', '\\\\', $matches[1]), $matches[2]);
  186. },
  187. $help
  188. );
  189. $help = preg_replace('#^ #m', ' ', $help);
  190. $help = preg_replace('#\*\* +\[#', '** [', $help);
  191. $downloadLatestUrl = sprintf('https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v%s/php-cs-fixer.phar', HelpCommand::getLatestReleaseVersionFromChangeLog());
  192. $downloadUrl = 'http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar';
  193. $header = str_replace('%download.version_url%', $downloadLatestUrl, $header);
  194. $header = str_replace('%download.url%', $downloadUrl, $header);
  195. $footer = str_replace('%download.version_url%', $downloadLatestUrl, $footer);
  196. $footer = str_replace('%download.url%', $downloadUrl, $footer);
  197. $output->write($header."\n".$help."\n".$footer);
  198. }
  199. }