2020-10-16 22:16:36 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-10-16 22:16:36 +02:00
|
|
|
namespace Rector\ReadWrite\ReadNodeAnalyzer;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
2021-03-01 01:56:08 +01:00
|
|
|
use Rector\NodeNestingScope\ParentScopeFinder;
|
2020-10-16 22:16:36 +02:00
|
|
|
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;
|
2021-03-01 01:56:08 +01:00
|
|
|
use Rector\ReadWrite\NodeFinder\NodeUsageFinder;
|
2021-05-10 22:23:08 +00:00
|
|
|
final class VariableReadNodeAnalyzer implements \Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface
|
2020-10-16 22:16:36 +02:00
|
|
|
{
|
2021-03-01 01:56:08 +01:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNestingScope\ParentScopeFinder
|
2021-03-01 01:56:08 +01:00
|
|
|
*/
|
|
|
|
private $parentScopeFinder;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\ReadWrite\NodeFinder\NodeUsageFinder
|
2021-03-01 01:56:08 +01:00
|
|
|
*/
|
|
|
|
private $nodeUsageFinder;
|
|
|
|
/**
|
2021-06-10 10:46:24 +00:00
|
|
|
* @var \Rector\ReadWrite\ReadNodeAnalyzer\JustReadExprAnalyzer
|
2021-03-01 01:56:08 +01:00
|
|
|
*/
|
2021-06-10 10:46:24 +00:00
|
|
|
private $justReadExprAnalyzer;
|
|
|
|
public function __construct(\Rector\NodeNestingScope\ParentScopeFinder $parentScopeFinder, \Rector\ReadWrite\NodeFinder\NodeUsageFinder $nodeUsageFinder, \Rector\ReadWrite\ReadNodeAnalyzer\JustReadExprAnalyzer $justReadExprAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-03-01 01:56:08 +01:00
|
|
|
$this->parentScopeFinder = $parentScopeFinder;
|
|
|
|
$this->nodeUsageFinder = $nodeUsageFinder;
|
2021-06-10 10:46:24 +00:00
|
|
|
$this->justReadExprAnalyzer = $justReadExprAnalyzer;
|
2021-03-01 01:56:08 +01:00
|
|
|
}
|
2021-07-05 22:50:18 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node $node
|
|
|
|
*/
|
|
|
|
public function supports($node) : bool
|
2020-10-16 22:16:36 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return $node instanceof \PhpParser\Node\Expr\Variable;
|
2020-10-16 22:16:36 +02:00
|
|
|
}
|
|
|
|
/**
|
2021-07-05 22:50:18 +00:00
|
|
|
* @param \PhpParser\Node $node
|
2020-10-16 22:16:36 +02:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function isRead($node) : bool
|
2020-10-16 22:16:36 +02:00
|
|
|
{
|
|
|
|
$parentScope = $this->parentScopeFinder->find($node);
|
|
|
|
if ($parentScope === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-10-16 22:16:36 +02:00
|
|
|
}
|
|
|
|
$variableUsages = $this->nodeUsageFinder->findVariableUsages((array) $parentScope->stmts, $node);
|
|
|
|
foreach ($variableUsages as $variableUsage) {
|
2021-06-10 10:46:24 +00:00
|
|
|
if ($this->justReadExprAnalyzer->isReadContext($variableUsage)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-16 22:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-10-16 22:16:36 +02:00
|
|
|
}
|
|
|
|
}
|