2020-02-23 21:49:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\MysqlToMysqli\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2020-10-12 19:06:24 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2020-02-23 21:49:30 +00:00
|
|
|
use PhpParser\Node\Name;
|
2020-09-03 18:49:31 +00:00
|
|
|
use PHPStan\Type\ResourceType;
|
2020-10-12 19:06:24 +00:00
|
|
|
use PHPStan\Type\Type;
|
2020-02-23 21:49:30 +00:00
|
|
|
use PHPStan\Type\UnionType;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-02-23 21:49:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://www.php.net/manual/en/mysqli.error.php
|
|
|
|
* @see https://www.php.net/manual/en/mysqli.query.php
|
|
|
|
*
|
|
|
|
* @see \Rector\MysqlToMysqli\Tests\Rector\FuncCall\MysqlQueryMysqlErrorWithLinkRector\MysqlQueryMysqlErrorWithLinkRectorTest
|
|
|
|
*/
|
|
|
|
final class MysqlQueryMysqlErrorWithLinkRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
2020-09-01 17:56:30 +00:00
|
|
|
* @var array<string, string>
|
2020-02-23 21:49:30 +00:00
|
|
|
*/
|
|
|
|
private const FUNCTION_RENAME_MAP = [
|
2020-09-03 18:49:31 +00:00
|
|
|
'mysql_affected_rows' => 'mysqli_affected_rows',
|
|
|
|
'mysql_client_encoding' => 'mysqli_character_set_name',
|
|
|
|
'mysql_close' => 'mysqli_close',
|
|
|
|
'mysql_errno' => 'mysqli_errno',
|
2020-02-23 21:49:30 +00:00
|
|
|
'mysql_error' => 'mysqli_error',
|
2020-09-03 18:49:31 +00:00
|
|
|
'mysql_escape_string' => 'mysqli_real_escape_string',
|
|
|
|
'mysql_get_host_info' => 'mysqli_get_host_info',
|
|
|
|
'mysql_get_proto_info' => 'mysqli_get_proto_info',
|
|
|
|
'mysql_get_server_info' => 'mysqli_get_server_info',
|
|
|
|
'mysql_info' => 'mysqli_info',
|
|
|
|
'mysql_insert_id' => 'mysqli_insert_id',
|
|
|
|
'mysql_ping' => 'mysqli_ping',
|
2020-02-23 21:49:30 +00:00
|
|
|
'mysql_query' => 'mysqli_query',
|
2020-09-03 18:49:31 +00:00
|
|
|
'mysql_real_escape_string' => 'mysqli_real_escape_string',
|
|
|
|
'mysql_select_db' => 'mysqli_select_db',
|
|
|
|
'mysql_set_charset' => 'mysqli_set_charset',
|
|
|
|
'mysql_stat' => 'mysqli_stat',
|
|
|
|
'mysql_thread_id' => 'mysqli_thread_id',
|
2020-02-23 21:49:30 +00:00
|
|
|
];
|
|
|
|
|
2020-12-05 20:26:46 +00:00
|
|
|
/**
|
|
|
|
* @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,
|
|
|
|
];
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-02-23 21:49:30 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Add mysql_query and mysql_error with connection',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2020-02-23 21:49:30 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$conn = mysqli_connect('host', 'user', 'pass');
|
|
|
|
|
|
|
|
mysql_error();
|
|
|
|
$sql = 'SELECT';
|
|
|
|
|
|
|
|
return mysql_query($sql);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-02-23 21:49:30 +00:00
|
|
|
,
|
2020-11-16 17:50:38 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-02-23 21:49:30 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$conn = mysqli_connect('host', 'user', 'pass');
|
|
|
|
|
|
|
|
mysqli_error($conn);
|
|
|
|
$sql = 'SELECT';
|
|
|
|
|
|
|
|
return mysqli_query($conn, $sql);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2020-02-23 21:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
foreach (self::FUNCTION_RENAME_MAP as $oldFunction => $newFunction) {
|
|
|
|
if (! $this->isName($node, $oldFunction)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-03 18:49:31 +00:00
|
|
|
if (
|
2020-10-29 08:59:39 +00:00
|
|
|
$node->args === []
|
2020-09-03 18:49:31 +00:00
|
|
|
|| ! $this->isProbablyMysql($node->args[0]->value)
|
|
|
|
) {
|
|
|
|
$connectionVariable = $this->findConnectionVariable($node);
|
|
|
|
|
2020-12-05 20:26:46 +00:00
|
|
|
$this->removeExistingConnectionParameter($node);
|
|
|
|
|
2020-09-03 18:49:31 +00:00
|
|
|
if ($connectionVariable === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->args = array_merge([new Arg($connectionVariable)], $node->args);
|
|
|
|
}
|
|
|
|
|
2020-02-23 21:49:30 +00:00
|
|
|
$node->name = new Name($newFunction);
|
2020-09-03 18:49:31 +00:00
|
|
|
|
|
|
|
return $node;
|
2020-02-23 21:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 18:49:31 +00:00
|
|
|
return null;
|
2020-02-23 21:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 18:49:31 +00:00
|
|
|
private function isProbablyMysql(Node $node): bool
|
2020-02-23 21:49:30 +00:00
|
|
|
{
|
2020-09-03 18:49:31 +00:00
|
|
|
if ($this->isObjectType($node, 'mysqli')) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-23 21:49:30 +00:00
|
|
|
|
2020-10-12 19:06:24 +00:00
|
|
|
$staticType = $this->getStaticType($node);
|
2020-09-03 18:49:31 +00:00
|
|
|
$resourceType = new ResourceType();
|
|
|
|
|
2020-10-12 19:06:24 +00:00
|
|
|
if ($staticType->equals($resourceType)) {
|
2020-09-03 18:49:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-02-23 21:49:30 +00:00
|
|
|
|
2020-10-12 19:06:24 +00:00
|
|
|
if ($this->isUnionTypeWithResourceSubType($staticType, $resourceType)) {
|
|
|
|
return true;
|
2020-09-03 18:49:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 19:06:24 +00:00
|
|
|
return $node instanceof Variable && $this->isName($node, 'connection');
|
2020-09-03 18:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function findConnectionVariable(FuncCall $funcCall): ?Expr
|
|
|
|
{
|
|
|
|
$connectionAssign = $this->betterNodeFinder->findFirstPrevious($funcCall, function (Node $node): ?bool {
|
|
|
|
if (! $node instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-23 21:49:30 +00:00
|
|
|
|
2020-09-03 18:49:31 +00:00
|
|
|
return $this->isObjectType($node->expr, 'mysqli');
|
2020-02-23 21:49:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return $connectionAssign !== null ? $connectionAssign->var : null;
|
|
|
|
}
|
2020-10-12 19:06:24 +00:00
|
|
|
|
2020-12-05 21:40:10 +00:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2020-10-12 19:06:24 +00:00
|
|
|
private function isUnionTypeWithResourceSubType(Type $staticType, ResourceType $resourceType): bool
|
|
|
|
{
|
|
|
|
if ($staticType instanceof UnionType) {
|
|
|
|
foreach ($staticType->getTypes() as $type) {
|
|
|
|
if ($type->equals($resourceType)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-23 21:49:30 +00:00
|
|
|
}
|