Skip to content

Commit beb79ba

Browse files
Antanas Strumilamicheh
authored andcommitted
Preserve same fingerprint after issue moved to another line
1 parent 13fdf71 commit beb79ba

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/Report/Gitlab.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Reports\Report;
14+
use SplFileObject;
1415

1516
use function md5;
1617
use function rtrim;
@@ -28,13 +29,18 @@ public function generateFileReport($report, File $phpcsFile, $showSources = fals
2829
$hasOutput = false;
2930

3031
foreach ($report['messages'] as $line => $lineErrors) {
32+
$file = new SplFileObject($phpcsFile->getFilename());
3133
foreach ($lineErrors as $column => $colErrors) {
3234
foreach ($colErrors as $error) {
3335
$issue = [
3436
'type' => 'issue',
3537
'categories' => ['Style'],
3638
'check_name' => $error['source'],
37-
'fingerprint' => md5($report['filename'] . $error['message'] . $line . $column),
39+
'fingerprint' => md5(
40+
$report['filename']
41+
. $error["source"]
42+
. $this->getRelevantSource($file, $line - 1)
43+
),
3844
'severity' => $error['type'] === 'ERROR' ? 'major' : 'minor',
3945
'description' => str_replace(["\n", "\r", "\t"], ['\n', '\r', '\t'], $error['message']),
4046
'location' => [
@@ -68,4 +74,18 @@ public function generate(
6874
) {
6975
echo '[' . rtrim($cachedData, ',') . ']' . PHP_EOL;
7076
}
77+
78+
private function getRelevantSource(SplFileObject $file, int $line): string
79+
{
80+
if (!$file->eof()) {
81+
$file->seek($line);
82+
$contents = $file->current();
83+
84+
if (false !== $contents) {
85+
return preg_replace('/\s+/', '', $contents);
86+
}
87+
}
88+
89+
return '';
90+
}
7191
}

tests/Fixtures/MixedViolations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public function getReportData(): array
4747

4848
public function getExpectedOutput(): string
4949
{
50-
return '{"type":"issue","categories":["Style"],"check_name":"PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeReturnType","fingerprint":"766bf1f7eff186fd667447bfb0291078","severity":"major","description":"There must be a single space between the colon and type in a return type declaration","location":{"path":"tests/Report/GitlabTest.php","lines":{"begin":23,"end":23}}},{"type":"issue","categories":["Style"],"check_name":"Generic.Files.LineLength.TooLong","fingerprint":"517eda4b85a33cd09fc933259f873191","severity":"minor","description":"Line exceeds 120 characters; contains 124 characters","location":{"path":"tests/Report/GitlabTest.php","lines":{"begin":34,"end":34}}},';
50+
return '{"type":"issue","categories":["Style"],"check_name":"PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeReturnType","fingerprint":"0f7fd875e9d8ce4260a335195f184ae8","severity":"major","description":"There must be a single space between the colon and type in a return type declaration","location":{"path":"tests/Report/GitlabTest.php","lines":{"begin":23,"end":23}}},{"type":"issue","categories":["Style"],"check_name":"Generic.Files.LineLength.TooLong","fingerprint":"ba24219048019211e5a36d99b23a0428","severity":"minor","description":"Line exceeds 120 characters; contains 124 characters","location":{"path":"tests/Report/GitlabTest.php","lines":{"begin":34,"end":34}}},';
5151
}
5252
}

tests/Fixtures/SingleViolation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public function getReportData(): array
3636

3737
public function getExpectedOutput(): string
3838
{
39-
return '{"type":"issue","categories":["Style"],"check_name":"PSR12.Files.FileHeader.SpacingInsideBlock","fingerprint":"21f4cd5b7d88d9eb67402be5d56caa8b","severity":"major","description":"Header blocks must not contain blank lines","location":{"path":"src/Report/Gitlab.php","lines":{"begin":18,"end":18}}},';
39+
return '{"type":"issue","categories":["Style"],"check_name":"PSR12.Files.FileHeader.SpacingInsideBlock","fingerprint":"b032bdbf36024c6287172cef1f54a6fd","severity":"major","description":"Header blocks must not contain blank lines","location":{"path":"src/Report/Gitlab.php","lines":{"begin":18,"end":18}}},';
4040
}
4141
}

tests/Report/GitlabTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ public function testGenerateFileReport(string $class): void
6767
{
6868
$fixture = new $class();
6969
self::assertInstanceOf(ReportFixture::class, $fixture);
70+
$fixtureReflection = new \ReflectionClass($class);
71+
$phpcsFile = $this->createMock(File::class);
72+
$phpcsFile->method('getFilename')->willReturn($fixtureReflection->getFileName());
7073

7174
$this->expectOutputString($fixture->getExpectedOutput());
72-
$this->report->generateFileReport($fixture->getReportData(), $this->createMock(File::class));
75+
$this->report->generateFileReport($fixture->getReportData(), $phpcsFile);
7376
}
7477
}

0 commit comments

Comments
 (0)