MigrationClassRenameRector::getNodeTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace Sserbin\DoctrineMigrations2Upgrade;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Name;
7
use Rector\Rector\AbstractRector;
8
use Rector\RectorDefinition\CodeSample;
9
use Rector\RectorDefinition\RectorDefinition;
10
11
/**
12
 * @see https://github.com/doctrine/migrations/pull/636
13
 */
14
class MigrationClassRenameRector extends AbstractRector
15
{
16
    private const CLASS_RENAMES = [
17
        'Doctrine\DBAL\Migrations\AbortMigrationException' => 'Doctrine\Migrations\Exception\AbortMigration',
18
        'Doctrine\DBAL\Migrations\IrreversibleMigrationException' =>
19
            'Doctrine\Migrations\Exception\IrreversibleMigration',
20
        'Doctrine\DBAL\Migrations\SkipMigrationException' => 'Doctrine\Migrations\Exception\SkipMigration',
21
    ];
22
23
    public function getDefinition(): RectorDefinition
24
    {
25
        return new RectorDefinition("Renames usages for class names that have been renamed", [
26
            new CodeSample(
27
                <<<'CODE_SAMPLE'
28
use Doctrine\DBAL\Migrations\AbortMigrationException;
29
use Doctrine\DBAL\Migrations\IrreversibleMigrationException;
30
use Doctrine\DBAL\Migrations\SkipMigrationException;
31
class Version20180101150000 extends AbstractMigration
32
{
33
}
34
CODE_SAMPLE
35
                ,
36
                <<<'CODE_SAMPLE'
37
use Doctrine\Migrations\Exception\AbortMigration;
38
use Doctrine\Migrations\Exception\IrreversibleMigration;
39
use Doctrine\Migrations\Exception\SkipMigration;
40
class Version20180101150000 extends AbstractMigration
41
{
42
}
43
CODE_SAMPLE
44
            )
45
        ]);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 1
    public function getNodeTypes(): array
52
    {
53 1
        return [Name::class];
54
    }
55
56 1
    public function refactor(Node $node): ?Node
57
    {
58 1
        assert($node instanceof Name);
59
60 1
        foreach (self::CLASS_RENAMES as $old => $new) {
61 1
            if ($this->isName($node, $old)) {
62 1
                $qualifiedNameParts = explode('\\', $new);
63 1
                if ($node->isQualified()) {
64 1
                    return new Name($qualifiedNameParts);
65
                } else {
66 1
                    $className = $qualifiedNameParts[count($qualifiedNameParts) - 1];
67 1
                    return new Name($className);
68
                }
69
            }
70
        }
71
72 1
        return null;
73
    }
74
}
75