Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 14 additions & 6 deletions src/BaselineSplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function split(string $loaderFilePath): array

/**
* @param array<mixed> $errors
* @return array<string, list<array{message: string, count: int, path: string}>>
* @return array<string, list<array{message: string, count: int, path: string}|array{rawMessage: string, count: int, path: string}>>
*
* @throws ErrorException
*/
Expand All @@ -113,12 +113,16 @@ private function groupErrorsByIdentifier(

$identifier = $error['identifier'] ?? 'missing-identifier';

if (!isset($error['message'])) {
throw new ErrorException("Ignored error #$index is missing 'message'");
if (isset($error['rawMessage'])) {
$message = $error['rawMessage'];
$rawMessage = true;
} elseif (isset($error['message'])) {
$message = $error['message'];
$rawMessage = false;
} else {
throw new ErrorException("Ignored error #$index is missing 'message' or 'rawMessage'");
}

$message = $error['message'];

if (!isset($error['count'])) {
throw new ErrorException("Ignored error #$index is missing 'count'");
}
Expand All @@ -140,7 +144,11 @@ private function groupErrorsByIdentifier(

unset($error['identifier']);

$groupedErrors[$identifier][] = [
$groupedErrors[$identifier][] = $rawMessage ? [
'rawMessage' => $message,
'count' => $count,
'path' => $normalizedPath,
] : [
'message' => $message,
'count' => $count,
'path' => $normalizedPath,
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/BaselineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface BaselineHandler
public function decodeBaseline(string $filepath): array;

/**
* @param list<array{message: string, count: int, path: string}> $errors
* @param list<array{message: string, count: int, path: string}|array{rawMessage: string, count: int, path: string}> $errors
*/
public function encodeBaseline(
?string $comment,
Expand Down
15 changes: 13 additions & 2 deletions src/Handler/PhpBaselineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ShipMonk\PHPStan\Baseline\Exception\ErrorException;
use Throwable;
use function assert;
use function gettype;
use function is_array;
use function sprintf;
Expand Down Expand Up @@ -45,9 +46,19 @@ public function encodeBaseline(
$php .= "\$ignoreErrors = [];\n";

foreach ($errors as $error) {
if (isset($error['rawMessage'])) {
$message = $error['rawMessage'];
$messageKey = 'rawMessage';
} else {
assert(isset($error['message']));
$message = $error['message'];
$messageKey = 'message';
}

$php .= sprintf(
"\$ignoreErrors[] = [\n{$indent}'message' => %s,\n{$indent}'count' => %d,\n{$indent}'path' => __DIR__ . %s,\n];\n",
var_export($error['message'], true),
"\$ignoreErrors[] = [\n{$indent}%s => %s,\n{$indent}'count' => %d,\n{$indent}'path' => __DIR__ . %s,\n];\n",
var_export($messageKey, true),
var_export($message, true),
var_export($error['count'], true),
var_export('/' . $error['path'], true),
);
Expand Down
22 changes: 14 additions & 8 deletions tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ final class IntegrationTest extends BinTestCase
/**
* @dataProvider provideExtension
*/
public function testResultCache(string $extension): void
public function testResultCache(
string $extension,
bool $bleedingEdge
): void
{
$emptyConfig = $extension === 'php' ? '<?php return [];' : '';
$baselinesDir = 'cache/integration-test/baselines';
Expand All @@ -33,25 +36,28 @@ public function testResultCache(string $extension): void
$cwd = __DIR__;
$phpstan = '../../vendor/bin/phpstan';
$split = '../../bin/split-phpstan-baseline';
$config = $bleedingEdge ? "$extension.bleedingEdge.neon" : "$extension.neon";

$this->runCommand("$phpstan clear-result-cache -c $extension.neon", $cwd, 0);
$this->runCommand("$phpstan analyse -vv -c $extension.neon --generate-baseline=../../$baselinesDir/_loader.$extension", $cwd, 0, null, 'Result cache is saved.');
$this->runCommand("$phpstan clear-result-cache -c $config", $cwd, 0);
$this->runCommand("$phpstan analyse -vv -c $config --generate-baseline=../../$baselinesDir/_loader.$extension", $cwd, 0, null, 'Result cache is saved.');
$this->runCommand("php $split ../../$baselinesDir/_loader.$extension", $cwd, 0, 'Writing baseline file');
$this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 0, null, 'Result cache restored. 0 files will be reanalysed.');
$this->runCommand("$phpstan analyse -vv -c $config", $cwd, 0, null, 'Result cache restored. 0 files will be reanalysed.');

// cache should invalidate by editing the baseline
file_put_contents($baselinesDirAbs . "/method.notFound.$extension", $emptyConfig);

$this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 1, 'Call to an undefined method DateTime::invalid()');
$this->runCommand("$phpstan analyse -vv -c $config", $cwd, 1, 'Call to an undefined method DateTime::invalid()');
}

/**
* @return iterable<array{string}>
* @return iterable<array{string, bool}>
*/
public function provideExtension(): iterable
{
yield 'Neon' => ['neon'];
yield 'PHP' => ['php'];
yield 'Neon' => ['neon', false];
yield 'Neon (bleeding edge)' => ['neon', true];
yield 'PHP' => ['php', false];
yield 'PHP (bleeding edge)' => ['php', true];
}

}
9 changes: 9 additions & 0 deletions tests/Integration/neon.bleedingEdge.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: max
tmpDir: ../../cache/integration-test/phpstan-cache-neon
paths:
- src

includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- ../../cache/integration-test/baselines/_loader.neon
9 changes: 9 additions & 0 deletions tests/Integration/php.bleedingEdge.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: max
tmpDir: ../../cache/integration-test/phpstan-cache-php
paths:
- src

includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- ../../cache/integration-test/baselines/_loader.php
8 changes: 4 additions & 4 deletions tests/Rule/BaselinePerIdentifierFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function testFormat(): void
$this->createOutput(),
);

self::assertFileEquals(__DIR__ . '/data/baselines-neon/loader.neon', $fakeRoot . '/baselines/loader.neon');
self::assertFileEquals(__DIR__ . '/data/baselines-neon/sample.identifier.neon', $fakeRoot . '/baselines/sample.identifier.neon');
self::assertFileEquals(__DIR__ . '/data/baselines-neon/another.identifier.neon', $fakeRoot . '/baselines/another.identifier.neon');
self::assertFileEquals(__DIR__ . '/data/baselines-neon/missing-identifier.neon', $fakeRoot . '/baselines/missing-identifier.neon');
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/loader.neon', $fakeRoot . '/baselines/loader.neon');
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/sample.identifier.neon', $fakeRoot . '/baselines/sample.identifier.neon');
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/another.identifier.neon', $fakeRoot . '/baselines/another.identifier.neon');
self::assertFileEquals(__DIR__ . '/data/baselines-neon-deprecated/missing-identifier.neon', $fakeRoot . '/baselines/missing-identifier.neon');
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# total 1 error

parameters:
ignoreErrors:
-
message: '#^Error to escape ''\#$#'
count: 1
path: ../app/config.php
6 changes: 6 additions & 0 deletions tests/Rule/data/baselines-neon-deprecated/loader.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# total 4 errors

includes:
- another.identifier.neon
- missing-identifier.neon
- sample.identifier.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# total 1 error

parameters:
ignoreErrors:
-
message: '#^Error 3$#'
count: 1
path: ../app/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# total 2 errors

parameters:
ignoreErrors:
-
message: '#^Error simple$#'
count: 2
path: ../app/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ parameters:
message: '#^Error simple$#'
count: 2
path: ../app/file.php

-
rawMessage: Error raw message list<Foo\Bar>|null
count: 1
path: ../app/index.php
2 changes: 1 addition & 1 deletion tests/Rule/data/baselines-neon/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 4 errors
# total 5 errors

includes:
- another.identifier.neon
Expand Down
7 changes: 6 additions & 1 deletion tests/Rule/data/baselines-neon/sample.identifier.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# total 2 errors
# total 3 errors

parameters:
ignoreErrors:
-
message: '#^Error simple$#'
count: 2
path: ../app/file.php

-
rawMessage: Error raw message list<Foo\Bar>|null
count: 1
path: ../app/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
'count' => 2,
'path' => __DIR__ . '/../app/file.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Error raw message list<Foo\\Bar>|null',
'count' => 1,
'path' => __DIR__ . '/../app/index.php',
];

return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
2 changes: 1 addition & 1 deletion tests/Rule/data/baselines-php/loader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

// total 4 errors
// total 5 errors

return ['includes' => [
__DIR__ . '/another.identifier.php',
Expand Down
7 changes: 6 additions & 1 deletion tests/Rule/data/baselines-php/sample.identifier.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php declare(strict_types = 1);

// total 2 errors
// total 3 errors

$ignoreErrors = [];
$ignoreErrors[] = [
'message' => '#^Error simple$#',
'count' => 2,
'path' => __DIR__ . '/../app/file.php',
];
$ignoreErrors[] = [
'rawMessage' => 'Error raw message list<Foo\\Bar>|null',
'count' => 1,
'path' => __DIR__ . '/../app/index.php',
];

return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
10 changes: 8 additions & 2 deletions tests/SplitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testSplitter(): void
self::assertSame([
$folder . '/baselines/another.identifier.neon' => 1,
$folder . '/baselines/missing-identifier.neon' => 1,
$folder . '/baselines/sample.identifier.neon' => 2,
$folder . '/baselines/sample.identifier.neon' => 3,
$folder . '/baselines/loader.neon' => null,
], $written);
}
Expand All @@ -104,7 +104,7 @@ private function prepareSampleFolder(): string
}

/**
* @return array{parameters: array{ignoreErrors: array{0: array{message: string, count: int, path: string, identifier?: string}}}}
* @return array{parameters: array{ignoreErrors: array{0: array{message?: string, rawMessage?: string, count: int, path: string, identifier?: string}}}}
*/
private function getSampleErrors(): array
{
Expand All @@ -128,6 +128,12 @@ private function getSampleErrors(): array
'count' => 1,
'path' => '../app/index.php',
],
[
'rawMessage' => 'Error raw message list<Foo\\Bar>|null',
'count' => 1,
'path' => '../app/index.php',
'identifier' => 'sample.identifier',
],
],
],
];
Expand Down