ReadmeCommand.php 7.5 KB

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