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

Fix #1634 - don’t remove existing return types when preventing bc breaks

This commit is contained in:
Matthew Brown 2019-05-15 00:34:56 -04:00
parent a34692fca0
commit cfff66efb7
4 changed files with 77 additions and 17 deletions

View File

@ -263,7 +263,7 @@ class ReturnTypeAnalyzer
$source,
$function_like_analyzer,
($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock,
$function_like_storage
);
@ -300,9 +300,9 @@ class ReturnTypeAnalyzer
$source,
$function_like_analyzer,
$compatible_method_ids
|| (($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock),
|| (($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock),
$function_like_storage
);
@ -356,8 +356,8 @@ class ReturnTypeAnalyzer
$function_like_analyzer,
$compatible_method_ids
|| (($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock)
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock)
);
return null;
@ -448,7 +448,7 @@ class ReturnTypeAnalyzer
$source,
$function_like_analyzer,
($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock,
$function_like_storage
);
@ -485,9 +485,9 @@ class ReturnTypeAnalyzer
$source,
$function_like_analyzer,
$compatible_method_ids
|| (($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock),
|| (($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock),
$function_like_storage
);
@ -542,7 +542,7 @@ class ReturnTypeAnalyzer
$source,
$function_like_analyzer,
($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock,
$function_like_storage
);
@ -578,7 +578,7 @@ class ReturnTypeAnalyzer
$source,
$function_like_analyzer,
($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock,
$function_like_storage
);
@ -736,11 +736,12 @@ class ReturnTypeAnalyzer
&& (
$codebase->allow_backwards_incompatible_changes
|| $is_final
|| !$function instanceof PhpParser\Node\Stmt\ClassMethod
);
$manipulator->setReturnType(
$allow_native_type
? $inferred_return_type->toPhpString(
? (string) $inferred_return_type->toPhpString(
$source->getNamespace(),
$source->getAliasedClassesFlipped(),
$source->getFQCLN(),

View File

@ -354,7 +354,8 @@ class FunctionDocblockManipulator
': ' . $manipulator->new_php_return_type
);
}
} elseif ($manipulator->return_typehint_colon_start
} elseif ($manipulator->new_php_return_type === ''
&& $manipulator->return_typehint_colon_start
&& $manipulator->new_phpdoc_return_type
&& $manipulator->return_typehint_start
&& $manipulator->return_typehint_end

View File

@ -34,8 +34,14 @@ abstract class FileManipulationTest extends \Psalm\Tests\TestCase
*
* @return void
*/
public function testValidCode($input_code, $output_code, $php_version, array $issues_to_fix, $safe_types, bool $allow_backwards_incompatible_changes = true)
{
public function testValidCode(
$input_code,
$output_code,
$php_version,
array $issues_to_fix,
$safe_types,
bool $allow_backwards_incompatible_changes = true
) {
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');

View File

@ -8,7 +8,7 @@ use Psalm\Tests\Internal\Provider;
class ReturnTypeManipulationTest extends FileManipulationTest
{
/**
* @return array<string,array{string,string,string,string[],bool}>
* @return array<string,array{string,string,string,string[],bool,5?:bool}>
*/
public function providerValidCodeParse()
{
@ -1226,6 +1226,58 @@ class ReturnTypeManipulationTest extends FileManipulationTest
['MissingReturnType'],
false,
],
'dontReplaceValidReturnTypePreventingBackwardsIncompatibility' => [
'<?php
class A {
/**
* @return int[]|null
*/
public function foo(): ?array {
return ["hello"];
}
}',
'<?php
class A {
/**
* @return string[]
*
* @psalm-return array{0:string}
*/
public function foo(): ?array {
return ["hello"];
}
}',
'7.3',
['InvalidReturnType'],
false,
false
],
'dontReplaceValidReturnTypeAllowBackwardsIncompatibility' => [
'<?php
class A {
/**
* @return int[]|null
*/
public function foo(): ?array {
return ["hello"];
}
}',
'<?php
class A {
/**
* @return string[]
*
* @psalm-return array{0:string}
*/
public function foo(): array {
return ["hello"];
}
}',
'7.3',
['InvalidReturnType'],
false,
true
],
];
}
}