forked from guanguans/rector-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddNoinspectionDocblockToFileFirstStmtRector.php
More file actions
172 lines (151 loc) · 5.74 KB
/
AddNoinspectionDocblockToFileFirstStmtRector.php
File metadata and controls
172 lines (151 loc) · 5.74 KB
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
<?php
/** @noinspection PhpMultipleClassDeclarationsInspection */
declare(strict_types=1);
/**
* Copyright (c) 2025-2026 guanguans<ityaozm@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/rector-rules
*/
namespace Guanguans\RectorRules\Rector\File;
use Guanguans\RectorRules\Rector\AbstractRector;
use Illuminate\Support\Str;
use PhpParser\Comment;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\FileNode;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Webmozart\Assert\Assert;
/**
* @see \Guanguans\RectorRulesTests\Rector\File\AddNoinspectionDocblockToFileFirstStmtRector\AddNoinspectionDocblockToFileFirstStmtRectorTest
*/
final class AddNoinspectionDocblockToFileFirstStmtRector extends AbstractRector implements ConfigurableRectorInterface
{
/** @var array<non-empty-string, non-empty-list<string>> */
private array $inspectionsMap = [];
public function getNodeTypes(): array
{
return [
FileNode::class,
];
}
/**
* @param \Rector\PhpParser\Node\FileNode $node
*/
public function refactor(Node $node): ?Node
{
if ([] === $node->stmts || [] === $this->getInspections()) {
return null;
}
$stmtNode = $node->stmts[0];
$comments = $stmtNode->getComments();
$newComments = collect($comments)
->merge(array_map(
static fn (string $inspection): Doc => new Doc("/** @noinspection $inspection */"),
$this->getInspections()
))
->unique(static fn (Comment $comment): string => $comment->getText())
->sort(function (Comment $a, Comment $b): int {
if ($this->inspectionsContains($a) && !$this->inspectionsContains($b)) {
return -1;
}
if (!$this->inspectionsContains($a) && $this->inspectionsContains($b)) {
return 1;
}
return $a->getText() <=> $b->getText();
})
->all();
if ($newComments === $comments) {
return null;
}
$stmtNode->setAttribute(AttributeKey::COMMENTS, $newComments);
return $node;
}
/**
* @param array<non-empty-string, non-empty-list<string>> $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsArray($configuration);
Assert::allNotEmpty($configuration);
Assert::allStringNotEmpty(array_keys($configuration));
$this->inspectionsMap = $configuration;
}
/**
* @throws \Symplify\RuleDocGenerator\Exception\ShouldNotHappenException
*
* @return list<\Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample>
*/
protected function codeSamples(): array
{
return [
new ConfiguredCodeSample(
<<<'PHP'
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */
/** @noinspection ALL */
/** @noinspection PhpUnusedAliasInspection */
declare(strict_types=1);
PHP,
<<<'PHP'
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
/** @noinspection PhpUndefinedClassInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpVoidFunctionResultUsedInspection */
/** @noinspection StaticClosureCanBeUsedInspection */
/** @noinspection ALL */
/** @noinspection PhpUnusedAliasInspection */
declare(strict_types=1);
PHP,
[
'*/Fixture/fixture.php' => [
'AnonymousFunctionStaticInspection',
'StaticClosureCanBeUsedInspection'],
'*/fixture.php' => [
'NullPointerExceptionInspection',
'PhpPossiblePolymorphicInvocationInspection',
'PhpUndefinedClassInspection',
'PhpUnhandledExceptionInspection',
'PhpVoidFunctionResultUsedInspection',
],
'*/skip_same_inspections.php' => [
'ALL',
],
],
),
];
}
private function inspectionsContains(Comment $comment): bool
{
foreach ($this->getInspections() as $inspection) {
if (str_contains($comment->getText(), $inspection)) {
return true;
}
}
return false;
}
/**
* @return list<string>
*/
private function getInspections(): array
{
/** @var array<non-empty-string, list<string>> $inspectionsMap */
static $inspectionsMap = [];
$inspectionsMap[$this->getFile()->getFilePath()] ??= collect($this->inspectionsMap)
->filter(fn (array $_, string $path) => Str::is($path, $this->getFile()->getFilePath()))
// ->flatten()
->collapse()
->unique()
// ->sort()
// ->values()
// ->dd()
->all();
return $inspectionsMap[$this->getFile()->getFilePath()];
}
}