Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ jobs:
- script: |
cd e2e/fix
composer install
rm -rf src cycle tmp && cp -r original src && cp -r cycle-original cycle
rm -rf src cycle tmp && cp -r original src && cp -r cycle-original cycle && cp ignore-original.neon ignore.neon
OUTPUT=$(AI_AGENT=1 ../bashunit assert exit_code "1" "../../bin/phpstan analyse --error-format raw")
echo "$OUTPUT"
../bashunit assert contains '7 of the errors can be fixed automatically. Run PHPStan again with --fix' "$OUTPUT"
Expand All @@ -984,9 +984,11 @@ jobs:
diff -u expected/Ignored-fix.php src/Ignored.php
OUTPUT=$(../bashunit assert exit_code "0" "../../bin/phpstan analyse --fix-ignored")
echo "$OUTPUT"
../bashunit assert contains '[OK] 2 errors fixed' "$OUTPUT"
# the last round removes the ignoreErrors entry, it does not match anything after the fixes
../bashunit assert contains '[OK] 3 errors fixed in 2 files' "$OUTPUT"
diff -u expected/Foo.php src/Foo.php
diff -u expected/Ignored-fix-ignored.php src/Ignored.php
diff -u ignore-expected.neon ignore.neon
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse --fix --fix-ignored")
echo "$OUTPUT"
../bashunit assert contains 'No fixable errors found' "$OUTPUT"
Expand All @@ -997,28 +999,50 @@ jobs:
cd e2e/fix-baseline
composer install
mkdir -p vendor/acme/rules && cp vendor-baseline.php vendor/acme/rules/baseline.php
rm -rf tmp && cp baseline-original.php baseline.php
rm -rf tmp && cp baseline-original.php baseline.php && cp baseline-original.neon baseline.neon
OUTPUT=$(AI_AGENT=1 ../bashunit assert exit_code "1" "../../bin/phpstan analyse --error-format raw")
echo "$OUTPUT"
# two unmatched messages and an unmatched entry of the PHP baseline, and a count higher than the occurrences;
# not the entries in NEON and in vendor/, and not a count lower than the occurrences (--generate-baseline does that)
../bashunit assert contains '4 of the errors can be fixed automatically. Run PHPStan again with --fix' "$OUTPUT"
# two unmatched messages, an unmatched entry, and a count higher than the occurrences in the PHP baseline,
# an unmatched message, an unmatched entry, and a count higher than the occurrences in the NEON baseline;
# not the entry in vendor/, and not a count lower than the occurrences (--generate-baseline does that)
../bashunit assert contains '7 of the errors can be fixed automatically. Run PHPStan again with --fix' "$OUTPUT"
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse --fix --generate-baseline")
echo "$OUTPUT"
../bashunit assert contains 'Errors cannot be fixed when generating the baseline.' "$OUTPUT"
OUTPUT=$(../bashunit assert exit_code "0" "../../bin/phpstan analyse --fix")
echo "$OUTPUT"
# removing the last two messages of the list conflicts, the second one goes in the next round
../bashunit assert contains 'Round 2: fixed 1 error in 1 file' "$OUTPUT"
../bashunit assert contains '[OK] 4 errors fixed in 1 file' "$OUTPUT"
# removing the last two messages of the PHP list conflicts, and so does removing the NEON entry
# and the first message of the entry after it, the second one goes in the next round
../bashunit assert contains 'Round 2: fixed 2 errors in 2 files' "$OUTPUT"
../bashunit assert contains '[OK] 7 errors fixed in 2 files' "$OUTPUT"
diff -u baseline-expected.php baseline.php
diff -u baseline-expected.neon baseline.neon
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse --error-format raw")
echo "$OUTPUT"
../bashunit assert contains 'Ignored error pattern #^Unmatched in NEON$#' "$OUTPUT"
../bashunit assert contains 'Ignored error pattern #^Unmatched in vendor$#' "$OUTPUT"
../bashunit assert contains 'is expected to occur 1 time, but occurred 2 times.' "$OUTPUT"
../bashunit assert not_contains 'Unmatched one' "$OUTPUT"
../bashunit assert not_contains 'NEON' "$OUTPUT"
../bashunit assert not_contains 'occurred only' "$OUTPUT"
- script: |
cd e2e/fix-ignored-entries
composer install
rm -rf src tmp && cp -r original src && cp baseline-original.php baseline.php && cp baseline-original.neon baseline.neon
OUTPUT=$(../bashunit assert exit_code "0" "../../bin/phpstan analyse --fix-ignored")
echo "$OUTPUT"
# the ignored errors in the first round, the ignores that matched them in the second round:
# the PHP baseline entry, the too high count of the NEON baseline entry and the @phpstan-ignore comment
../bashunit assert contains 'Round 1: fixed 5 errors in 3 files' "$OUTPUT"
../bashunit assert contains 'Round 2: fixed 3 errors in 3 files' "$OUTPUT"
../bashunit assert contains '[OK] 8 errors fixed in 5 files' "$OUTPUT"
for file in InComment.php InNeonBaseline.php InPhpBaseline.php; do diff -u expected/$file src/$file; done
diff -u expected/baseline.php baseline.php
diff -u expected/baseline.neon baseline.neon
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse --error-format raw")
echo "$OUTPUT"
# the entry that was stale before, in a file --fix-ignored did not change, is left alone
../bashunit assert contains 'Untouched.php:?:Ignored error pattern #^Stale$#' "$OUTPUT"
../bashunit assert not_contains 'src/In' "$OUTPUT"
- script: |
cd e2e/editor-mode
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
Expand Down
8 changes: 8 additions & 0 deletions build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ parameters:
message: '#^Call to PHPStan\\Cache\\ArenaCache\:\:attach\(\) on a separate line has no effect\.$#'
count: 1
path: ../src/Command/WorkerCommand.php
-
# nette/neon 3 marks its syntax tree, lexer and parser @internal. The format-preserving printer
# and the ignoreErrors fixer build on them, composer.json pins nette/neon to one version.
message: '#internal class Nette\\Neon\\#'
paths:
- ../src/Neon/*
- ../src/Analyser/Ignore/IgnoreErrorsConfigFixer.php
- ../tests/PHPStan/Neon/*
-
rawMessage: 'Property PHPStan\Command\CommandHelper::$reservedMemory is never read' # intentional
reportUnmatched: false
Expand Down
1 change: 1 addition & 0 deletions e2e/fix-baseline/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/composer.lock
/tmp
/baseline.php
/baseline.neon
12 changes: 12 additions & 0 deletions e2e/fix-baseline/baseline-expected.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
ignoreErrors:
-
message: '#^Undefined variable\: \$g$#'
identifier: variable.undefined
count: 3 # lowered to 3
path: src/Foo.php

-
messages:
- '#^Undefined variable\: \$h$#'
path: src/Foo.php
18 changes: 18 additions & 0 deletions e2e/fix-baseline/baseline-original.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
parameters:
ignoreErrors:
-
message: '#^Undefined variable\: \$g$#'
identifier: variable.undefined
count: 5 # lowered to 3
path: src/Foo.php

# removed together with this comment
-
message: '#^Unmatched in NEON$#'
path: src/Foo.php

-
messages:
- '#^Unmatched in a NEON list$#'
- '#^Undefined variable\: \$h$#'
path: src/Foo.php
6 changes: 1 addition & 5 deletions e2e/fix-baseline/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
includes:
- baseline.php
- baseline.neon
# a config file of an installed package is never fixed
- vendor/acme/rules/baseline.php

Expand All @@ -8,8 +9,3 @@ parameters:
paths:
- src
tmpDir: tmp
ignoreErrors:
# NEON config files are not fixed yet
-
message: '#^Unmatched in NEON$#'
path: src/Foo.php
8 changes: 8 additions & 0 deletions e2e/fix-baseline/src/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ function qux(): void
echo $f;
echo $f;
}

function quux(): void
{
echo $g;
echo $g;
echo $g;
echo $h;
}
6 changes: 6 additions & 0 deletions e2e/fix-ignored-entries/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/src
/tmp
/vendor
/composer.lock
/baseline.php
/baseline.neon
7 changes: 7 additions & 0 deletions e2e/fix-ignored-entries/baseline-original.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: '#^Call modern\(\) instead\.$#'
identifier: fixIgnoredEntriesE2E.rename
count: 3
path: src/InNeonBaseline.php
16 changes: 16 additions & 0 deletions e2e/fix-ignored-entries/baseline-original.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

$ignoreErrors = [];
$ignoreErrors[] = [
'message' => '#^Call modern\(\) instead\.$#',
'identifier' => 'fixIgnoredEntriesE2E.rename',
'count' => 2,
'path' => __DIR__ . '/src/InPhpBaseline.php',
];
// stale before --fix-ignored, in a file it does not change, so it is left alone
$ignoreErrors[] = [
'message' => '#^Stale$#',
'path' => __DIR__ . '/src/Untouched.php',
];

return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
7 changes: 7 additions & 0 deletions e2e/fix-ignored-entries/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"autoload-dev": {
"classmap": [
"rules/"
]
}
}
5 changes: 5 additions & 0 deletions e2e/fix-ignored-entries/expected/InComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace FixIgnoredEntriesE2E;

modern();
7 changes: 7 additions & 0 deletions e2e/fix-ignored-entries/expected/InNeonBaseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace FixIgnoredEntriesE2E;

modern();
legacy(1);
modern();
6 changes: 6 additions & 0 deletions e2e/fix-ignored-entries/expected/InPhpBaseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace FixIgnoredEntriesE2E;

modern();
modern();
7 changes: 7 additions & 0 deletions e2e/fix-ignored-entries/expected/baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: '#^Call modern\(\) instead\.$#'
identifier: fixIgnoredEntriesE2E.rename
count: 1
path: src/InNeonBaseline.php
10 changes: 10 additions & 0 deletions e2e/fix-ignored-entries/expected/baseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

$ignoreErrors = [];
// stale before --fix-ignored, in a file it does not change, so it is left alone
$ignoreErrors[] = [
'message' => '#^Stale$#',
'path' => __DIR__ . '/src/Untouched.php',
];

return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
5 changes: 5 additions & 0 deletions e2e/fix-ignored-entries/original/InComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace FixIgnoredEntriesE2E;

legacy(); // @phpstan-ignore fixIgnoredEntriesE2E.rename
7 changes: 7 additions & 0 deletions e2e/fix-ignored-entries/original/InNeonBaseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace FixIgnoredEntriesE2E;

legacy();
legacy(1);
legacy();
6 changes: 6 additions & 0 deletions e2e/fix-ignored-entries/original/InPhpBaseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace FixIgnoredEntriesE2E;

legacy();
legacy();
5 changes: 5 additions & 0 deletions e2e/fix-ignored-entries/original/Untouched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace FixIgnoredEntriesE2E;

modern();
11 changes: 11 additions & 0 deletions e2e/fix-ignored-entries/original/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace FixIgnoredEntriesE2E;

function legacy(int $i = 0): void
{
}

function modern(): void
{
}
12 changes: 12 additions & 0 deletions e2e/fix-ignored-entries/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
includes:
- baseline.php
- baseline.neon

parameters:
level: 0
paths:
- src
tmpDir: tmp

rules:
- FixIgnoredEntriesE2E\RenameRule
41 changes: 41 additions & 0 deletions e2e/fix-ignored-entries/rules/rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace FixIgnoredEntriesE2E;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* Renames legacy() to modern(). A call with arguments is reported too, but cannot be fixed.
*
* @implements Rule<FuncCall>
*/
final class RenameRule implements Rule
{

public function getNodeType(): string
{
return FuncCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Name || $node->name->toString() !== 'legacy') {
return [];
}

$error = RuleErrorBuilder::message('Call modern() instead.')->identifier('fixIgnoredEntriesE2E.rename');
if ($node->getArgs() === []) {
$error->fixNode($node, static function (FuncCall $node): FuncCall {
$node->name = new Node\Name('modern');
return $node;
});
}

return [$error->build()];
}

}
1 change: 1 addition & 0 deletions e2e/fix/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/tmp
/vendor
/composer.lock
/ignore.neon
2 changes: 2 additions & 0 deletions e2e/fix/ignore-expected.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors: []
5 changes: 5 additions & 0 deletions e2e/fix/ignore-original.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
ignoreErrors:
-
identifier: fixE2E.step
path: src/Ignored.php
7 changes: 3 additions & 4 deletions e2e/fix/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
includes:
- ignore.neon

parameters:
level: 0
paths:
- src
tmpDir: tmp
ignoreErrors:
-
identifier: fixE2E.step
path: src/Ignored.php

rules:
- FixE2E\StepRule
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ parameters:
-
rawMessage: 'Call to static method expand() of internal class Nette\DI\Helpers from outside its root namespace Nette.'
identifier: staticMethod.internalClass
count: 1
count: 2
path: src/Analyser/Ignore/IgnoreErrorsConfigFixer.php

-
Expand Down
Loading
Loading