Skip to content

Commit 5e4d6fa

Browse files
authored
Initial implementation (#1)
* Initial implementation * Fix tool name * Fix incomplete error message
1 parent ff03aad commit 5e4d6fa

18 files changed

+922
-0
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[*]
2+
charset = utf-8
3+
end_of_line = lf
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 4
8+

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/cache export-ignore
4+
/tests export-ignore
5+
/.editorconfig export-ignore
6+
/.gitattributes export-ignore
7+
/.github export-ignore
8+
/.gitignore export-ignore
9+
/phpcs.xml.dist export-ignore
10+
/phpstan.neon.dist export-ignore
11+
/phpunit.xml.dist export-ignore

.github/workflows/checks.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Checks
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- master
7+
jobs:
8+
checks:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
fail-fast: false
12+
steps:
13+
-
14+
name: Checkout code
15+
uses: actions/checkout@v4
16+
-
17+
name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: 8.4
21+
-
22+
name: Install dependencies
23+
run: composer install --no-progress --prefer-dist --no-interaction
24+
25+
-
26+
name: Run checks
27+
run: composer check
28+
29+
tests:
30+
runs-on: ubuntu-latest
31+
strategy:
32+
fail-fast: false
33+
matrix:
34+
php-version: [ '8.1', '8.2', '8.3', '8.4' ]
35+
dependency-version: [ prefer-lowest, prefer-stable ]
36+
steps:
37+
-
38+
name: Checkout code
39+
uses: actions/checkout@v4
40+
-
41+
name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php-version }}
45+
-
46+
name: Update dependencies
47+
run: composer update --no-progress --${{ matrix.dependency-version }} --no-interaction
48+
-
49+
name: Run tests
50+
run: composer check:tests

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/vendor
2+
/cache/*
3+
!/cache/.gitkeep
4+
/.idea
5+
/phpstan.neon
6+
/composer.lock

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# PHPStan error ignore inliner
2+
3+
Allows you to easily **inline ignore your PHPStan errors** via `@phpstan-ignore` comment.
4+
5+
So instead of:
6+
7+
```neon
8+
parameters:
9+
ignoreErrors:
10+
-
11+
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
12+
identifier: empty.notAllowed
13+
path: ../src/App/User.php
14+
count: 1
15+
```
16+
17+
You will have the ignored error directly in the source code `src/App/User.php`:
18+
19+
```php
20+
class User {
21+
22+
public function updateSurname(string $surname): void
23+
{
24+
if (empty($surname)) { // @phpstan-ignore empty.notAllowed
25+
throw new EmptyNameException();
26+
}
27+
}
28+
29+
}
30+
```
31+
32+
## Installation:
33+
34+
```sh
35+
composer require --dev shipmonk/phpstan-ignore-inliner
36+
```
37+
38+
## Usage
39+
40+
```sh
41+
vendor/bin/phpstan --error-format=json | vendor/bin/inline-phpstan-ignores
42+
```
43+
44+
## Contributing
45+
- Check your code by `composer check`
46+
- Autofix coding-style by `composer fix:cs`
47+
- All functionality must be tested

bin/inline-phpstan-ignores

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env php
2+
<?php declare(strict_types=1);
3+
4+
namespace ShipMonk\PHPStan\Errors;
5+
6+
use JsonException;
7+
8+
$autoloadFiles = [
9+
__DIR__ . '/../../../autoload.php',
10+
__DIR__ . '/../vendor/autoload.php',
11+
];
12+
13+
foreach ($autoloadFiles as $autoloadFile) {
14+
if (file_exists($autoloadFile)) {
15+
require_once $autoloadFile;
16+
break;
17+
}
18+
}
19+
20+
$usage = "\n\nUsage:\n$ vendor/bin/phpstan --error-format=json | vendor/bin/inline-phpstan-ignores\n";
21+
22+
try {
23+
$io = new Io();
24+
$input = $io->readInput();
25+
$errorsData = json_decode($input, associative: true, flags: JSON_THROW_ON_ERROR);
26+
$errors = $errorsData['files'] ?? throw new FailureException('No \'files\' key found on input JSON.');
27+
28+
$inliner = new InlineIgnoreInliner($io);
29+
$inliner->inlineErrors($errors);
30+
31+
$errorsCount = count($errors);
32+
echo "Done, $errorsCount errors processed.\n";
33+
34+
} catch (JsonException | FailureException $e) {
35+
echo $e->getMessage() . $usage;
36+
}

cache/.gitkeep

Whitespace-only changes.

composer.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"name": "shipmonk/phpstan-ignore-inliner",
3+
"description": "Inline your PHPStan error ignores into the source files via @phpstan-ignore comments.",
4+
"license": [
5+
"MIT"
6+
],
7+
"type": "phpstan-extension",
8+
"keywords": [
9+
"dev",
10+
"phpstan",
11+
"phpstan errors",
12+
"phpstan extension",
13+
"error identifier"
14+
],
15+
"require": {
16+
"php": "^8.1",
17+
"phpstan/phpstan": "^2"
18+
},
19+
"require-dev": {
20+
"editorconfig-checker/editorconfig-checker": "^10.7.0",
21+
"ergebnis/composer-normalize": "2.47.0",
22+
"phpstan/extension-installer": "^1.4.3",
23+
"phpstan/phpstan-phpunit": "^2.0.6",
24+
"phpstan/phpstan-strict-rules": "^2.0.4",
25+
"phpunit/phpunit": "^10.5.46",
26+
"shipmonk/phpstan-rules": "^4.1.2",
27+
"slevomat/coding-standard": "^8.18.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"ShipMonk\\PHPStan\\Errors\\": "src/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"ShipMonk\\PHPStan\\Errors\\": "tests/"
37+
}
38+
},
39+
"bin": [
40+
"bin/inline-phpstan-ignores"
41+
],
42+
"config": {
43+
"allow-plugins": {
44+
"dealerdirect/phpcodesniffer-composer-installer": false,
45+
"ergebnis/composer-normalize": true,
46+
"phpstan/extension-installer": true
47+
},
48+
"sort-packages": true
49+
},
50+
"scripts": {
51+
"check": [
52+
"@check:composer",
53+
"@check:ec",
54+
"@check:cs",
55+
"@check:types",
56+
"@check:tests"
57+
],
58+
"check:composer": [
59+
"composer normalize --dry-run --no-check-lock --no-update-lock",
60+
"composer validate --strict"
61+
],
62+
"check:cs": "phpcs",
63+
"check:ec": "ec src tests",
64+
"check:tests": "phpunit tests",
65+
"check:types": "phpstan analyse -vv --ansi",
66+
"fix:cs": "phpcbf"
67+
}
68+
}

0 commit comments

Comments
 (0)