Skip to content

Commit 7fbcb9e

Browse files
authored
Merge pull request #27 from 123inkt/Add-bin-phpcs-baseline-shortcut
Add `phpcs-baseline` command for convenience
2 parents 7d57c3f + 3a67d33 commit 7fbcb9e

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ to the root of the project and name it `phpcs.baseline.xml`.
2020
```bash
2121
php vendor/bin/phpcs src tests --report=\\DR\\CodeSnifferBaseline\\Reports\\Baseline --report-file=phpcs.baseline.xml --basepath=.
2222
```
23+
Or convenient wrapper for the above command:
24+
```bash
25+
php vendor/bin/phpcs-baseline src tests [--no-exit-code]
26+
```
2327

2428
## Usage
2529
Use phpcs like you normally would. With `phpcs.baseline.xml` in the root of your project, the baseline extension will automatically read the config

bin/phpcs-baseline

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
// set default arguments next to src locations
7+
$_SERVER['argc'] += 3;
8+
$_SERVER['argv'][] = '--report=\\DR\\CodeSnifferBaseline\\Reports\\Baseline';
9+
$_SERVER['argv'][] = '--report-file=phpcs.baseline.xml';
10+
$_SERVER['argv'][] = '--basepath=.';
11+
12+
// check for --no-exit-code argument
13+
foreach ($_SERVER['argv'] as $index => $argument) {
14+
if ($argument === '--no-exit-code') {
15+
unset($_SERVER['argv'][$index]);
16+
register_shutdown_function(static fn() => exit(0));
17+
break;
18+
}
19+
}
20+
21+
// invoke phpcs. find it next to this file or in vendor/bin
22+
foreach ([__DIR__ . '/phpcs', __DIR__ . '/../vendor/bin/phpcs', __DIR__ . '/../../../bin/phpcs'] as $path) {
23+
if (file_exists($path)) {
24+
require $path;
25+
26+
return;
27+
}
28+
}
29+
30+
// failed to find phpcs
31+
fwrite(STDERR, 'Could not find `vendor/bin/phpcs`. Make sure to run `composer require squizlabs/php_codesniffer`' . PHP_EOL);
32+
exit(1);

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"scripts": {
3232
"baseline": ["@baseline:phpcs", "@baseline:phpmd", "@baseline:phpstan", "@baseline:phpcqc"],
33-
"baseline:phpcs": "phpcs --report=\\\\DR\\\\CodeSnifferBaseline\\\\Reports\\\\Baseline --report-file=phpcs.baseline.xml --basepath=.",
33+
"baseline:phpcs": "@php bin/phpcs-baseline src tests --no-exit-code",
3434
"baseline:phpmd": "@check:phpmd --generate-baseline",
3535
"baseline:phpstan": "phpstan --generate-baseline",
3636
"run:plugin": "DR\\CodeSnifferBaseline\\Plugin\\Plugin::run",
@@ -57,5 +57,8 @@
5757
"DR\\CodeSnifferBaseline\\Tests\\Unit\\": "tests/Unit/",
5858
"DR\\CodeSnifferBaseline\\Tests\\": "tests/"
5959
}
60-
}
60+
},
61+
"bin": [
62+
"bin/phpcs-baseline"
63+
]
6164
}

0 commit comments

Comments
 (0)