2017-03-15 01:14:25 +01:00
|
|
|
<?php
|
2020-03-15 04:54:42 +01:00
|
|
|
namespace Psalm\Internal\PhpVisitor;
|
2017-03-15 01:14:25 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
|
2017-03-15 01:14:25 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
2021-01-11 23:14:23 +01:00
|
|
|
*
|
|
|
|
* This produces a graph of probably assignments inside a loop
|
|
|
|
*
|
|
|
|
* With this map we can calculate how many times the loop analysis must
|
|
|
|
* be run before all variables have the correct types
|
2018-12-02 00:37:49 +01:00
|
|
|
*/
|
2020-10-15 19:23:35 +02:00
|
|
|
class AssignmentMapVisitor extends PhpParser\NodeVisitorAbstract
|
2017-03-15 01:14:25 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, array<string, bool>>
|
|
|
|
*/
|
|
|
|
protected $assignment_map = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $this_class_name;
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function __construct(?string $this_class_name)
|
2017-03-15 01:14:25 +01:00
|
|
|
{
|
|
|
|
$this->this_class_name = $this_class_name;
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function enterNode(PhpParser\Node $node): ?int
|
2017-03-15 01:14:25 +01:00
|
|
|
{
|
|
|
|
if ($node instanceof PhpParser\Node\Expr\Assign) {
|
2020-05-18 21:13:27 +02:00
|
|
|
$right_var_id = ExpressionIdentifier::getRootVarId($node->expr, $this->this_class_name);
|
2017-03-15 01:14:25 +01:00
|
|
|
|
2021-01-11 18:46:02 +01:00
|
|
|
if ($node->var instanceof PhpParser\Node\Expr\List_
|
|
|
|
|| $node->var instanceof PhpParser\Node\Expr\Array_
|
|
|
|
) {
|
|
|
|
foreach ($node->var->items as $assign_item) {
|
|
|
|
if ($assign_item) {
|
|
|
|
$left_var_id = ExpressionIdentifier::getRootVarId($assign_item->value, $this->this_class_name);
|
|
|
|
|
|
|
|
if ($left_var_id) {
|
|
|
|
$this->assignment_map[$left_var_id][$right_var_id ?: 'isset'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$left_var_id = ExpressionIdentifier::getRootVarId($node->var, $this->this_class_name);
|
|
|
|
|
|
|
|
if ($left_var_id) {
|
|
|
|
$this->assignment_map[$left_var_id][$right_var_id ?: 'isset'] = true;
|
|
|
|
}
|
2017-03-15 01:14:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 19:56:30 +02:00
|
|
|
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
2021-01-11 18:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof PhpParser\Node\Expr\PostInc
|
2018-05-03 19:56:30 +02:00
|
|
|
|| $node instanceof PhpParser\Node\Expr\PostDec
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\PreInc
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\PreDec
|
2018-05-05 22:17:54 +02:00
|
|
|
|| $node instanceof PhpParser\Node\Expr\AssignOp
|
2018-05-03 19:56:30 +02:00
|
|
|
) {
|
2020-05-18 21:13:27 +02:00
|
|
|
$var_id = ExpressionIdentifier::getRootVarId($node->var, $this->this_class_name);
|
2018-05-03 19:56:30 +02:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$this->assignment_map[$var_id][$var_id] = true;
|
|
|
|
}
|
|
|
|
|
2017-03-15 01:14:25 +01:00
|
|
|
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
2021-01-11 18:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof PhpParser\Node\Expr\FuncCall) {
|
2018-07-02 22:44:32 +02:00
|
|
|
foreach ($node->args as $arg) {
|
2020-05-18 21:13:27 +02:00
|
|
|
$arg_var_id = ExpressionIdentifier::getRootVarId($arg->value, $this->this_class_name);
|
2018-07-02 22:44:32 +02:00
|
|
|
|
2018-12-20 22:03:21 +01:00
|
|
|
if ($arg_var_id) {
|
|
|
|
$this->assignment_map[$arg_var_id][$arg_var_id] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Unset_) {
|
|
|
|
foreach ($node->vars as $arg) {
|
2020-05-18 21:13:27 +02:00
|
|
|
$arg_var_id = ExpressionIdentifier::getRootVarId($arg, $this->this_class_name);
|
2018-12-20 22:03:21 +01:00
|
|
|
|
2018-07-02 22:44:32 +02:00
|
|
|
if ($arg_var_id) {
|
|
|
|
$this->assignment_map[$arg_var_id][$arg_var_id] = true;
|
|
|
|
}
|
|
|
|
}
|
2017-03-15 01:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, array<string, bool>>
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getAssignmentMap(): array
|
2017-03-15 01:14:25 +01:00
|
|
|
{
|
|
|
|
return $this->assignment_map;
|
|
|
|
}
|
|
|
|
}
|