1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00

Allow edge-case of by-reference assignment with unitiliazed property

Fixes #3003
This commit is contained in:
Matthew Brown 2020-03-21 19:23:24 -04:00
parent a7affbe716
commit c986cdf12e
3 changed files with 29 additions and 1 deletions

View File

@ -788,6 +788,9 @@ class CallAnalyzer
true 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, // if the variable is in scope, get or we're in a special array function,
// figure out its type before proceeding // figure out its type before proceeding
if (ExpressionAnalyzer::analyze( if (ExpressionAnalyzer::analyze(
@ -797,6 +800,8 @@ class CallAnalyzer
) === false) { ) === false) {
return false; return false;
} }
$context->inside_assignment = $was_inside_assignment;
} }
// special handling for array sort // special handling for array sort

View File

@ -148,7 +148,12 @@ class PropertyFetchAnalyzer
&& $source->getMethodName() === '__construct' && $source->getMethodName() === '__construct'
&& !$context->inside_unset && !$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; $stmt_type->initialized = true;
} else { } else {
if (IssueBuffer::accepts( if (IssueBuffer::accepts(

View File

@ -1910,6 +1910,24 @@ class PropertyTypeTest extends TestCase
} }
}' }'
], ],
'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 {};
}
}'
],
]; ];
} }