ReadmeCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Console\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class ReadmeCommand extends Command
  18. {
  19. /**
  20. * @see Command
  21. */
  22. protected function configure()
  23. {
  24. $this
  25. ->setName('readme')
  26. ->setDescription('Generates the README content, based on the fix command help')
  27. ;
  28. }
  29. /**
  30. * @see Command
  31. */
  32. protected function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. $header = <<<'EOF'
  35. PHP Coding Standards Fixer
  36. ==========================
  37. The PHP Coding Standards Fixer tool fixes *most* issues in your code when you
  38. want to follow the PHP coding standards as defined in the PSR-1 and PSR-2
  39. documents and many more.
  40. If you are already using a linter to identify coding standards problems in your
  41. code, you know that fixing them by hand is tedious, especially on large
  42. projects. This tool does not only detect them, but also fixes them for you.
  43. Requirements
  44. ------------
  45. PHP needs to be a minimum version of PHP 5.3.6.
  46. Installation
  47. ------------
  48. Locally
  49. ~~~~~~~
  50. Download the `php-cs-fixer.phar`_ file and store it somewhere on your computer.
  51. Globally (manual)
  52. ~~~~~~~~~~~~~~~~~
  53. You can run these commands to easily access ``php-cs-fixer`` from anywhere on
  54. your system:
  55. .. code-block:: bash
  56. $ wget http://get.sensiolabs.org/php-cs-fixer.phar -O php-cs-fixer
  57. # With a specific version
  58. $ wget http://get.sensiolabs.org/php-cs-fixer-v1.11.phar -O php-cs-fixer
  59. or with curl:
  60. .. code-block:: bash
  61. $ curl http://get.sensiolabs.org/php-cs-fixer.phar -o php-cs-fixer
  62. # With a specific version
  63. $ curl http://get.sensiolabs.org/php-cs-fixer-v1.11.phar -o php-cs-fixer
  64. then:
  65. .. code-block:: bash
  66. $ sudo chmod a+x php-cs-fixer
  67. $ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
  68. Then, just run ``php-cs-fixer``.
  69. Globally (Composer)
  70. ~~~~~~~~~~~~~~~~~~~
  71. To install PHP-CS-Fixer, install Composer and issue the following command:
  72. .. code-block:: bash
  73. $ ./composer.phar global require fabpot/php-cs-fixer
  74. Then make sure you have ``~/.composer/vendor/bin`` in your ``PATH`` and
  75. you're good to go:
  76. .. code-block:: bash
  77. export PATH="$PATH:$HOME/.composer/vendor/bin"
  78. Globally (homebrew)
  79. ~~~~~~~~~~~~~~~~~~~
  80. PHP-CS-Fixer is part of the homebrew-php project. Follow the installation
  81. instructions at https://github.com/homebrew/homebrew-php if you don't
  82. already have it.
  83. .. code-block:: bash
  84. $ brew install homebrew/php/php-cs-fixer
  85. Update
  86. ------
  87. Locally
  88. ~~~~~~~
  89. The ``self-update`` command tries to update ``php-cs-fixer`` itself:
  90. .. code-block:: bash
  91. $ php php-cs-fixer.phar self-update
  92. Globally (manual)
  93. ~~~~~~~~~~~~~~~~~
  94. You can update ``php-cs-fixer`` through this command:
  95. .. code-block:: bash
  96. $ sudo php-cs-fixer self-update
  97. Globally (Composer)
  98. ~~~~~~~~~~~~~~~~~~~
  99. You can update ``php-cs-fixer`` through this command:
  100. .. code-block:: bash
  101. $ ./composer.phar global update fabpot/php-cs-fixer
  102. Globally (homebrew)
  103. ~~~~~~~~~~~~~~~~~~~
  104. You can update ``php-cs-fixer`` through this command:
  105. .. code-block:: bash
  106. $ brew upgrade php-cs-fixer
  107. Usage
  108. -----
  109. EOF;
  110. $footer = <<<'EOF'
  111. Helpers
  112. -------
  113. Dedicated plugins exist for:
  114. * `Atom`_
  115. * `NetBeans`_
  116. * `PhpStorm`_
  117. * `Sublime Text`_
  118. * `Vim`_
  119. Contribute
  120. ----------
  121. The tool comes with quite a few built-in fixers and finders, but everyone is
  122. more than welcome to `contribute`_ more of them.
  123. Fixers
  124. ~~~~~~
  125. A *fixer* is a class that tries to fix one CS issue (a ``Fixer`` class must
  126. implement ``FixerInterface``).
  127. Configs
  128. ~~~~~~~
  129. A *config* knows about the CS level and the files and directories that must be
  130. scanned by the tool when run in the directory of your project. It is useful for
  131. projects that follow a well-known directory structures (like for Symfony
  132. projects for instance).
  133. .. _php-cs-fixer.phar: http://get.sensiolabs.org/php-cs-fixer.phar
  134. .. _Atom: https://github.com/Glavin001/atom-beautify
  135. .. _NetBeans: http://plugins.netbeans.org/plugin/49042/php-cs-fixer
  136. .. _PhpStorm: http://tzfrs.de/2015/01/automatically-format-code-to-match-psr-standards-with-phpstorm
  137. .. _Sublime Text: https://github.com/benmatselby/sublime-phpcs
  138. .. _Vim: https://github.com/stephpy/vim-php-cs-fixer
  139. .. _contribute: https://github.com/FriendsOfPhp/php-cs-fixer/blob/master/CONTRIBUTING.md
  140. EOF;
  141. $command = $this->getApplication()->get('fix');
  142. $help = $command->getHelp();
  143. $help = str_replace('%command.full_name%', 'php-cs-fixer.phar '.$command->getName(), $help);
  144. $help = str_replace('%command.name%', $command->getName(), $help);
  145. $help = preg_replace('#</?(comment|info)>#', '``', $help);
  146. $help = preg_replace('#^(\s+)``(.+)``$#m', '$1$2', $help);
  147. $help = preg_replace('#^ \* ``(.+)``#m', '* **$1**', $help);
  148. $help = preg_replace("#^\n( +)#m", "\n.. code-block:: bash\n\n$1", $help);
  149. $help = preg_replace("#^\.\. code-block:: bash\n\n( +<\?(\w+))#m", ".. code-block:: $2\n\n$1", $help);
  150. $help = preg_replace_callback(
  151. "#^\s*<\?(\w+).*?\?>#ms",
  152. function ($matches) {
  153. $result = preg_replace("#^\.\. code-block:: bash\n\n#m", '', $matches[0]);
  154. if ('php' !== $matches[1]) {
  155. $result = preg_replace("#<\?{$matches[1]}\s*#", '', $result);
  156. }
  157. $result = preg_replace("#\n\n +\?>#", '', $result);
  158. return $result;
  159. },
  160. $help
  161. );
  162. $help = preg_replace('#^ #m', ' ', $help);
  163. $help = preg_replace('#\*\* +\[#', '** [', $help);
  164. $output->write($header."\n".$help."\n".$footer);
  165. }
  166. }