mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-26 12:52:36 +00:00
[MysqlQueryMysqlErrorWithLinkRector] Remove already existing connection parameter from function call (#4793)
* Remove already existing connection parameter in function call * CS Fixer
This commit is contained in:
parent
47a9a52ffd
commit
69b63de4d3
|
@ -50,6 +50,29 @@ final class MysqlQueryMysqlErrorWithLinkRector extends AbstractRector
|
|||
'mysql_thread_id' => 'mysqli_thread_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<string, int>
|
||||
*/
|
||||
private const FUNCTION_CONNECTION_PARAMETER_POSITION_MAP = [
|
||||
'mysql_affected_rows' => 0,
|
||||
'mysql_client_encoding' => 0,
|
||||
'mysql_close' => 0,
|
||||
'mysql_errno' => 0,
|
||||
'mysql_error' => 0,
|
||||
'mysql_get_host_info' => 0,
|
||||
'mysql_get_proto_info' => 0,
|
||||
'mysql_get_server_info' => 0,
|
||||
'mysql_info' => 0,
|
||||
'mysql_insert_id' => 0,
|
||||
'mysql_ping' => 0,
|
||||
'mysql_query' => 1,
|
||||
'mysql_real_escape_string' => 1,
|
||||
'mysql_select_db' => 1,
|
||||
'mysql_set_charset' => 1,
|
||||
'mysql_stat' => 0,
|
||||
'mysql_thread_id' => 0,
|
||||
];
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
|
@ -114,6 +137,8 @@ CODE_SAMPLE
|
|||
) {
|
||||
$connectionVariable = $this->findConnectionVariable($node);
|
||||
|
||||
$this->removeExistingConnectionParameter($node);
|
||||
|
||||
if ($connectionVariable === null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -129,6 +154,18 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
private function removeExistingConnectionParameter(FuncCall $funcCall): void
|
||||
{
|
||||
/** @var string $functionName */
|
||||
$functionName = $this->getName($funcCall);
|
||||
if (! isset(self::FUNCTION_CONNECTION_PARAMETER_POSITION_MAP[$functionName])){
|
||||
return;
|
||||
}
|
||||
|
||||
$connectionPosition = self::FUNCTION_CONNECTION_PARAMETER_POSITION_MAP[$functionName];
|
||||
unset($funcCall->args[$connectionPosition]);
|
||||
}
|
||||
|
||||
private function isProbablyMysql(Node $node): bool
|
||||
{
|
||||
if ($this->isObjectType($node, 'mysqli')) {
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\MysqlToMysqli\Tests\Rector\FuncCall\MysqlQueryMysqlErrorWithLinkRector\Fixture;
|
||||
|
||||
class ConnectionProvided
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$link = mysqli_connect('host', 'user', 'pass');
|
||||
$res = mysql_query("INSERT INTO ", $link) or die (mysql_error());
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MysqlToMysqli\Tests\Rector\FuncCall\MysqlQueryMysqlErrorWithLinkRector\Fixture;
|
||||
|
||||
class ConnectionProvided
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$link = mysqli_connect('host', 'user', 'pass');
|
||||
$res = mysqli_query($link, "INSERT INTO ") or die (mysqli_error($link));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user