Skip to content

Commit 2664558

Browse files
committed
Use content instead of line number for the fingerprint
1 parent 13fdf71 commit 2664558

File tree

16 files changed

+144
-129
lines changed

16 files changed

+144
-129
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- uses: shivammathur/setup-php@2.31.1
2727
with:
2828
php-version: ${{ matrix.php-versions }}
29-
coverage: pcov
29+
coverage: xdebug
3030
if: matrix.php-versions == '7.4' && matrix.dependencies == 'highest'
3131

3232
- name: Setup problem matcher for PHPUnit

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"require-dev": {
2525
"phpstan/phpstan": "^2.0",
2626
"phpunit/phpunit": "^8.5.14 || ^9.0",
27-
"squizlabs/php_codesniffer": "^3.3.1"
27+
"squizlabs/php_codesniffer": "^3.5.0"
2828
},
2929
"autoload": {
3030
"psr-4": {

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515

1616
<file>src</file>
1717
<file>tests</file>
18+
<exclude-pattern>tests/_files/*</exclude-pattern>
1819
</ruleset>

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ parameters:
44
paths:
55
- src
66
- tests
7+
excludePaths:
8+
- tests/_files/
79
scanDirectories:
810
- vendor/squizlabs/php_codesniffer

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
colors="true"
88
forceCoversAnnotation="true"
99
>
10+
<php>
11+
<const name="PHP_CODESNIFFER_IN_TESTS" value="true"/>
12+
<const name="PHP_CODESNIFFER_CBF" value="false"/>
13+
</php>
14+
1015
<testsuites>
1116
<testsuite name="Project Test Suite">
1217
<directory>tests</directory>

src/Report/Gitlab.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

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

16+
use function is_string;
1517
use function md5;
18+
use function preg_replace;
1619
use function rtrim;
1720
use function str_replace;
1821

@@ -26,15 +29,25 @@ class Gitlab implements Report
2629
public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80)
2730
{
2831
$hasOutput = false;
32+
$fingerprints = [];
2933

3034
foreach ($report['messages'] as $line => $lineErrors) {
31-
foreach ($lineErrors as $column => $colErrors) {
35+
$lineContent = $this->getContentOfLine($phpcsFile->getFilename(), $line);
36+
37+
foreach ($lineErrors as $colErrors) {
3238
foreach ($colErrors as $error) {
39+
$fingerprint = md5($report['filename'] . $lineContent . $error['source']);
40+
if (isset($fingerprints[$fingerprint])) {
41+
++$fingerprints[$fingerprint];
42+
} else {
43+
$fingerprints[$fingerprint] = 1;
44+
}
45+
3346
$issue = [
3447
'type' => 'issue',
3548
'categories' => ['Style'],
3649
'check_name' => $error['source'],
37-
'fingerprint' => md5($report['filename'] . $error['message'] . $line . $column),
50+
'fingerprint' => $fingerprint . '-' . $fingerprints[$fingerprint],
3851
'severity' => $error['type'] === 'ERROR' ? 'major' : 'minor',
3952
'description' => str_replace(["\n", "\r", "\t"], ['\n', '\r', '\t'], $error['message']),
4053
'location' => [
@@ -68,4 +81,25 @@ public function generate(
6881
) {
6982
echo '[' . rtrim($cachedData, ',') . ']' . PHP_EOL;
7083
}
84+
85+
/**
86+
* @param string $filename
87+
* @param int $line
88+
* @return string
89+
*/
90+
private function getContentOfLine($filename, $line)
91+
{
92+
$file = new SplFileObject($filename);
93+
94+
if (!$file->eof()) {
95+
$file->seek($line - 1);
96+
$contents = $file->current();
97+
98+
if (is_string($contents)) {
99+
return (string) preg_replace('/\s+/', '', $contents);
100+
}
101+
}
102+
103+
return '';
104+
}
71105
}

tests/Fixtures/MixedViolations.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

tests/Fixtures/ReportFixture.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/Fixtures/SingleViolation.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/Report/GitlabTest.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
namespace MichehTest\PhpCodeSniffer\Report;
1111

1212
use Micheh\PhpCodeSniffer\Report\Gitlab;
13-
use MichehTest\PhpCodeSniffer\Fixtures\MixedViolations;
14-
use MichehTest\PhpCodeSniffer\Fixtures\ReportFixture;
15-
use MichehTest\PhpCodeSniffer\Fixtures\SingleViolation;
13+
use PHP_CodeSniffer\Config;
1614
use PHP_CodeSniffer\Files\File;
15+
use PHP_CodeSniffer\Files\LocalFile;
16+
use PHP_CodeSniffer\Reporter;
17+
use PHP_CodeSniffer\Ruleset;
18+
use PHP_CodeSniffer\Runner;
1719
use PHPUnit\Framework\TestCase;
20+
use Throwable;
21+
22+
use function file_get_contents;
1823

1924
class GitlabTest extends TestCase
2025
{
@@ -23,6 +28,26 @@ class GitlabTest extends TestCase
2328
*/
2429
private $report;
2530

31+
/**
32+
* @var Config
33+
*/
34+
private static $config;
35+
36+
37+
public static function setUpBeforeClass(): void
38+
{
39+
self::$config = new Config();
40+
self::$config->basepath = __DIR__ . '/../';
41+
self::$config->standards = ['PSR12'];
42+
43+
try {
44+
$runner = new Runner();
45+
$runner->config = self::$config;
46+
$runner->init();
47+
} catch (Throwable $exception) {
48+
49+
}
50+
}
2651

2752
protected function setUp(): void
2853
{
@@ -48,27 +73,49 @@ public function testGenerateWithEmpty(): void
4873
}
4974

5075
/**
51-
* @return array<string, array<class-string<ReportFixture>>>
76+
* @return array<string, array<string>>
5277
*/
5378
public function violations(): array
5479
{
5580
return [
56-
'single' => [SingleViolation::class],
57-
'mixed' => [MixedViolations::class],
81+
'single' => ['Single'],
82+
'mixed' => ['Mixed'],
83+
'multiple' => ['Multiple'],
5884
];
5985
}
6086

6187
/**
62-
* @param class-string<ReportFixture> $class
6388
* @covers \Micheh\PhpCodeSniffer\Report\Gitlab::generateFileReport
6489
* @dataProvider violations
6590
*/
66-
public function testGenerateFileReport(string $class): void
91+
public function testGenerateFileReport(string $fileName): void
92+
{
93+
$phpPath = __DIR__ . '/../_files/' . $fileName . '.php';
94+
self::assertFileExists($phpPath);
95+
96+
$outputPath = __DIR__ . '/../_files/' . $fileName . '.json';
97+
self::assertFileExists($outputPath);
98+
99+
$file = $this->createFile($phpPath);
100+
101+
$this->expectOutputString((string) file_get_contents($outputPath));
102+
$this->report->generateFileReport($this->getReportData($file), $file);
103+
}
104+
105+
private function createFile(string $path): File
67106
{
68-
$fixture = new $class();
69-
self::assertInstanceOf(ReportFixture::class, $fixture);
107+
$file = new LocalFile($path, new Ruleset(self::$config), self::$config);
108+
$file->process();
70109

71-
$this->expectOutputString($fixture->getExpectedOutput());
72-
$this->report->generateFileReport($fixture->getReportData(), $this->createMock(File::class));
110+
return $file;
111+
}
112+
113+
/**
114+
* @return array{filename: string, errors: int, warnings: int, fixable: int, messages: array<mixed>}
115+
*/
116+
private function getReportData(File $file): array
117+
{
118+
$reporter = new Reporter(self::$config);
119+
return $reporter->prepareFileReport($file); // @phpstan-ignore return.type
73120
}
74121
}

0 commit comments

Comments
 (0)