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;
|
|
|
|
|
2020-10-19 01:18:04 +02:00
|
|
|
use PhpParser\Node\Arg;
|
2020-10-16 22:16:36 +02:00
|
|
|
use PhpParser\Node\Expr;
|
2020-10-19 01:46:38 +02:00
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
2021-02-07 11:24:53 +01:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2020-10-16 22:16:36 +02:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-06-10 10:46:24 +00:00
|
|
|
final class JustReadExprAnalyzer
|
2020-10-16 22:16:36 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isReadContext(\PhpParser\Node\Expr $expr) : bool
|
2020-10-16 22:16:36 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$parent = $expr->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
|
|
|
if ($parent instanceof \PhpParser\Node\Stmt\Return_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-16 22:16:36 +02:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($parent instanceof \PhpParser\Node\Arg) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-19 01:18:04 +02:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($parent instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
$parentParent = $parent->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
|
|
|
if (!$parentParent instanceof \PhpParser\Node\Expr\Assign) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-19 01:46:38 +02:00
|
|
|
}
|
|
|
|
return $parentParent->var !== $parent;
|
|
|
|
}
|
2021-02-11 16:01:41 +01:00
|
|
|
// assume it's used by default
|
2021-05-10 22:23:08 +00:00
|
|
|
return !$parent instanceof \PhpParser\Node\Stmt\Expression;
|
2020-10-16 22:16:36 +02:00
|
|
|
}
|
|
|
|
}
|