2017-03-15 01:14:25 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Visitor;
|
2017-03-15 01:14:25 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
2017-03-15 01:14:25 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2017-03-15 01:14:25 +01:00
|
|
|
class AssignmentMapVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, array<string, bool>>
|
|
|
|
*/
|
|
|
|
protected $assignment_map = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $this_class_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|null $this_class_name
|
|
|
|
*/
|
|
|
|
public function __construct($this_class_name)
|
|
|
|
{
|
|
|
|
$this->this_class_name = $this_class_name;
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node $node
|
|
|
|
*
|
2018-01-28 23:07:09 +01:00
|
|
|
* @return null|int
|
2017-11-26 22:03:17 +01:00
|
|
|
*/
|
2017-03-15 01:14:25 +01:00
|
|
|
public function enterNode(PhpParser\Node $node)
|
|
|
|
{
|
|
|
|
if ($node instanceof PhpParser\Node\Expr\Assign) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$left_var_id = ExpressionAnalyzer::getRootVarId($node->var, $this->this_class_name);
|
|
|
|
$right_var_id = ExpressionAnalyzer::getRootVarId($node->expr, $this->this_class_name);
|
2017-03-15 01:14:25 +01:00
|
|
|
|
|
|
|
if ($left_var_id) {
|
2018-06-01 16:52:26 +02:00
|
|
|
$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;
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Expr\PostInc
|
|
|
|
|| $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
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_id = ExpressionAnalyzer::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;
|
2018-07-02 22:44:32 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Expr\FuncCall) {
|
|
|
|
foreach ($node->args as $arg) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$arg_var_id = ExpressionAnalyzer::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) {
|
|
|
|
$arg_var_id = ExpressionAnalyzer::getRootVarId($arg, $this->this_class_name);
|
|
|
|
|
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>>
|
|
|
|
*/
|
|
|
|
public function getAssignmentMap()
|
|
|
|
{
|
|
|
|
return $this->assignment_map;
|
|
|
|
}
|
|
|
|
}
|