Skip to content
Permalink
Browse files

Don’t emit MissingClosureReturnType when inside call and type can be …

…inferred
  • Loading branch information...
muglug committed May 7, 2019
1 parent fec1a60 commit 4f8d6a50d8aa21f1c68e62f47443ab9c82ed2ff3
@@ -62,6 +62,13 @@ class Context
*/
public $inside_class_exists = false;
/**
* Whether or not we're inside a function/method call
*
* @var bool
*/
public $inside_call = false;
/**
* @var null|CodeLocation
*/
@@ -56,7 +56,8 @@ public static function verifyReturnType(
Type\Union $return_type = null,
$fq_class_name = null,
CodeLocation $return_type_location = null,
array $compatible_method_ids = []
array $compatible_method_ids = [],
bool $closure_inside_call = false
) {
$suppressed_issues = $function_like_analyzer->getSuppressedIssues();
$codebase = $source->getCodebase();
@@ -246,36 +247,38 @@ public static function verifyReturnType(
if (!$return_type) {
if ($function instanceof Closure) {
if ($codebase->alter_code
&& isset($project_analyzer->getIssuesToFix()['MissingClosureReturnType'])
) {
if ($inferred_return_type->hasMixed() || $inferred_return_type->isNull()) {
return null;
}
if (!$closure_inside_call || $inferred_return_type->isMixed()) {
if ($codebase->alter_code
&& isset($project_analyzer->getIssuesToFix()['MissingClosureReturnType'])
) {
if ($inferred_return_type->hasMixed() || $inferred_return_type->isNull()) {
return null;
}
self::addOrUpdateReturnType(
$function,
$project_analyzer,
$inferred_return_type,
$source,
$function_like_analyzer,
($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock,
$function_like_storage
);
self::addOrUpdateReturnType(
$function,
$project_analyzer,
$inferred_return_type,
$source,
$function_like_analyzer,
($project_analyzer->only_replace_php_types_with_non_docblock_types
|| $unsafe_return_type)
&& $inferred_return_type->from_docblock,
$function_like_storage
);
return null;
}
return null;
}
if (IssueBuffer::accepts(
new MissingClosureReturnType(
'Closure does not have a return type, expecting ' . $inferred_return_type,
new CodeLocation($function_like_analyzer, $function, null, true)
),
$suppressed_issues
)) {
// fall through
if (IssueBuffer::accepts(
new MissingClosureReturnType(
'Closure does not have a return type, expecting ' . $inferred_return_type,
new CodeLocation($function_like_analyzer, $function, null, true)
),
$suppressed_issues
)) {
// fall through
}
}
return null;
@@ -708,7 +708,8 @@ function (FunctionLikeParameter $p) {
$statements_analyzer,
$storage->return_type,
$this->source->getFQCLN(),
$storage->return_type_location
$storage->return_type_location,
$global_context && $global_context->inside_call
);
$closure_yield_types = [];
@@ -931,15 +932,18 @@ public function verifyReturnType(
StatementsAnalyzer $statements_analyzer,
Type\Union $return_type = null,
$fq_class_name = null,
CodeLocation $return_type_location = null
CodeLocation $return_type_location = null,
bool $closure_inside_call = false
) {
ReturnTypeAnalyzer::verifyReturnType(
$this->function,
$statements_analyzer,
$this,
$return_type,
$fq_class_name,
$return_type_location
$return_type_location,
[],
$closure_inside_call
);
}
@@ -448,10 +448,18 @@ function (PhpParser\Node\Param $closure_param) : bool {
}
}
$was_inside_call = $context->inside_call;
$context->inside_call = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
return false;
}
if (!$was_inside_call) {
$context->inside_call = false;
}
if ($context->collect_references
&& ($arg->value instanceof PhpParser\Node\Expr\AssignOp
|| $arg->value instanceof PhpParser\Node\Expr\PreInc
@@ -2178,7 +2178,7 @@ function get();
*/
function expandOptions(Collection $collection) : Collection {
return $collection->map(
function ($optional) : string {
function ($optional) {
return $optional->get();
}
);

0 comments on commit 4f8d6a5

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