Skip to content

Commit f168f2d

Browse files
committed
refactor(docblock): Simplify parameter names in closures
- Rename parameters in closures to underscore for clarity - Improve readability of the code by reducing unnecessary variable names - Maintain functionality while enhancing code style consistency
1 parent 6194e51 commit f168f2d

File tree

16 files changed

+198
-82
lines changed

16 files changed

+198
-82
lines changed

composer-bump

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ final class ComposerBump
307307
$process = Process::fromShellCommandline(implode(' ', array_merge([$command], $options)), $cwd, $env, $input, $timeout);
308308
$this->symfonyStyle->warning("The command [{$process->getCommandLine()}] is running in the working directory [$cwd] .");
309309

310-
return $process->mustRun(function (string $type, string $buffer): void {
310+
return $process->mustRun(function (string $_, string $buffer): void {
311311
$this->symfonyStyle->isVerbose() and $this->symfonyStyle->write($buffer);
312312
});
313313
}

config/set/rector.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,43 @@
2020
use PhpParser\Node\Name\FullyQualified;
2121
use PhpParser\Node\Scalar\Int_;
2222
use PhpParser\Node\Scalar\String_;
23+
use PhpParser\NodeFinder;
24+
use PhpParser\PrettyPrinter\Standard;
25+
use PHPStan\PhpDocParser\Parser\PhpDocParser;
26+
use PHPStan\PhpDocParser\Parser\TokenIterator;
27+
use PHPStan\Reflection\BetterReflection\BetterReflectionSourceLocatorFactory;
28+
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
29+
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
2330
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
2431
use Rector\Config\RectorConfig;
2532
use Rector\NodeTypeResolver\Node\AttributeKey;
33+
use Rector\NodeTypeResolver\Reflection\BetterReflection\RectorBetterReflectionSourceLocatorFactory;
34+
use Rector\PhpParser\Node\BetterNodeFinder;
35+
use Rector\PhpParser\Parser\RectorParser;
36+
use Rector\PhpParser\Parser\SimplePhpParser;
37+
use Rector\PhpParser\Printer\BetterStandardPrinter;
38+
use Rector\Renaming\Rector\Name\RenameClassRector;
2639
use Rector\Transform\Rector\Scalar\ScalarValueToConstFetchRector;
2740
use Rector\Transform\ValueObject\ScalarValueToConstFetch;
2841
use Rector\ValueObject\PhpVersion;
2942

3043
return static function (RectorConfig $rectorConfig): void {
3144
$rectorConfig->import(__DIR__.'/../config.php');
45+
$rectorConfig->skip([__FILE__]);
3246
$rectorConfig->rules([
3347
UpdateRectorCodeSamplesFromFixturesRector::class,
3448
UpdateRectorMethodNodeParamDocblockFromNodeTypesRector::class,
3549
]);
3650

51+
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
52+
BetterReflectionSourceLocatorFactory::class => RectorBetterReflectionSourceLocatorFactory::class,
53+
NodeFinder::class => BetterNodeFinder::class,
54+
PhpDocParser::class => BetterPhpDocParser::class,
55+
// SimplePhpParser::class => RectorParser::class,
56+
Standard::class => BetterStandardPrinter::class,
57+
TokenIterator::class => BetterTokenIterator::class,
58+
]);
59+
3760
$rectorConfig->ruleWithConfiguration(
3861
ScalarValueToConstFetchRector::class,
3962
collect((new ReflectionClass(AttributeKey::class))->getConstants())

docs/rules-overview.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,26 @@ Rename garbage variable name
258258

259259
```diff
260260
/** @noinspection ALL */
261-
-collect($array)->filter(static function ($value, int $key): bool {
262-
+collect($array)->filter(static function ($_, int $key): bool {
261+
-collect($array)->filter(static function (string $value, int $key): bool {
262+
+collect($array)->filter(static function (string $_, int $key): bool {
263263
return 2 === $key;
264264
});
265265

266266
/**
267-
- * @param mixed $value
267+
- * @param mixed $value
268268
+ * @param mixed $_
269269
*/
270-
-function filter($value, int $key): bool
271-
+function filter($_, int $key): bool
270+
-function filter($value, $key): bool
271+
+function filter($_, $key): bool
272+
{
273+
return 2 === $key;
274+
}
275+
276+
/**
277+
* @param mixed $key
278+
*/
279+
-function filter($value, $key): bool
280+
+function filter($_, $key): bool
272281
{
273282
return 2 === $key;
274283
}

monorepo-builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
]))
7575
->setEnv(['COMPOSER_MEMORY_LIMIT' => -1])
7676
->setTimeout(600)
77-
->mustRun(static function (string $type, string $buffer): void {
77+
->mustRun(static function (string $_, string $buffer): void {
7878
$symfonyStyle ??= new SymfonyStyle(new ArgvInput, new ConsoleOutput);
7979
$symfonyStyle->write($buffer);
8080
});

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ parameters:
148148
identifier: complexity.functionLike
149149
path: src/Rector/Name/RenameToConventionalCaseNameRector.php
150150

151+
-
152+
identifier: guanguans.exceptionMustImplementNativeThrowable
153+
path: src/Support/helpers.php
151154
-
152155
identifier: rector.noClassReflectionStaticReflection
153156
path: src/Support/helpers.php

rector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
])
155155
->withConfiguredRule(
156156
ChangeMethodVisibilityRector::class,
157-
classes(static fn (string $class, string $file): bool => str_starts_with($class, 'Guanguans\RectorRules'))
157+
classes(static fn (string $class): bool => str_starts_with($class, 'Guanguans\RectorRules'))
158158
->filter(static fn (ReflectionClass $reflectionClass): bool => $reflectionClass->isTrait())
159159
->map(
160160
static fn (ReflectionClass $reflectionClass): array => collect($reflectionClass->getMethods(ReflectionMethod::IS_PRIVATE))

src/Rector/Array_/UpdateRectorCodeSamplesFromFixturesRector.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
use PhpParser\Node\Expr\New_;
2727
use PhpParser\Node\Name\FullyQualified;
2828
use PhpParser\Node\Scalar\String_;
29-
use PhpParser\NodeTraverser;
30-
use PhpParser\NodeVisitor\FirstFindingVisitor;
3129
use PHPStan\Reflection\ClassReflection;
3230
use Rector\Config\RectorConfig;
3331
use Rector\Contract\Rector\ConfigurableRectorInterface;
3432
use Rector\NodeTypeResolver\Node\AttributeKey;
33+
use Rector\PhpParser\Node\BetterNodeFinder;
3534
use Rector\PhpParser\Parser\SimplePhpParser;
3635
use Rector\PHPStan\ScopeFetcher;
3736
use Rector\Testing\Fixture\FixtureSplitter;
@@ -47,12 +46,16 @@
4746
*/
4847
final class UpdateRectorCodeSamplesFromFixturesRector extends AbstractRector
4948
{
49+
private BetterNodeFinder $betterNodeFinder;
5050
private SimplePhpParser $simplePhpParser;
5151

52-
public function __construct(SimplePhpParser $simplePhpParser)
53-
{
52+
public function __construct(
53+
BetterNodeFinder $betterNodeFinder,
54+
SimplePhpParser $simplePhpParser
55+
) {
5456
// $this->rectorConfig = clone $rectorConfig;
5557
// $this->rectorConfig = unserialize(serialize($rectorConfig), ['allowed_classes' => true]);
58+
$this->betterNodeFinder = $betterNodeFinder;
5659
$this->simplePhpParser = $simplePhpParser;
5760
}
5861

@@ -262,10 +265,10 @@ private function createConfigurationArrayNode(ClassReflection $classReflection):
262265
static $configurationNodes = [];
263266

264267
if (!isset($configurationNodes[$configFile])) {
265-
(new NodeTraverser($firstFindingVisitor = new FirstFindingVisitor(
266-
static fn (Node $node): bool => $node instanceof Array_
267-
)))->traverse($this->simplePhpParser->parseFile($configFile));
268-
$configurationNodes[$configFile] = $firstFindingVisitor->getFoundNode();
268+
$configurationNodes[$configFile] = $this->betterNodeFinder->findFirstInstanceOf(
269+
$this->simplePhpParser->parseFile($configFile),
270+
Array_::class
271+
);
269272
}
270273

271274
return $configurationNodes[$configFile];

src/Rector/File/AddNoinspectionDocblockToFileFirstStmtRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private function getInspections(): array
158158
static $inspectionsMap = [];
159159

160160
$inspectionsMap[$this->file->getFilePath()] ??= collect($this->inspectionsMap)
161-
->filter(fn (array $inspections, string $path) => Str::is($path, $this->file->getFilePath()))
161+
->filter(fn (array $_, string $path) => Str::is($path, $this->file->getFilePath()))
162162
// ->flatten()
163163
->collapse()
164164
->unique()

src/Rector/FunctionLike/RenameGarbageVariableNameRector.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
/** @noinspection PhpMultipleClassDeclarationsInspection */
4+
/** @noinspection EfferentObjectCouplingInspection */
45
/** @noinspection PhpUnusedAliasInspection */
56
declare(strict_types=1);
67

@@ -93,14 +94,22 @@ protected function codeSamples(): array
9394
new CodeSample(
9495
<<<'PHP'
9596
/** @noinspection ALL */
96-
collect($array)->filter(static function ($value, int $key): bool {
97+
collect($array)->filter(static function (string $value, int $key): bool {
9798
return 2 === $key;
9899
});
99100
100101
/**
101-
* @param mixed $value
102+
* @param mixed $value
102103
*/
103-
function filter($value, int $key): bool
104+
function filter($value, $key): bool
105+
{
106+
return 2 === $key;
107+
}
108+
109+
/**
110+
* @param mixed $key
111+
*/
112+
function filter($value, $key): bool
104113
{
105114
return 2 === $key;
106115
}
@@ -120,14 +129,22 @@ function array_is_list(array $array): bool
120129
PHP,
121130
<<<'PHP'
122131
/** @noinspection ALL */
123-
collect($array)->filter(static function ($_, int $key): bool {
132+
collect($array)->filter(static function (string $_, int $key): bool {
124133
return 2 === $key;
125134
});
126135
127136
/**
128137
* @param mixed $_
129138
*/
130-
function filter($_, int $key): bool
139+
function filter($_, $key): bool
140+
{
141+
return 2 === $key;
142+
}
143+
144+
/**
145+
* @param mixed $key
146+
*/
147+
function filter($_, $key): bool
131148
{
132149
return 2 === $key;
133150
}
@@ -240,17 +257,13 @@ private function updateParamTagValueNodeParameterName(?PhpDocInfo $phpDocInfo, s
240257
return false;
241258
}
242259

243-
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($name);
244-
245-
if (!$paramTagValueNode instanceof ParamTagValueNode) {
246-
return false;
247-
}
248-
249260
$phpDocTagNodes = $phpDocInfo->getTagsByName('param');
250261

251262
foreach ($phpDocTagNodes as $phpDocTagNode) {
252-
if ($phpDocTagNode->value === $paramTagValueNode) {
253-
$paramTagValueNode->parameterName = '$'.$newName;
263+
\assert($phpDocTagNode->value instanceof ParamTagValueNode);
264+
265+
if ('$'.$name === $phpDocTagNode->value->parameterName) {
266+
$phpDocTagNode->value->parameterName = '$'.$newName;
254267

255268
/** @see \Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter::printDocChildNode() */
256269
$phpDocTagNode->setAttribute(PhpDocAttributeKey::START_AND_END, null);

src/Rector/Namespace_/RemoveNamespaceRector.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@
2121
use PhpParser\Node\Stmt\Function_;
2222
use PhpParser\Node\Stmt\Namespace_;
2323
use PhpParser\Node\Stmt\Nop;
24-
use PhpParser\NodeTraverser;
25-
use PhpParser\NodeVisitor\FirstFindingVisitor;
2624
use Rector\NodeTypeResolver\Node\AttributeKey;
25+
use Rector\PhpParser\Node\BetterNodeFinder;
2726
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
28-
use function Guanguans\RectorRules\Support\is_instance_of_any;
2927

3028
/**
3129
* @see \Guanguans\RectorRulesTests\Rector\Namespace_\RemoveNamespaceRector\RemoveNamespaceRectorTest
3230
*/
3331
final class RemoveNamespaceRector extends AbstractRector
3432
{
33+
private BetterNodeFinder $betterNodeFinder;
34+
35+
public function __construct(BetterNodeFinder $betterNodeFinder)
36+
{
37+
$this->betterNodeFinder = $betterNodeFinder;
38+
}
39+
3540
public function getNodeTypes(): array
3641
{
3742
return [
@@ -46,7 +51,12 @@ public function getNodeTypes(): array
4651
*/
4752
public function refactor(Node $node): ?array
4853
{
49-
if ($this->findFirstSkippedNode($node) instanceof Node) {
54+
if ($this->betterNodeFinder->hasInstancesOf($node, [
55+
/** ClassLike(Attribute、Class、Enum、Interface、Trait)、Constant、Function. */
56+
ClassLike::class,
57+
Const_::class,
58+
Function_::class,
59+
])) {
5060
return null;
5161
}
5262

@@ -85,25 +95,4 @@ protected function codeSamples(): array
8595
),
8696
];
8797
}
88-
89-
/**
90-
* @see \PhpParser\NodeVisitor\
91-
* @see \PhpParser\NodeVisitor\FirstFindingVisitor
92-
*
93-
* @return null|\PhpParser\Node\Stmt\ClassLike|\PhpParser\Node\Stmt\Const_|\PhpParser\Node\Stmt\Function_
94-
*/
95-
private function findFirstSkippedNode(Namespace_ $namespaceNode): ?Node
96-
{
97-
$nodeTraverser = new NodeTraverser($firstFindingVisitor = new FirstFindingVisitor(
98-
static fn (Node $node): bool => is_instance_of_any($node, [
99-
/** ClassLike(Attribute、Class、Enum、Interface、Trait)、Constant、Function. */
100-
ClassLike::class,
101-
Const_::class,
102-
Function_::class,
103-
])
104-
));
105-
$nodeTraverser->traverse([$namespaceNode]);
106-
107-
return $firstFindingVisitor->getFoundNode();
108-
}
10998
}

0 commit comments

Comments
 (0)