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

Add by-reference clause removal

This commit is contained in:
Matthew Brown 2017-04-02 17:37:56 -04:00
parent a09e668dbc
commit 075cc5f50d
2 changed files with 34 additions and 0 deletions

View File

@ -608,6 +608,15 @@ class ExpressionChecker
} }
} else { } else {
$existing_type = $context->vars_in_scope[$var_id]; $existing_type = $context->vars_in_scope[$var_id];
// removes dependennt vars from $context
$context->removeDescendents(
$var_id,
$existing_type,
$by_ref_type,
$statements_checker->getFileChecker()
);
if ((string)$existing_type !== 'array<empty, empty>') { if ((string)$existing_type !== 'array<empty, empty>') {
$context->vars_in_scope[$var_id] = $by_ref_type; $context->vars_in_scope[$var_id] = $by_ref_type;
$stmt->inferredType = $context->vars_in_scope[$var_id]; $stmt->inferredType = $context->vars_in_scope[$var_id];

View File

@ -607,4 +607,29 @@ class TypeAlgebraTest extends PHPUnit_Framework_TestCase
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts); $file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods(); $file_checker->visitAndAnalyzeMethods();
} }
/**
* @return void
*/
public function testByRefAssignment()
{
$stmts = self::$parser->parse('<?php
function foo() : void {
$matches = rand(0, 1) ? ["hello"] : null;
if (!$matches) {
return;
}
preg_match("/hello/", "hello dolly", $matches);
if ($matches) {
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
} }