[MYSQL_TO_MYSQLI] Restore Mysql to Mysqli Behaviour when used with Dead Code SetList (#1161)

* [MYSQL_TO_MYSQLI] Restore Mysql to Mysqli Behaviour when used with Dead Code SetList

* check with namespaced name

* check Arg

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-11-06 07:36:41 +07:00 committed by GitHub
parent 23046668ed
commit c31cabf0b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 4 deletions

View File

@ -5,8 +5,12 @@ declare(strict_types=1);
namespace Rector\DeadCode\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class ExprUsedInNextNodeAnalyzer
{
@ -16,11 +20,27 @@ final class ExprUsedInNextNodeAnalyzer
) {
}
public function isUsed(Expr $expr): bool
/**
* $isCheckNameScope parameter is used to whether to check scope of Name that may be renamed
* @see https://github.com/rectorphp/rector/issues/6675
*/
public function isUsed(Expr $expr, bool $isCheckNameScope = false): bool
{
return (bool) $this->betterNodeFinder->findFirstNext(
$expr,
fn (Node $node): bool => $this->exprUsedInNodeAnalyzer->isUsed($node, $expr)
function (Node $node) use ($expr, $isCheckNameScope): bool {
if ($isCheckNameScope && $node instanceof Name) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
$resolvedName = $node->getAttribute(AttributeKey::RESOLVED_NAME);
$next = $node->getAttribute(AttributeKey::NEXT_NODE);
if (! $scope instanceof Scope && ! $resolvedName instanceof Name && $next instanceof Arg) {
return true;
}
}
return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);
}
);
}
}

View File

@ -85,7 +85,7 @@ CODE_SAMPLE
return null;
}
if ($this->exprUsedInNextNodeAnalyzer->isUsed($node->var)) {
if ($this->exprUsedInNextNodeAnalyzer->isUsed($node->var, true)) {
return null;
}

View File

@ -12,6 +12,6 @@ echo mysql_error();
namespace Rector\Core\Tests\Issues\Issue6675\Fixture;
$db = mysqli_connect("server","user","password");
echo mysql_error();
echo mysqli_error($db);
?>

View File

@ -0,0 +1,13 @@
<?php
$db = mysql_connect("server","user","password");
echo mysql_error();
?>
-----
<?php
$db = mysqli_connect("server","user","password");
echo mysqli_error($db);
?>