Skip to content

Commit d401716

Browse files
authored
Merge pull request #5 from mnapoli/fix
Add the `pretty fix` command to fix files
2 parents 6907496 + 6e8d075 commit d401716

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ Now it's easy, simply run:
1010
pretty
1111
```
1212

13-
*Pretty* will detect the configuration file that exist in the current directory and will run the correct tool.
13+
*Pretty* will detect the configuration file that exist in the current directory and will run the correct tool. If no configuration file exist, *Pretty* will run PHP CodeSniffer with PSR-2 by default.
1414

15-
If no configuration file exist, *Pretty* will run PHP CodeSniffer with PSR-2 by default.
15+
If errors are found, simply run:
16+
17+
```
18+
pretty fix
19+
```
20+
21+
Again, *Pretty* will run the appropriate tool (`php-cs-fixer` or `phpcbf`) to fix as many errors as possible in your code.
1622

1723
## Installation
1824

@@ -34,13 +40,19 @@ composer global update mnapoli/pretty
3440

3541
## Usage
3642

37-
Running pretty is as simple as running:
43+
Running an analysis is as simple as running:
3844

3945
```
4046
pretty
4147
```
4248

43-
In case you are running it in CI, you might want to run:
49+
This command will not change any code. To fix errors reported by this command, simply run:
50+
51+
```
52+
pretty fix
53+
```
54+
55+
In case you are running the analyses in CI you might want to run:
4456

4557
```
4658
pretty ci

pretty

+25-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@
44
ini_set('display_errors', '1');
55
error_reporting(E_ALL);
66

7-
// TODO: "fix" option
8-
97
$commands = [
8+
/**
9+
* The default command will not change any file, it's just an analysis.
10+
*/
1011
'default' => [
1112
'php-cs-fixer' => 'php-cs-fixer fix --dry-run --allow-risky=yes --diff',
1213
'phpcs' => 'phpcs',
13-
'default' => 'phpcs --standard=PSR2 --extensions=php --ignore=vendor/* .',
14+
'default' => 'phpcs --standard=PSR2 --extensions=php --ignore="vendor/*" .',
15+
],
16+
/**
17+
* The `fix` command will modify the files to try to fix the errors.
18+
*/
19+
'fix' => [
20+
'php-cs-fixer' => 'php-cs-fixer fix --allow-risky=yes',
21+
'phpcs' => 'phpcbf',
22+
'default' => 'phpcbf --standard=PSR2 --extensions=php --ignore="vendor/*" .',
1423
],
1524
/**
1625
* In CI we disable the cache.
1726
*/
1827
'ci' => [
1928
'php-cs-fixer' => 'php-cs-fixer fix --dry-run --allow-risky=yes --diff --using-cache=no',
2029
'phpcs' => 'phpcs --no-cache',
21-
'default' => 'phpcs --standard=PSR2 --extensions=php --ignore=vendor/* --no-cache .',
30+
'default' => 'phpcs --standard=PSR2 --extensions=php --ignore="vendor/*" --no-cache .',
2231
],
2332
];
2433

@@ -30,21 +39,27 @@ $command = '';
3039
// Detect which tool to run
3140
if (file_exists('phpcs.xml') || file_exists('phpcs.xml.dist')) {
3241
echo "PHP CodeSniffer configuration file found, running CodeSniffer\n";
33-
assertPhpCodeSnifferIsInstalled();
3442
$command = $commands['phpcs'];
43+
$program = explode(' ', $command, 1)[0];
44+
assertPhpCodeSnifferIsInstalled($program);
3545
} elseif (file_exists('.php_cs') || file_exists('.php_cs.dist')) {
3646
echo "PHP-CS-Fixer configuration file found, running PHP-CS-Fixer\n";
3747
assertPhpCsFixerIsInstalled();
3848
$command = $commands['php-cs-fixer'];
3949
} else {
4050
echo "No configuration file found, running PHP CodeSniffer with PSR-2\n";
41-
assertPhpCodeSnifferIsInstalled();
4251
$command = $commands['default'];
52+
$program = explode(' ', $command, 2)[0];
53+
assertPhpCodeSnifferIsInstalled($program);
4354
}
4455

4556
// Run the analysis
4657
passthru($command, $exitCode);
4758

59+
if ($exitCode !== 0) {
60+
echo "Errors were found, run 'pretty fix' to fix them.\n";
61+
}
62+
4863
exit($exitCode);
4964

5065
function commandExists($command)
@@ -53,11 +68,11 @@ function commandExists($command)
5368
return !empty($return);
5469
}
5570

56-
function assertPhpCodeSnifferIsInstalled()
71+
function assertPhpCodeSnifferIsInstalled($command)
5772
{
58-
if (!commandExists('phpcs')) {
73+
if (!commandExists($command)) {
5974
echo <<<INSTRUCTIONS
60-
ERROR: PHP CodeSniffer does not seem to be installed because the 'phpcs' program cannot be found.
75+
ERROR: PHP CodeSniffer does not seem to be installed because the '$command' program cannot be found.
6176
You can install it by following the instructions here: https://github.com/squizlabs/PHP_CodeSniffer#installation
6277
INSTRUCTIONS;
6378
exit(1);
@@ -67,7 +82,7 @@ INSTRUCTIONS;
6782
function assertPhpCsFixerIsInstalled()
6883
{
6984
if (!commandExists('php-cs-fixer')) {
70-
echo <<<INSTRUCTIONS
85+
echo <<<'INSTRUCTIONS'
7186
ERROR: PHP-CS-Fixer does not seem to be installed because the 'php-cs-fixer' program cannot be found.
7287
You can install it by following the instructions here: https://github.com/FriendsOfPHP/PHP-CS-Fixer#installation
7388
INSTRUCTIONS;

0 commit comments

Comments
 (0)