forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveInterfacesToContractNamespaceDirectoryRector.php
104 lines (87 loc) · 2.82 KB
/
MoveInterfacesToContractNamespaceDirectoryRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace Rector\Autodiscovery\Rector\FileNode;
use PhpParser\Node;
use PhpParser\Node\Stmt\Interface_;
use Rector\Core\PhpParser\Node\CustomNode\FileNode;
use Rector\Core\Rector\AbstractRector;
use Rector\FileSystemRector\ValueObject\MovedFileWithNodes;
use Rector\FileSystemRector\ValueObjectFactory\MovedFileWithNodesFactory;
use Rector\NetteToSymfony\NodeAnalyzer\NetteControlFactoryInterfaceAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Inspiration @see https://github.com/rectorphp/rector/pull/1865/files#diff-0d18e660cdb626958662641b491623f8
*
* @see \Rector\Tests\Autodiscovery\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\MoveInterfacesToContractNamespaceDirectoryRectorTest
*/
final class MoveInterfacesToContractNamespaceDirectoryRector extends AbstractRector
{
/**
* @var NetteControlFactoryInterfaceAnalyzer
*/
private $netteControlFactoryInterfaceAnalyzer;
/**
* @var MovedFileWithNodesFactory
*/
private $movedFileWithNodesFactory;
public function __construct(
NetteControlFactoryInterfaceAnalyzer $netteControlFactoryInterfaceAnalyzer,
MovedFileWithNodesFactory $movedFileWithNodesFactory
) {
$this->netteControlFactoryInterfaceAnalyzer = $netteControlFactoryInterfaceAnalyzer;
$this->movedFileWithNodesFactory = $movedFileWithNodesFactory;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Move interface to "Contract" namespace', [
new CodeSample(
<<<'CODE_SAMPLE'
// file: app/Exception/Rule.php
namespace App\Exception;
interface Rule
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
// file: app/Contract/Rule.php
namespace App\Contract;
interface Rule
{
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FileNode::class];
}
/**
* @param FileNode $node
*/
public function refactor(Node $node): ?Node
{
$interface = $this->betterNodeFinder->findFirstInstanceOf([$node], Interface_::class);
if (! $interface instanceof Interface_) {
return null;
}
if ($this->netteControlFactoryInterfaceAnalyzer->isComponentFactoryInterface($interface)) {
return null;
}
$movedFileWithNodes = $this->movedFileWithNodesFactory->createWithDesiredGroup(
$node->getFileInfo(),
$node->stmts,
'Contract'
);
if (! $movedFileWithNodes instanceof MovedFileWithNodes) {
return null;
}
$this->removedAndAddedFilesCollector->addMovedFile($movedFileWithNodes);
return null;
}
}