Skip to content

Commit

Permalink
Allow edge-case of by-reference assignment with unitiliazed property
Browse files Browse the repository at this point in the history
Fixes #3003
  • Loading branch information
muglug committed Mar 21, 2020
1 parent a7affbe commit c986cdf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ private static function handleByRefFunctionArg(
true
))
) {
$was_inside_assignment = $context->inside_assignment;
$context->inside_assignment = true;

// if the variable is in scope, get or we're in a special array function,
// figure out its type before proceeding
if (ExpressionAnalyzer::analyze(
Expand All @@ -797,6 +800,8 @@ private static function handleByRefFunctionArg(
) === false) {
return false;
}

$context->inside_assignment = $was_inside_assignment;
}

// special handling for array sort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public static function analyzeInstance(
&& $source->getMethodName() === '__construct'
&& !$context->inside_unset
) {
if ($context->inside_isset) {
if ($context->inside_isset
|| ($context->inside_assignment
&& isset($context->vars_in_scope[$var_id])
&& $context->vars_in_scope[$var_id]->isNullable()
)
) {
$stmt_type->initialized = true;
} else {
if (IssueBuffer::accepts(
Expand Down
18 changes: 18 additions & 0 deletions tests/PropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,24 @@ public function __construct() {
}
}'
],
'allowByReferenceAssignmentToUninitializedNullableProperty' => [
'<?php
class C {
private ?\Closure $onCancel;
public function __construct() {
$this->foo($this->onCancel);
}
/**
* @param mixed $onCancel
* @param-out \Closure $onCancel
*/
public function foo(&$onCancel) : void {
$onCancel = function (): void {};
}
}'
],
];
}

Expand Down

0 comments on commit c986cdf

Please sign in to comment.