1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Don’t update return types to null

This commit is contained in:
Matt Brown 2018-01-22 18:00:19 -05:00
parent e58e824af3
commit e249c27308
3 changed files with 40 additions and 3 deletions

View File

@ -1258,7 +1258,7 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
if ($project_checker->alter_code if ($project_checker->alter_code
&& isset($project_checker->getIssuesToFix()['MissingClosureReturnType']) && isset($project_checker->getIssuesToFix()['MissingClosureReturnType'])
) { ) {
if ($inferred_return_type->isMixed()) { if ($inferred_return_type->isMixed() || $inferred_return_type->isNull()) {
return null; return null;
} }
@ -1288,7 +1288,7 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
if ($project_checker->alter_code if ($project_checker->alter_code
&& isset($project_checker->getIssuesToFix()['MissingReturnType']) && isset($project_checker->getIssuesToFix()['MissingReturnType'])
) { ) {
if ($inferred_return_type->isMixed()) { if ($inferred_return_type->isMixed() || $inferred_return_type->isNull()) {
return null; return null;
} }
@ -1383,6 +1383,7 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
) { ) {
if ($project_checker->alter_code if ($project_checker->alter_code
&& isset($project_checker->getIssuesToFix()['InvalidNullableReturnType']) && isset($project_checker->getIssuesToFix()['InvalidNullableReturnType'])
&& !$inferred_return_type->isNull()
) { ) {
$this->addOrUpdateReturnType( $this->addOrUpdateReturnType(
$project_checker, $project_checker,

View File

@ -60,7 +60,7 @@ class TNamedObject extends Atomic
$class_name = array_pop($class_parts); $class_name = array_pop($class_parts);
if ($this->value === $this_class) { if ($this->value === $this_class) {
return $class_name; return 'self';
} }
if ($namespace && preg_match('/^' . preg_quote($namespace) . '\\\\' . $class_name . '$/i', $this->value)) { if ($namespace && preg_match('/^' . preg_quote($namespace) . '\\\\' . $class_name . '$/i', $this->value)) {

View File

@ -415,6 +415,42 @@ class FileManipulationTest extends TestCase
['MissingReturnType'], ['MissingReturnType'],
false, false,
], ],
'addSelfReturnType' => [
'<?php
class A {
public function foo() {
return $this;
}
}',
'<?php
class A {
public function foo(): self {
return $this;
}
}',
'7.1',
['MissingReturnType'],
false,
],
'dontAddMissingVoidReturnType56' => [
'<?php
/** @return void */
function foo() { }
function bar() {
return foo();
}',
'<?php
/** @return void */
function foo() { }
function bar() {
return foo();
}',
'5.6',
['MissingReturnType'],
true,
],
'fixInvalidIntReturnType56' => [ 'fixInvalidIntReturnType56' => [
'<?php '<?php
/** /**