Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/sets/composer-based.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\Doctrine\Dbal211\Rector\MethodCall\ExtractArrayArgOnQueryBuilderSelectRector;
use Rector\Doctrine\Dbal211\Rector\MethodCall\ReplaceFetchAllMethodCallRector;
use Rector\Doctrine\Dbal31\Rector\MethodCall\QueryBuilderExecuteToExecuteQueryOrExecuteStatementRector;
use Rector\Doctrine\Dbal32\Rector\Identical\PlatformGetNameToInstanceofRector;
use Rector\Doctrine\Dbal36\Rector\MethodCall\MigrateQueryBuilderResetQueryPartRector;
use Rector\Doctrine\Dbal40\Rector\MethodCall\ChangeCompositeExpressionAddMultipleWithWithRector;
use Rector\Doctrine\Dbal40\Rector\StmtsAwareInterface\ExecuteQueryParamsToBindValueRector;
Expand Down Expand Up @@ -71,6 +72,9 @@
// doctrine/dbal 3.1
QueryBuilderExecuteToExecuteQueryOrExecuteStatementRector::class,

// doctrine/dbal 3.2
PlatformGetNameToInstanceofRector::class,

// doctrine/dbal 4.0 and 4.2
ChangeCompositeExpressionAddMultipleWithWithRector::class,
ExecuteQueryParamsToBindValueRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Rector\Doctrine\Tests\Dbal32\Rector\Identical\PlatformGetNameToInstanceofRector;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class TestMigration
{
/** @var AbstractPlatform */
public $platform;

public function run(): void
{
if ('postgresql' === $this->platform->getName()) {
echo "Is Postgres";
}

if ($this->platform->getName() === 'postgresql') {
echo "Is Postgres";
}

if ($this->platform->getName() === 'mysql') {
echo "Is MySQL";
}

if ($this->platform->getName() === 'sqlite') {
echo "Is SQLite";
}

if ($this->platform->getName() === 'oracle') {
echo "Is Oracle";
}

if ($this->platform->getName() === 'mssql') {
echo "Is SQL Server";
}

if ($this->platform->getName() === 'db2') {
echo "Is DB2";
}

if ($this->platform->getName() === 'unknown_db') {
echo "Is Unknown";
}
}
}

-----
<?php

namespace Rector\Doctrine\Tests\Dbal32\Rector\Identical\PlatformGetNameToInstanceofRector;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class TestMigration
{
/** @var AbstractPlatform */
public $platform;

public function run(): void
{
if ($this->platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
echo "Is Postgres";
}

if ($this->platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
echo "Is Postgres";
}

if ($this->platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform) {
echo "Is MySQL";
}

if ($this->platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
echo "Is SQLite";
}

if ($this->platform instanceof \Doctrine\DBAL\Platforms\OraclePlatform) {
echo "Is Oracle";
}

if ($this->platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform) {
echo "Is SQL Server";
}

if ($this->platform instanceof \Doctrine\DBAL\Platforms\DB2Platform) {
echo "Is DB2";
}

if ($this->platform->getName() === 'unknown_db') {
echo "Is Unknown";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Doctrine\Tests\Dbal32\Rector\Identical\PlatformGetNameToInstanceofRector;

class SkipTest
{
public function run($someObject): void
{
// Skip: Not a method call (comparing string to standard variable)
if ('postgresql' === $someObject) {
return;
}

// Skip: Method name is not getName()
if ('postgresql' === $someObject->getDatabaseType()) {
return;
}

// Skip: Unknown/custom platform string not in PLATFORM_MAP
if ($someObject->getName() === 'postgresql') {
return;
}
if ('postgresql' === $someObject->getName()) {
return;
}

// Skip: Comparing getName() result to an integer or non-string
if (123 === $someObject->getName()) {
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\Dbal32\Rector\Identical\PlatformGetNameToInstanceofRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class PlatformGetNameToInstanceofRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Dbal32\Rector\Identical\PlatformGetNameToInstanceofRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(PlatformGetNameToInstanceofRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Dbal32\Rector\Identical;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/doctrine/dbal/pull/4755
* @see Rector\Doctrine\Tests\Dbal32\Rector\Identical\PlatformGetNameToInstanceofRector\PlatformGetNameToInstanceofRectorTest
*/
final class PlatformGetNameToInstanceofRector extends AbstractRector implements ComposerPackageConstraintInterface
{
/**
* Maps the string returned by AbstractPlatform::getName() to its platform class.
*
* @var array<string, string>
*/
private const array PLATFORM_MAP = [
'postgresql' => 'Doctrine\DBAL\Platforms\PostgreSQLPlatform',
'mysql' => 'Doctrine\DBAL\Platforms\MySQLPlatform',
'sqlite' => 'Doctrine\DBAL\Platforms\SqlitePlatform',
'oracle' => 'Doctrine\DBAL\Platforms\OraclePlatform',
'mssql' => 'Doctrine\DBAL\Platforms\SQLServerPlatform',
'db2' => 'Doctrine\DBAL\Platforms\DB2Platform',
];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change $platform->getName() === "postgresql" to $platform instanceof PostgreSQLPlatform, following the DBAL 3.2 deprecation of AbstractPlatform::getName(), see https://github.com/doctrine/dbal/pull/4755',
[
new CodeSample(
"if ('postgresql' === \$this->platform->getName()) {}",
"if (\$this->platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {}"
),
]
);
}

public function provideComposerPackageConstraint(): ComposerPackageConstraint
{
return new ComposerPackageConstraint('doctrine/dbal', '>= 3.2');
}

public function getNodeTypes(): array
{
return [Identical::class, Equal::class];
}

/**
* @param Identical|Equal $node
*/
public function refactor(Node $node): ?Node
{
// Handle both: 'postgresql' === $platform->getName() AND $platform->getName() === 'postgresql'
if ($node->left instanceof String_ && $node->right instanceof MethodCall) {
$stringNode = $node->left;
$methodCallNode = $node->right;
} elseif ($node->right instanceof String_ && $node->left instanceof MethodCall) {
$stringNode = $node->right;
$methodCallNode = $node->left;
} else {
return null;
}

if (! $this->isName($methodCallNode->name, 'getName')) {
return null;
}

// ONLY apply this if the variable calling getName() is a Doctrine DBAL Platform
if (! $this->isObjectType($methodCallNode->var, new ObjectType('Doctrine\DBAL\Platforms\AbstractPlatform'))) {
return null;
}

$platformName = $stringNode->value;
if (! isset(self::PLATFORM_MAP[$platformName])) {
return null;
}

return new Instanceof_(
$methodCallNode->var,
new FullyQualified(self::PLATFORM_MAP[$platformName])
);
}
}
Loading