Skip to content

Commit 45aeaaa

Browse files
committed
Add the line & column to the fingerprint only if the original fingerprint appears multiple times
1 parent bd60447 commit 45aeaaa

File tree

7 files changed

+32
-14
lines changed

7 files changed

+32
-14
lines changed

src/Report/Gitlab.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Reports\Report;
1414
use SplFileObject;
1515

16+
use function count;
1617
use function is_string;
1718
use function md5;
1819
use function preg_replace;
@@ -29,25 +30,20 @@ class Gitlab implements Report
2930
public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80)
3031
{
3132
$hasOutput = false;
32-
$fingerprints = [];
33+
$violations = [];
3334

3435
foreach ($report['messages'] as $line => $lineErrors) {
3536
$lineContent = $this->getContentOfLine($phpcsFile->getFilename(), $line);
3637

37-
foreach ($lineErrors as $colErrors) {
38+
foreach ($lineErrors as $col => $colErrors) {
3839
foreach ($colErrors as $error) {
3940
$fingerprint = md5($report['filename'] . $lineContent . $error['source']);
40-
if (isset($fingerprints[$fingerprint])) {
41-
++$fingerprints[$fingerprint];
42-
} else {
43-
$fingerprints[$fingerprint] = 1;
44-
}
4541

46-
$issue = [
42+
$violations[$fingerprint][$line . $col] = [
4743
'type' => 'issue',
4844
'categories' => ['Style'],
4945
'check_name' => $error['source'],
50-
'fingerprint' => $fingerprint . '-' . $fingerprints[$fingerprint],
46+
'fingerprint' => $fingerprint,
5147
'severity' => $error['type'] === 'ERROR' ? 'major' : 'minor',
5248
'description' => str_replace(["\n", "\r", "\t"], ['\n', '\r', '\t'], $error['message']),
5349
'location' => [
@@ -58,10 +54,19 @@ public function generateFileReport($report, File $phpcsFile, $showSources = fals
5854
]
5955
],
6056
];
57+
}
58+
}
59+
}
6160

62-
echo json_encode($issue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ',';
63-
$hasOutput = true;
61+
foreach ($violations as $fingerprints) {
62+
$hasMultiple = count($fingerprints) > 1;
63+
foreach ($fingerprints as $lineColumn => $issue) {
64+
if ($hasMultiple) {
65+
$issue['fingerprint'] = md5($issue['fingerprint'] . $lineColumn);
6466
}
67+
68+
echo json_encode($issue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ',';
69+
$hasOutput = true;
6570
}
6671
}
6772

tests/Report/GitlabTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static function violations(): array
7676
'single' => ['Single'],
7777
'mixed' => ['Mixed'],
7878
'multiple' => ['Multiple'],
79+
'same-line' => ['SameLine'],
7980
];
8081
}
8182

tests/_files/Mixed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"type":"issue","categories":["Style"],"check_name":"Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine","fingerprint":"0da49d5a2f6b30f151e4abef2fccd4e2-1","severity":"major","description":"Opening brace should be on a new line","location":{"path":"_files/Mixed.php","lines":{"begin":7,"end":7}}},{"type":"issue","categories":["Style"],"check_name":"Generic.Files.LineLength.TooLong","fingerprint":"0be4b9620fcab36dcef7ba50bd3ac097-1","severity":"minor","description":"Line exceeds 120 characters; contains 143 characters","location":{"path":"_files/Mixed.php","lines":{"begin":8,"end":8}}},
1+
{"type":"issue","categories":["Style"],"check_name":"Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine","fingerprint":"0da49d5a2f6b30f151e4abef2fccd4e2","severity":"major","description":"Opening brace should be on a new line","location":{"path":"_files/Mixed.php","lines":{"begin":7,"end":7}}},{"type":"issue","categories":["Style"],"check_name":"Generic.Files.LineLength.TooLong","fingerprint":"0be4b9620fcab36dcef7ba50bd3ac097","severity":"minor","description":"Line exceeds 120 characters; contains 143 characters","location":{"path":"_files/Mixed.php","lines":{"begin":8,"end":8}}},

tests/_files/Multiple.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"type":"issue","categories":["Style"],"check_name":"PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket","fingerprint":"e187fcc76a1b52d178447fea5370dfb1-1","severity":"major","description":"Space after opening parenthesis of function call prohibited","location":{"path":"_files/Multiple.php","lines":{"begin":11,"end":11}}},{"type":"issue","categories":["Style"],"check_name":"PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket","fingerprint":"e187fcc76a1b52d178447fea5370dfb1-2","severity":"major","description":"Space after opening parenthesis of function call prohibited","location":{"path":"_files/Multiple.php","lines":{"begin":12,"end":12}}},
1+
{"type":"issue","categories":["Style"],"check_name":"PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket","fingerprint":"06b1cc1d00c91fec6c5a1cb76bee5dd2","severity":"major","description":"Space after opening parenthesis of function call prohibited","location":{"path":"_files/Multiple.php","lines":{"begin":11,"end":11}}},{"type":"issue","categories":["Style"],"check_name":"PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket","fingerprint":"801a0665f60767260b1c67a3e58ff5b4","severity":"major","description":"Space after opening parenthesis of function call prohibited","location":{"path":"_files/Multiple.php","lines":{"begin":12,"end":12}}},

tests/_files/SameLine.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"type":"issue","categories":["Style"],"check_name":"PSR12.Operators.OperatorSpacing.NoSpaceBefore","fingerprint":"b1ea69031496b62b007b76e45078839f","severity":"major","description":"Expected at least 1 space before \".\"; 0 found","location":{"path":"_files/SameLine.php","lines":{"begin":9,"end":9}}},{"type":"issue","categories":["Style"],"check_name":"PSR12.Operators.OperatorSpacing.NoSpaceBefore","fingerprint":"13fd4b267a197c14c8b40afb777ec6cf","severity":"major","description":"Expected at least 1 space before \".\"; 0 found","location":{"path":"_files/SameLine.php","lines":{"begin":9,"end":9}}},

tests/_files/SameLine.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DoNotUse;
4+
5+
class SameLine
6+
{
7+
public function __invoke(): string
8+
{
9+
return 'same'. 'line'. 'twice';
10+
}
11+
}

tests/_files/Single.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"type":"issue","categories":["Style"],"check_name":"Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine","fingerprint":"98816311d045b285f10765696fa38858-1","severity":"major","description":"Opening brace should be on a new line","location":{"path":"_files/Single.php","lines":{"begin":7,"end":7}}},
1+
{"type":"issue","categories":["Style"],"check_name":"Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine","fingerprint":"98816311d045b285f10765696fa38858","severity":"major","description":"Opening brace should be on a new line","location":{"path":"_files/Single.php","lines":{"begin":7,"end":7}}},

0 commit comments

Comments
 (0)