Skip to content

Commit b176480

Browse files
authored
fix(ValueObject): FileDiff first line number is below as expected (#6746)
Signed-off-by: Emilien Escalle <[email protected]>
1 parent e8d16b9 commit b176480

File tree

18 files changed

+62
-30
lines changed

18 files changed

+62
-30
lines changed

e2e/applied-polyfill-php80/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) src/SomeStartWith.php:3
4+
1) src/SomeStartWith.php:4
55

66
---------- begin diff ----------
77
@@ @@

e2e/applied-rule-change-docblock/expected-output.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
2 files with changes
22
====================
33

4-
1) src/RenameDocblock.php:0
4+
1) src/RenameDocblock.php:1
55

66
---------- begin diff ----------
77
@@ @@
@@ -19,7 +19,7 @@ Applied rules:
1919
* RenameClassRector
2020

2121

22-
2) src/UselessVarTag.php:1
22+
2) src/UselessVarTag.php:2
2323

2424
---------- begin diff ----------
2525
@@ @@

e2e/applied-rule-removed-node-with-cache/expected-output.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
2 files with changes
22
====================
33

4-
1) src/AlwaysTrue.php:3
4+
1) src/AlwaysTrue.php:4
55

66
---------- begin diff ----------
77
@@ @@
@@ -20,7 +20,7 @@ Applied rules:
2020
* RemoveAlwaysTrueIfConditionRector
2121

2222

23-
2) src/DeadConstructor.php:1
23+
2) src/DeadConstructor.php:2
2424

2525
---------- begin diff ----------
2626
@@ @@

e2e/applied-rule-removed-node/expected-output.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
2 files with changes
22
====================
33

4-
1) src/AlwaysTrue.php:3
4+
1) src/AlwaysTrue.php:4
55

66
---------- begin diff ----------
77
@@ @@
@@ -20,7 +20,7 @@ Applied rules:
2020
* RemoveAlwaysTrueIfConditionRector
2121

2222

23-
2) src/DeadConstructor.php:1
23+
2) src/DeadConstructor.php:2
2424

2525
---------- begin diff ----------
2626
@@ @@

e2e/applied-rule-return-array-nodes/expected-output.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
2 files with changes
22
====================
33

4-
1) src/MultiRules.php:5
4+
1) src/MultiRules.php:6
55

66
---------- begin diff ----------
77
@@ @@
@@ -25,7 +25,7 @@ Applied rules:
2525
* RemoveAlwaysElseRector
2626

2727

28-
2) src/RemoveAlwaysElse.php:5
28+
2) src/RemoveAlwaysElse.php:6
2929

3030
---------- begin diff ----------
3131
@@ @@

e2e/different-path-over-skip-config/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) src/models/DeadConstructor.php:3
4+
1) src/models/DeadConstructor.php:4
55

66
---------- begin diff ----------
77
@@ @@

e2e/no-parallel-reflection-resolver/expected-output.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
2 files with changes
22
====================
33

4-
1) src/NamespacedSomeClassFound.php:5
4+
1) src/NamespacedSomeClassFound.php:6
55

66
---------- begin diff ----------
77
@@ @@
@@ -16,7 +16,7 @@ Applied rules:
1616
* RemoveUnusedPrivatePropertyRector
1717

1818

19-
2) src/SomeClassNotFound.php:3
19+
2) src/SomeClassNotFound.php:4
2020

2121
---------- begin diff ----------
2222
@@ @@

e2e/only-option-quote-double-equalnone/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) ../only-option/src/MultiRules.php:9
4+
1) ../only-option/src/MultiRules.php:10
55

66
---------- begin diff ----------
77
@@ @@

e2e/only-option-quote-single-bsdouble/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) ../only-option/src/MultiRules.php:9
4+
1) ../only-option/src/MultiRules.php:10
55

66
---------- begin diff ----------
77
@@ @@

e2e/only-option-quote-single-equalnone/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) ../only-option/src/MultiRules.php:9
4+
1) ../only-option/src/MultiRules.php:10
55

66
---------- begin diff ----------
77
@@ @@

e2e/only-option-quote-single/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) ../only-option/src/MultiRules.php:9
4+
1) ../only-option/src/MultiRules.php:10
55

66
---------- begin diff ----------
77
@@ @@

e2e/only-option/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) src/MultiRules.php:9
4+
1) src/MultiRules.php:10
55

66
---------- begin diff ----------
77
@@ @@

e2e/parallel-custom-config/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) src/SomeClass.php:6
4+
1) src/SomeClass.php:7
55

66
---------- begin diff ----------
77
@@ @@

e2e/parallel-reflection-resolver/expected-output.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
2 files with changes
22
====================
33

4-
1) src/NamespacedSomeClassFound.php:5
4+
1) src/NamespacedSomeClassFound.php:6
55

66
---------- begin diff ----------
77
@@ @@
@@ -16,7 +16,7 @@ Applied rules:
1616
* RemoveUnusedPrivatePropertyRector
1717

1818

19-
2) src/SomeClassNotFound.php:3
19+
2) src/SomeClassNotFound.php:4
2020

2121
---------- begin diff ----------
2222
@@ @@

e2e/print-new-node/expected-output.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
1 file with changes
22
===================
33

4-
1) src/ExtendingTestClass.php:0
4+
1) src/ExtendingTestClass.php:1
55

66
---------- begin diff ----------
77
@@ @@

rules/Php53/Rector/Ternary/TernaryToElvisRector.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public function refactor(Node $node): ?Node
7070
return $node;
7171
}
7272

73+
public function provideMinPhpVersion(): int
74+
{
75+
return PhpVersionFeature::ELVIS_OPERATOR;
76+
}
77+
7378
private function isParenthesized(Expr $ifExpr, Expr $elseExpr): bool
7479
{
7580
$tokens = $this->file->getOldTokens();
@@ -95,9 +100,4 @@ private function isParenthesized(Expr $ifExpr, Expr $elseExpr): bool
95100

96101
return false;
97102
}
98-
99-
public function provideMinPhpVersion(): int
100-
{
101-
return PhpVersionFeature::ELVIS_OPERATOR;
102-
}
103103
}

src/ValueObject/Reporting/FileDiff.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
{
1616
/**
1717
* @var string
18-
* @se https://regex101.com/r/AUPIX4/1
18+
* @see https://en.wikipedia.org/wiki/Diff#Unified_format
19+
* @see https://regex101.com/r/AUPIX4/1
1920
*/
20-
private const FIRST_LINE_REGEX = '#@@(.*?)(?<' . self::FIRST_LINE_KEY . '>\d+)(.*?)@@#';
21+
private const DIFF_HUNK_HEADER_REGEX = '#@@(.*?)(?<' . self::FIRST_LINE_KEY . '>\d+)(.*?)@@#';
2122

2223
/**
2324
* @var string
@@ -93,14 +94,14 @@ public function getRectorClasses(): array
9394

9495
public function getFirstLineNumber(): ?int
9596
{
96-
$match = Strings::match($this->diff, self::FIRST_LINE_REGEX);
97+
$match = Strings::match($this->diff, self::DIFF_HUNK_HEADER_REGEX);
9798

9899
// probably some error in diff
99100
if (! isset($match[self::FIRST_LINE_KEY])) {
100101
return null;
101102
}
102103

103-
return (int) $match[self::FIRST_LINE_KEY] - 1;
104+
return (int) $match[self::FIRST_LINE_KEY];
104105
}
105106

106107
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\ValueObject\Reporting;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\ValueObject\Reporting\FileDiff;
9+
10+
final class FileDiffTest extends TestCase
11+
{
12+
public function testGetFirstLineNumberShouldReturnFirstLineNumberRegardingHunk(): void
13+
{
14+
$fileDiff = new FileDiff(
15+
'some/file.php',
16+
'--- Original\n+++ New\n@@ -38,5 +39,6 @@\nreturn true;\n}\n',
17+
'diff console formatted'
18+
);
19+
$this->assertSame(38, $fileDiff->getFirstLineNumber());
20+
}
21+
22+
public function testGetFirstLineNumberShouldBeNullWhenHunkIsInvalid(): void
23+
{
24+
$fileDiff = new FileDiff(
25+
'some/file.php',
26+
'--- Original\n+++ New\n@@@@\nreturn true;\n}\n',
27+
'diff console formatted'
28+
);
29+
$this->assertNull($fileDiff->getFirstLineNumber());
30+
}
31+
}

0 commit comments

Comments
 (0)