mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-22 10:55:10 +00:00
[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:
parent
23046668ed
commit
c31cabf0b3
|
@ -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);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->exprUsedInNextNodeAnalyzer->isUsed($node->var)) {
|
||||
if ($this->exprUsedInNextNodeAnalyzer->isUsed($node->var, true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
?>
|
||||
|
|
13
tests/Issues/Issue6675/Fixture/no_namespace.php.inc
Normal file
13
tests/Issues/Issue6675/Fixture/no_namespace.php.inc
Normal 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);
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user