MigrationSignatureRector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 79
ccs 13
cts 19
cp 0.6842
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinition() 0 21 1
A refactor() 0 20 6
A getNodeTypes() 0 3 1
1
<?php
2
declare(strict_types=1);
3
namespace Sserbin\DoctrineMigrations2Upgrade;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Identifier;
7
use PhpParser\Node\NullableType;
8
use PhpParser\Node\Stmt\ClassMethod;
9
use Rector\Rector\AbstractRector;
10
use Rector\RectorDefinition\CodeSample;
11
use Rector\RectorDefinition\RectorDefinition;
12
13
class MigrationSignatureRector extends AbstractRector
14
{
15
    private const REPLACEMENTS_MAP = [
16
        'up' => 'void',
17
        'down' => 'void',
18
        'preUp' => 'void',
19
        'postUp' => 'void',
20
        'preDown' => 'void',
21
        'postDown' => 'void',
22
        'getDescription' => 'string',
23
        'isTransactional' => 'bool',
24
    ];
25
26
    public function getDefinition(): RectorDefinition
27
    {
28
        return new RectorDefinition(
29
            "Fixes up/down signature to conform to v2 of Doctrine\Migrations\AbstractMigration",
30
            [
31
            new CodeSample(
32
                <<<'CODE_SAMPLE'
33
class Version20180101150000 extends AbstractMigration
34
{
35
    public function up(Schema $schema)
36
    {
37
38
    }
39
    public function down(Schema $schema)
40
    {
41
42
    }
43
}
44
CODE_SAMPLE
45
                ,
46
                <<<'CODE_SAMPLE'
47
class Version20180101150000 extends AbstractMigration
48
{
49
    public function up(Schema $schema): void
50
    {
51
52
    }
53
    public function down(Schema $schema): void
54
    {
55
56
    }
57
}
58
CODE_SAMPLE
59
            )
60
            ]
61
        );
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 1
    public function getNodeTypes(): array
68
    {
69 1
        return [ClassMethod::class];
70
    }
71
72 1
    public function refactor(Node $node): ?Node
73
    {
74 1
        assert($node instanceof ClassMethod);
75
76 1
        foreach (self::REPLACEMENTS_MAP as $method => $type) {
77 1
            if (!$this->isName($node, $method)) {
78 1
                continue;
79
            }
80
81 1
            if ($node->returnType !== null
82 1
                && !$node->returnType instanceof NullableType
83 1
                && $node->returnType->toString() === $type
84
            ) {
85 1
                return null; // already set, skip
86
            }
87
88 1
            $node->returnType = new Identifier($type);
89
        }
90
91 1
        return null;
92
    }
93
}
94