mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix loop analysis for byref vars, likely performance hit
This commit is contained in:
parent
beeab32f60
commit
6287f52dd5
@ -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()) {
|
||||
|
@ -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;
|
||||
|
@ -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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user