1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #1991 - allow overlapping code to subsume manipulations inside

This commit is contained in:
Matthew Brown 2019-08-08 22:58:54 -04:00
parent cf53f5d825
commit 53817ed40b
3 changed files with 33 additions and 9 deletions

View File

@ -1233,11 +1233,12 @@ class Analyzer
*/
public function updateFile($file_path, $dry_run)
{
$new_return_type_manipulations = FunctionDocblockManipulator::getManipulationsForFile($file_path);
FileManipulationBuffer::add(
$file_path,
FunctionDocblockManipulator::getManipulationsForFile($file_path)
);
$other_manipulations = FileManipulationBuffer::getManipulationsForFile($file_path);
$file_manipulations = array_merge($new_return_type_manipulations, $other_manipulations);
$file_manipulations = FileManipulationBuffer::getManipulationsForFile($file_path);
if (!$file_manipulations) {
return;
@ -1249,22 +1250,27 @@ class Analyzer
* @return int
*/
function (FileManipulation $a, FileManipulation $b) {
if ($a->start === $b->start) {
if ($b->end === $a->end) {
if ($b->end === $a->end) {
if ($a->start === $b->start) {
return $b->insertion_text > $a->insertion_text ? 1 : -1;
}
return $b->end > $a->end ? 1 : -1;
return $b->start > $a->start ? 1 : -1;
}
return $b->start > $a->start ? 1 : -1;
return $b->end > $a->end ? 1 : -1;
}
);
$last_start = \PHP_INT_MAX;
$existing_contents = $this->file_provider->getContents($file_path);
foreach ($file_manipulations as $manipulation) {
$existing_contents = $manipulation->transform($existing_contents);
if ($manipulation->start <= $last_start) {
$existing_contents = $manipulation->transform($existing_contents);
$last_start = $manipulation->start;
}
}
if ($dry_run) {

View File

@ -125,6 +125,7 @@ class Reconciler
$new_base_key = $base_key . '[' . $array_key . ']';
if (strpos($array_key, '\'') !== false) {
$new_types[$base_key][] = ['~array', '~ArrayAccess'];
$new_types[$base_key][] = ['!string'];
$new_types[$base_key][] = ['!=falsy'];
}

View File

@ -348,6 +348,23 @@ class UnusedCodeManipulationTest extends FileManipulationTest
['PossiblyUnusedMethod'],
true,
],
'removePossiblyUnusedMethodAndMissingReturnType' => [
'<?php
class A {
public function foo() {}
}
new A();',
'<?php
class A {
}
new A();',
'7.1',
['PossiblyUnusedMethod', 'MissingReturnType'],
true,
],
'removePossiblyUnusedPropertyWithDocblock' => [
'<?php
class A {