1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix issue where diffs span multiple functions

This commit is contained in:
Brown 2018-10-29 17:12:20 -04:00
parent c4e540f1b9
commit 55b29e71a8
2 changed files with 133 additions and 10 deletions

View File

@ -107,6 +107,12 @@ class PartialParserVisitor extends PhpParser\NodeVisitorAbstract implements PhpP
break;
}
// we have a diff that goes outside the bounds that we care about
if ($a_e2 > $stmt_end_pos) {
$this->must_rescan = true;
return PhpParser\NodeTraverser::STOP_TRAVERSAL;
}
$end_offset = $b_e2 - $a_e2;
if ($a_s2 < $stmt_start_pos) {
@ -137,6 +143,11 @@ class PartialParserVisitor extends PhpParser\NodeVisitorAbstract implements PhpP
$stmt_end_pos - $stmt_start_pos + 1
);
if (!$method_contents) {
$this->must_rescan = true;
return PhpParser\NodeTraverser::STOP_TRAVERSAL;
}
$error_handler = new \PhpParser\ErrorHandler\Collecting();
$fake_class = "<?php class _ {" . $method_contents . "}";
@ -149,11 +160,11 @@ class PartialParserVisitor extends PhpParser\NodeVisitorAbstract implements PhpP
if (!$replacement_stmts
|| !$replacement_stmts[0] instanceof PhpParser\Node\Stmt\ClassLike
|| count($replacement_stmts[0]->stmts) > 1
|| count($replacement_stmts[0]->stmts) !== 1
) {
if ($replacement_stmts
&& $replacement_stmts[0] instanceof PhpParser\Node\Stmt\ClassLike
&& count($replacement_stmts[0]->stmts) > 1
&& count($replacement_stmts[0]->stmts) !== 1
) {
$this->must_rescan = true;
return PhpParser\NodeTraverser::STOP_TRAVERSAL;
@ -213,14 +224,7 @@ class PartialParserVisitor extends PhpParser\NodeVisitorAbstract implements PhpP
$traverseChildren = false;
$replacement_stmt = reset($replacement_stmts);
if ($replacement_stmt === false) {
$this->must_rescan = true;
return PhpParser\NodeTraverser::STOP_TRAVERSAL;
}
return $replacement_stmt;
return reset($replacement_stmts);
}
$this->must_rescan = true;

View File

@ -1139,6 +1139,55 @@ class FileDiffTest extends TestCase
['foo\a::bat'],
[[183, 7]]
],
'removeAdditionalComments' => [
'<?php
namespace Foo;
class A {
/**
* more Comments
*
* @return void
*/
public function foo() {
$a = 1;
}
/**
* more Comments
*
* @return void
*/
public function bar() {
$b = 1;
}
}',
'<?php
namespace Foo;
use D;
use E;
class A {
/**
* @return void
*/
public function foo() {
$c = 1;
}
/**
* @return void
*/
public function bar() {
$a = 1;
}
}',
[],
[],
['foo\a::foo', 'foo\a::bar', 'foo\a::foo', 'foo\a::bar'],
[]
],
'SKIPPED-whiteSpaceOnly' => [
'<?php
namespace Foo;
@ -1445,6 +1494,76 @@ class FileDiffTest extends TestCase
['use:Exception'],
[[-36, -2]]
],
'vimeoDiff' => [
'<?php
namespace C;
class A extends B
{
/**
* Another thing
*
* @return D
*/
public function foo() {
$a = 1;
$b = 2;
}
/**
* Other thing
*
* @return D
*/
public function bar() {
$a = 1;
$b = 2;
}
/**
* Some thing
*
* @return D
*/
public function zap() {
$a = 1;
$b = 2;
}
/**
* @return Foo
*/
private function top(
D $c
) {
return $c;
}
}
',
'<?php
namespace C;
class A extends B
{
/**
* @return D
*/
public function rot() {
$c = 1;
}
/**
* @return D
*/
public function bar() {
return $c;
}
}',
[],
[],
['c\a::foo', 'c\a::bar', 'c\a::zap', 'c\a::top', 'c\a::rot', 'c\a::bar'],
[]
],
];
}
}