Skip to content

Commit 5136cf1

Browse files
committed
feat(set): Add PHPBENCH set configuration
- Introduced a new PHPBENCH set in SetList.php for better organization. - Created phpbench.php to configure PhpBench rules. - Ensured compatibility by checking for the existence of BeforeMethods class.
1 parent 9c1192b commit 5136cf1

File tree

8 files changed

+94
-18
lines changed

8 files changed

+94
-18
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ composer require guanguans/rector-rules --dev --ansi -v
2525

2626
### :monocle_face: [Rules Overview](docs/rules-overview.md)
2727

28+
### :monocle_face: [Sets Overview](config/set/)
29+
30+
* [`Guanguans\RectorRules\Set\SetList::ALL`](src/Set/SetList.php)
31+
* [`Guanguans\RectorRules\Set\SetList::COMMON`](src/Set/SetList.php)
32+
* [`Guanguans\RectorRules\Set\SetList::PHPBENCH`](src/Set/SetList.php)
33+
* [`Guanguans\RectorRules\Set\SetList::PHPSTAN`](src/Set/SetList.php)
34+
* [`Guanguans\RectorRules\Set\SetList::RECTOR`](src/Set/SetList.php)
35+
2836
### In your rector configuration register rules
2937

3038
```php
@@ -34,6 +42,11 @@ use PhpParser\NodeVisitor\ParentConnectingVisitor;
3442
use Rector\Config\RectorConfig;
3543

3644
return RectorConfig::configure()
45+
->withSets([
46+
Guanguans\RectorRules\Set\SetList::ALL,
47+
// ...
48+
])
49+
// ...
3750
->registerDecoratingNodeVisitor(ParentConnectingVisitor::class)
3851
->withConfiguredRule(RenameToPsrNameRector::class, [
3952
'assertMatches*Snapshot',

config/set/all.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$rectorConfig->import(__DIR__.'/../config.php');
2121
$rectorConfig->sets([
2222
SetList::COMMON,
23+
SetList::PHPBENCH,
2324
SetList::PHPSTAN,
2425
SetList::RECTOR,
2526
]);

config/set/phpbench.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/** @noinspection PhpInternalEntityUsedInspection */
4+
5+
declare(strict_types=1);
6+
7+
/**
8+
* Copyright (c) 2025-2026 guanguans<ityaozm@gmail.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*
13+
* @see https://github.com/guanguans/rector-rules
14+
*/
15+
16+
use PhpBench\Attributes\BeforeMethods;
17+
use Rector\Config\RectorConfig;
18+
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
19+
use Rector\Php80\ValueObject\AnnotationToAttribute;
20+
use Rector\ValueObject\PhpVersion;
21+
22+
return static function (RectorConfig $rectorConfig): void {
23+
if (\PHP_VERSION_ID < PhpVersion::PHP_80 || !class_exists(BeforeMethods::class)) {
24+
return;
25+
}
26+
27+
$rectorConfig->import(__DIR__.'/../config.php');
28+
$rectorConfig->ruleWithConfiguration(
29+
AnnotationToAttributeRector::class,
30+
array_reduce(
31+
glob(\sprintf('%s/*.php', \dirname((new ReflectionClass(BeforeMethods::class))->getFileName()))),
32+
static function (array $annotationToAttributes, string $file): array {
33+
$filename = pathinfo($file, \PATHINFO_FILENAME);
34+
35+
if ('AbstractMethodsAttribute' === $filename) {
36+
return $annotationToAttributes;
37+
}
38+
39+
$annotationToAttributes[] = new AnnotationToAttribute($filename, "PhpBench\\Attributes\\$filename");
40+
41+
return $annotationToAttributes;
42+
},
43+
[]
44+
)
45+
);
46+
};

config/set/rector.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,41 @@
1515

1616
use Guanguans\RectorRules\Rector\Array_\UpdateRectorCodeSamplesFromFixturesRector;
1717
use Guanguans\RectorRules\Rector\Class_\UpdateRectorMethodNodeParamDocblockFromNodeTypesRector;
18+
use PhpParser\Node\Expr\ClassConstFetch;
19+
use PhpParser\Node\Identifier;
20+
use PhpParser\Node\Name\FullyQualified;
21+
use PhpParser\Node\Scalar\Int_;
22+
use PhpParser\Node\Scalar\String_;
1823
use Rector\Config\RectorConfig;
24+
use Rector\NodeTypeResolver\Node\AttributeKey;
25+
use Rector\Transform\Rector\Scalar\ScalarValueToConstFetchRector;
26+
use Rector\Transform\ValueObject\ScalarValueToConstFetch;
27+
use Rector\ValueObject\PhpVersion;
1928

2029
return static function (RectorConfig $rectorConfig): void {
2130
$rectorConfig->import(__DIR__.'/../config.php');
2231
$rectorConfig->rules([
2332
UpdateRectorCodeSamplesFromFixturesRector::class,
2433
UpdateRectorMethodNodeParamDocblockFromNodeTypesRector::class,
2534
]);
35+
36+
$rectorConfig->ruleWithConfiguration(
37+
ScalarValueToConstFetchRector::class,
38+
collect((new ReflectionClass(AttributeKey::class))->getConstants())
39+
->map(static fn (string $value, string $name): ScalarValueToConstFetch => new ScalarValueToConstFetch(
40+
new String_($value),
41+
new ClassConstFetch(new FullyQualified(AttributeKey::class), new Identifier($name))
42+
))
43+
->all()
44+
);
45+
46+
$rectorConfig->ruleWithConfiguration(
47+
ScalarValueToConstFetchRector::class,
48+
collect((new ReflectionClass(PhpVersion::class))->getConstants())
49+
->map(static fn (int $value, string $name): ScalarValueToConstFetch => new ScalarValueToConstFetch(
50+
new Int_($value),
51+
new ClassConstFetch(new FullyQualified(PhpVersion::class), new Identifier($name))
52+
))
53+
->all()
54+
);
2655
};

rector.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
use Guanguans\RectorRules\Rector\Name\RenameToPsrNameRector;
2424
use Guanguans\RectorRules\Rector\New_\NewExceptionToNewAnonymousExtendsExceptionImplementsRector;
2525
use Illuminate\Support\Str;
26-
use PhpParser\Node\Expr\ClassConstFetch;
27-
use PhpParser\Node\Identifier;
28-
use PhpParser\Node\Name\FullyQualified;
29-
use PhpParser\Node\Scalar\String_;
3026
use PhpParser\NodeVisitor\ParentConnectingVisitor;
3127
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
3228
use Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector;
@@ -48,17 +44,14 @@
4844
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
4945
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
5046
use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector;
51-
use Rector\NodeTypeResolver\Node\AttributeKey;
5247
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
5348
use Rector\PHPUnit\Set\PHPUnitSetList;
5449
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
5550
use Rector\Set\ValueObject\DowngradeLevelSetList;
5651
use Rector\Set\ValueObject\SetList;
5752
use Rector\Transform\Rector\FuncCall\FuncCallToStaticCallRector;
58-
use Rector\Transform\Rector\Scalar\ScalarValueToConstFetchRector;
5953
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
6054
use Rector\Transform\ValueObject\FuncCallToStaticCall;
61-
use Rector\Transform\ValueObject\ScalarValueToConstFetch;
6255
use Rector\Transform\ValueObject\StaticCallToFuncCall;
6356
use Rector\ValueObject\PhpVersion;
6457
use Rector\ValueObject\Visibility;
@@ -164,15 +157,6 @@
164157
'phpstan-ignore-next-line',
165158
'psalm-suppress',
166159
])
167-
->withConfiguredRule(
168-
ScalarValueToConstFetchRector::class,
169-
collect((new ReflectionClass(AttributeKey::class))->getConstants())
170-
->map(static fn (string $value, string $name): ScalarValueToConstFetch => new ScalarValueToConstFetch(
171-
new String_($value),
172-
new ClassConstFetch(new FullyQualified(AttributeKey::class), new Identifier($name))
173-
))
174-
->all()
175-
)
176160
->withConfiguredRule(StaticCallToFuncCallRector::class, [
177161
// new StaticCallToFuncCall(Str::class, 'of', 'str'),
178162
])

src/Rector/Class_/UpdatePHPStanMethodNodeParamDocblockFromNodeTypesRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpParser\Node\Stmt\Class_;
2020
use PhpParser\Node\Stmt\ClassMethod;
2121
use PHPStan\Rules\Rule;
22+
use Rector\ValueObject\PhpVersion;
2223
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2324

2425
/**
@@ -113,7 +114,7 @@ protected function nodeTypes(\ReflectionClass $reflectionClass): array
113114

114115
if (method_exists($rule, $methodName = 'getNodeTypes')) {
115116
$reflectionMethod = $reflectionClass->getMethod($methodName);
116-
\PHP_VERSION_ID < 80100 and $reflectionMethod->setAccessible(true);
117+
\PHP_VERSION_ID < PhpVersion::PHP_81 and $reflectionMethod->setAccessible(true);
117118

118119
return $reflectionMethod->invoke($rule);
119120
}

src/Rector/Name/RenameToPsrNameRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use Rector\Contract\Rector\ConfigurableRectorInterface;
5656
use Rector\NodeTypeResolver\Node\AttributeKey;
5757
use Rector\PHPStan\ScopeFetcher;
58+
use Rector\ValueObject\PhpVersion;
5859
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
5960
use Webmozart\Assert\Assert;
6061
use function Guanguans\RectorRules\Support\is_instance_of_any;
@@ -139,7 +140,7 @@ function (SymfonyStyleFactory $symfonyStyleFactory): void { // @codeCoverageIgno
139140
$reflectionClass = (new \ReflectionObject($rectorStyle))->getParentClass();
140141
\assert($reflectionClass instanceof \ReflectionClass);
141142
$reflectionProperty = $reflectionClass->getProperty('input');
142-
\PHP_VERSION_ID < 80100 and $reflectionProperty->setAccessible(true);
143+
\PHP_VERSION_ID < PhpVersion::PHP_81 and $reflectionProperty->setAccessible(true);
143144
$input = $reflectionProperty->getValue($rectorStyle);
144145
// \assert($input instanceof \Symfony\Component\Console\Input\InputInterface);
145146

src/Set/SetList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ final class SetList
2020
{
2121
public const ALL = __DIR__.'/../../config/set/all.php';
2222
public const COMMON = __DIR__.'/../../config/set/common.php';
23+
public const PHPBENCH = __DIR__.'/../../config/set/phpbench.php';
2324
public const PHPSTAN = __DIR__.'/../../config/set/phpstan.php';
2425
public const RECTOR = __DIR__.'/../../config/set/rector.php';
2526
}

0 commit comments

Comments
 (0)