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