1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix loop analysis for byref vars, likely performance hit

This commit is contained in:
Matt Brown 2018-07-02 16:44:32 -04:00
parent beeab32f60
commit 6287f52dd5
3 changed files with 60 additions and 0 deletions

View File

@ -39,6 +39,9 @@ class DoChecker
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_checker->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_checker->addSuppressedIssues(['TypeDoesNotContainType']);
}
$statements_checker->analyze($stmt->stmts, $do_context);
@ -48,6 +51,9 @@ class DoChecker
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_checker->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_checker->removeSuppressedIssues(['TypeDoesNotContainType']);
}
foreach ($context->vars_in_scope as $var => $type) {
if ($type->isMixed()) {

View File

@ -53,6 +53,14 @@ class AssignmentMapVisitor extends PhpParser\NodeVisitorAbstract implements PhpP
}
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
} elseif ($node instanceof PhpParser\Node\Expr\FuncCall) {
foreach ($node->args as $arg) {
$arg_var_id = ExpressionChecker::getRootVarId($arg->value, $this->this_class_name);
if ($arg_var_id) {
$this->assignment_map[$arg_var_id][$arg_var_id] = true;
}
}
}
return null;

View File

@ -1019,6 +1019,36 @@ class LoopScopeTest extends TestCase
}
}'
],
'invalidateBothByRefAssignments' => [
'<?php
function foo(?string &$i) : void {}
function bar(?string &$i) : void {}
$c = null;
while (rand(0, 1)) {
if (!$c) {
foo($c);
} else {
bar($c);
}
}',
],
'invalidateBothByRefAssignmentsInDo' => [
'<?php
function foo(?string &$i) : void {}
function bar(?string &$i) : void {}
$c = null;
do {
if (!$c) {
foo($c);
} else {
bar($c);
}
} while (rand(0, 1));',
],
];
}
@ -1204,6 +1234,22 @@ class LoopScopeTest extends TestCase
}',
'error_message' => 'LoopInvalidation',
],
'invalidateByRefAssignmentWithRedundantCondition' => [
'<?php
function foo(?string $i) : void {}
function bar(?string $i) : void {}
$c = null;
while (rand(0, 1)) {
if (!$c) {
foo($c);
} else {
bar($c);
}
}',
'error_message' => 'RedundantCondition',
],
];
}
}