Skip to content
Permalink
Browse files

Fix #1574 - detect docblock additions to the first statement of a met…

…hod when diffing
  • Loading branch information...
muglug committed Apr 23, 2019
1 parent 473c8cb commit c657a45132883092648657913eb09e2ef91f06e5
Showing with 101 additions and 5 deletions.
  1. +30 −5 src/Psalm/Internal/Diff/ClassStatementsDiffer.php
  2. +29 −0 tests/FileDiffTest.php
  3. +42 −0 tests/FileUpdates/ErrorFixTest.php
@@ -82,7 +82,9 @@ function (
$a_size = $a_end - $a_start;
$b_size = $b_end - $b_start;
if (substr($a_code, $a_start, $a_size) === substr($b_code, $b_start, $b_size)) {
if ($a_size === $b_size
&& substr($a_code, $a_start, $a_size) === substr($b_code, $b_start, $b_size)
) {
$start_diff = $b_start - $a_start;
$line_diff = $b->getLine() - $a->getLine();
@@ -103,11 +105,34 @@ function (
return false;
}
$a_stmts_start = $a->stmts ? (int) $a->stmts[0]->getAttribute('startFilePos') : $a_end;
$b_stmts_start = $b->stmts ? (int) $b->stmts[0]->getAttribute('startFilePos') : $b_end;
if ($a->stmts) {
$first_stmt = $a->stmts[0];
$a_stmts_start = (int) $first_stmt->getAttribute('startFilePos');
if ($a_stmt_comments = $first_stmt->getComments()) {
$a_stmts_start = $a_stmt_comments[0]->getFilePos();
}
} else {
$a_stmts_start = $a_end;
}
if ($b->stmts) {
$first_stmt = $b->stmts[0];
$b_stmts_start = (int) $first_stmt->getAttribute('startFilePos');
if ($b_stmt_comments = $first_stmt->getComments()) {
$b_stmts_start = $b_stmt_comments[0]->getFilePos();
}
} else {
$b_stmts_start = $b_end;
}
$a_body_size = $a_end - $a_stmts_start;
$b_body_size = $b_end - $b_stmts_start;
$body_change = substr($a_code, $a_stmts_start, $a_end - $a_stmts_start)
!== substr($b_code, $b_stmts_start, $b_end - $b_stmts_start);
$body_change = $a_body_size !== $b_body_size
|| substr($a_code, $a_stmts_start, $a_end - $a_stmts_start)
!== substr($b_code, $b_stmts_start, $b_end - $b_stmts_start);
if (!$signature_change) {
$a_signature = substr($a_code, $a_start, $a_stmts_start - $a_start);
@@ -1497,6 +1497,35 @@ public function foo() : void {
['use:Exception'],
[[-36, -2]],
],
'addDocblockToFirstFunctionStatement' => [
'<?php
namespace Foo;
class C {
public function foo(array $a) : void {
foreach ($a as $b) {
$b->bar();
}
}
}',
'<?php
namespace Foo;
class C {
public function foo(array $a) : void {
/**
* @psalm-suppress MixedAssignment
*/
foreach ($a as $b) {
$b->bar();
}
}
}',
[],
['foo\c::foo'],
[],
[],
],
'vimeoDiff' => [
'<?php
namespace C;
@@ -326,6 +326,48 @@ public function bar() : string {
],
'error_counts' => [1, 0, 0],
],
'addSuppressions' => [
'start_files' => [
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
class C {
public function foo(array $a) : void {
foreach ($a as $b) {
$b->bar();
}
}
}',
],
'middle_files' => [
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
class C {
public function foo(array $a) : void {
/**
* @psalm-suppress MixedAssignment
*/
foreach ($a as $b) {
$b->bar();
}
}
}',
],
'end_files' => [
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
class C {
public function foo(array $a) : void {
/**
* @psalm-suppress MixedAssignment
*/
foreach ($a as $b) {
/**
* @psalm-suppress MixedMethodCall
*/
$b->bar();
}
}
}',
],
'error_counts' => [2, 1, 0],
],
];
}
}

0 comments on commit c657a45

Please sign in to comment.
You can’t perform that action at this time.