forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveServicesBySuffixToDirectoryRector.php
176 lines (149 loc) · 5.21 KB
/
MoveServicesBySuffixToDirectoryRector.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Rector\Autodiscovery\Rector\FileNode;
use Nette\Utils\Strings;
use PhpParser\Node;
use Rector\Autodiscovery\FileLocation\ExpectedFileLocationResolver;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileNode;
use Rector\Core\Rector\AbstractRector;
use Rector\FileSystemRector\ValueObject\MovedFileWithNodes;
use Rector\FileSystemRector\ValueObjectFactory\MovedFileWithNodesFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Symplify\SmartFileSystem\SmartFileInfo;
use Webmozart\Assert\Assert;
/**
* Inspiration @see https://github.com/rectorphp/rector/pull/1865/files#diff-0d18e660cdb626958662641b491623f8
*
* @see \Rector\Tests\Autodiscovery\Rector\FileNode\MoveServicesBySuffixToDirectoryRector\MoveServicesBySuffixToDirectoryRectorTest
* @see \Rector\Tests\Autodiscovery\Rector\FileNode\MoveServicesBySuffixToDirectoryRector\MutualRenameTest
*/
final class MoveServicesBySuffixToDirectoryRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const GROUP_NAMES_BY_SUFFIX = 'group_names_by_suffix';
/**
* @var string[]
*/
private $groupNamesBySuffix = [];
/**
* @var ExpectedFileLocationResolver
*/
private $expectedFileLocationResolver;
/**
* @var MovedFileWithNodesFactory
*/
private $movedFileWithNodesFactory;
public function __construct(
ExpectedFileLocationResolver $expectedFileLocationResolver,
MovedFileWithNodesFactory $movedFileWithNodesFactory
) {
$this->expectedFileLocationResolver = $expectedFileLocationResolver;
$this->movedFileWithNodesFactory = $movedFileWithNodesFactory;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Move classes by their suffix to their own group/directory', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
// file: app/Entity/ProductRepository.php
namespace App/Entity;
class ProductRepository
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
// file: app/Repository/ProductRepository.php
namespace App/Repository;
class ProductRepository
{
}
CODE_SAMPLE
,
[
self::GROUP_NAMES_BY_SUFFIX => ['Repository'],
]
),
]);
}
/**
* @param FileNode $node
*/
public function refactor(Node $node): ?Node
{
$classLikes = $this->betterNodeFinder->findClassLikes($node);
if ($classLikes === []) {
return null;
}
$this->processGroupNamesBySuffix($node->getFileInfo(), $node, $this->groupNamesBySuffix);
return null;
}
public function configure(array $configuration): void
{
$groupNamesBySuffix = $configuration[self::GROUP_NAMES_BY_SUFFIX] ?? [];
Assert::allString($groupNamesBySuffix);
$this->groupNamesBySuffix = $groupNamesBySuffix;
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FileNode::class];
}
/**
* A. Match classes by suffix and move them to group namespace
*
* E.g. "App\Controller\SomeException"
* ↓
* "App\Exception\SomeException"
*
* @param string[] $groupNamesBySuffix
*/
private function processGroupNamesBySuffix(
SmartFileInfo $smartFileInfo,
FileNode $fileNode,
array $groupNamesBySuffix
): void {
foreach ($groupNamesBySuffix as $groupNames) {
// has class suffix
$suffixPattern = '\w+' . $groupNames . '(Test)?\.php$';
if (! Strings::match($smartFileInfo->getRealPath(), '#' . $suffixPattern . '#')) {
continue;
}
if ($this->isLocatedInExpectedLocation($groupNames, $suffixPattern, $smartFileInfo)) {
continue;
}
// file is already in the group
if (Strings::match($smartFileInfo->getPath(), '#' . $groupNames . '$#')) {
continue;
}
$this->moveFileToGroupName($smartFileInfo, $fileNode, $groupNames);
return;
}
}
private function isLocatedInExpectedLocation(
string $groupName,
string $suffixPattern,
SmartFileInfo $smartFileInfo
): bool {
$expectedLocationFilePattern = $this->expectedFileLocationResolver->resolve($groupName, $suffixPattern);
return (bool) Strings::match($smartFileInfo->getRealPath(), $expectedLocationFilePattern);
}
private function moveFileToGroupName(SmartFileInfo $fileInfo, FileNode $fileNode, string $desiredGroupName): void
{
$movedFileWithNodes = $this->movedFileWithNodesFactory->createWithDesiredGroup(
$fileInfo,
$fileNode->stmts,
$desiredGroupName
);
if (! $movedFileWithNodes instanceof MovedFileWithNodes) {
return;
}
$this->removedAndAddedFilesCollector->addMovedFile($movedFileWithNodes);
}
}