Skip to content

Commit 6d31b0c

Browse files
committed
feat: create rector to add names to boolean arguments
1 parent c03bb57 commit 6d31b0c

File tree

9 files changed

+328
-0
lines changed

9 files changed

+328
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddNameToBooleanArgumentRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return AddNameToBooleanArgumentRectorTest::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture;
4+
5+
use Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source\Service;
6+
7+
in_array($value, $array, true);
8+
9+
function (Service $service, string $value): void
10+
{
11+
$service->configure($value, true);
12+
Service::create($value, true);
13+
new Service($value, true);
14+
};
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture;
21+
22+
use Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source\Service;
23+
24+
in_array($value, $array, strict: true);
25+
26+
function (Service $service, string $value): void
27+
{
28+
$service->configure($value, strict: true);
29+
Service::create($value, strict: true);
30+
new Service($value, strict: true);
31+
};
32+
33+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture;
4+
5+
json_decode($json, true, 512, JSON_THROW_ON_ERROR);
6+
7+
?>
8+
-----
9+
<?php
10+
11+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture;
12+
13+
json_decode($json, associative: true, depth: 512, flags: JSON_THROW_ON_ERROR);
14+
15+
?>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture;
4+
5+
in_array($value, $array, strict: true);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture;
4+
5+
in_array(...);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture;
4+
5+
use Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source\Service;
6+
7+
function (array $rest): void
8+
{
9+
Service::create('value', true, ...$rest);
10+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source;
6+
7+
final class Service
8+
{
9+
public function __construct(string $value, bool $strict, ?string $fallback = null)
10+
{
11+
}
12+
13+
public function configure(string $value, bool $strict, ?string $fallback = null): void
14+
{
15+
}
16+
17+
public static function create(string $value, bool $strict, ?string $fallback = null): self
18+
{
19+
return new self($value, $strict, $fallback);
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return RectorConfig::configure()
9+
->withRules([AddNameToBooleanArgumentRector::class]);
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\CodeQuality\Rector\CallLike;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\CallLike;
10+
use PhpParser\Node\Identifier;
11+
use PHPStan\Reflection\FunctionReflection;
12+
use PHPStan\Reflection\MethodReflection;
13+
use PHPStan\Reflection\ParameterReflection;
14+
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
15+
use Rector\PhpParser\Node\Value\ValueResolver;
16+
use Rector\PHPStan\ScopeFetcher;
17+
use Rector\Rector\AbstractRector;
18+
use Rector\Reflection\ReflectionResolver;
19+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
22+
/**
23+
* @see \Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\AddNameToBooleanArgumentRectorTest
24+
*/
25+
final class AddNameToBooleanArgumentRector extends AbstractRector
26+
{
27+
public function __construct(
28+
private readonly ReflectionResolver $reflectionResolver,
29+
private readonly ValueResolver $valueResolver,
30+
) {
31+
}
32+
33+
public function getRuleDefinition(): RuleDefinition
34+
{
35+
return new RuleDefinition(
36+
'Add parameter names to boolean arguments.',
37+
[
38+
new CodeSample(
39+
<<<'CODE_SAMPLE'
40+
in_array($value, $array, true);
41+
CODE_SAMPLE
42+
,
43+
<<<'CODE_SAMPLE'
44+
in_array($value, $array, strict: true);
45+
CODE_SAMPLE
46+
),
47+
]
48+
);
49+
}
50+
51+
/**
52+
* @return array<class-string<Node>>
53+
*/
54+
public function getNodeTypes(): array
55+
{
56+
return [CallLike::class];
57+
}
58+
59+
/**
60+
* @param CallLike $node
61+
*/
62+
public function refactor(Node $node): ?Node
63+
{
64+
if ($this->shouldSkip($node)) {
65+
return null;
66+
}
67+
68+
$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
69+
if (! $reflection instanceof FunctionReflection && ! $reflection instanceof MethodReflection) {
70+
return null;
71+
}
72+
73+
$scope = ScopeFetcher::fetch($node);
74+
$args = $node->getArgs();
75+
$parameters = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $node, $scope)
76+
->getParameters();
77+
78+
$position = $this->resolveFirstPositionToName($args, $parameters);
79+
if ($position === null) {
80+
return null;
81+
}
82+
83+
$wasChanged = false;
84+
for ($i = $position; $i < count($args); ++$i) {
85+
$arg = $args[$i];
86+
if ($arg->name instanceof Identifier) {
87+
continue;
88+
}
89+
90+
$parameterReflection = $this->resolveParameterReflection($arg, $i, $parameters);
91+
if (! $parameterReflection instanceof ParameterReflection) {
92+
return null;
93+
}
94+
95+
$arg->name = new Identifier($parameterReflection->getName());
96+
$wasChanged = true;
97+
}
98+
99+
if (! $wasChanged) {
100+
return null;
101+
}
102+
103+
return $node;
104+
}
105+
106+
private function shouldSkip(CallLike $callLike): bool
107+
{
108+
if ($callLike->isFirstClassCallable()) {
109+
return true;
110+
}
111+
112+
$args = $callLike->getArgs();
113+
if ($args === []) {
114+
return true;
115+
}
116+
117+
foreach ($args as $arg) {
118+
if ($arg->unpack) {
119+
return true;
120+
}
121+
}
122+
123+
return false;
124+
}
125+
126+
/**
127+
* @param Arg[] $args
128+
* @param ParameterReflection[] $parameters
129+
*/
130+
private function resolveFirstPositionToName(array $args, array $parameters): ?int
131+
{
132+
foreach ($args as $position => $arg) {
133+
if ($arg->name instanceof Identifier) {
134+
continue;
135+
}
136+
137+
if (! $this->valueResolver->isTrueOrFalse($arg->value)) {
138+
continue;
139+
}
140+
141+
if ($this->canNameArgsFromPosition($args, $parameters, $position)) {
142+
return $position;
143+
}
144+
}
145+
146+
return null;
147+
}
148+
149+
/**
150+
* @param Arg[] $args
151+
* @param ParameterReflection[] $parameters
152+
*/
153+
private function canNameArgsFromPosition(array $args, array $parameters, int $position): bool
154+
{
155+
$count = count($args);
156+
for ($i = $position; $i < $count; ++$i) {
157+
$arg = $args[$i];
158+
if ($arg->name instanceof Identifier) {
159+
continue;
160+
}
161+
162+
$parameterReflection = $this->resolveParameterReflection($arg, $i, $parameters);
163+
if (! $parameterReflection instanceof ParameterReflection) {
164+
return false;
165+
}
166+
167+
if ($parameterReflection->isVariadic()) {
168+
return false;
169+
}
170+
}
171+
172+
return true;
173+
}
174+
175+
/**
176+
* @param ParameterReflection[] $parameters
177+
*/
178+
private function resolveParameterReflection(Arg $arg, int $position, array $parameters): ?ParameterReflection
179+
{
180+
if ($arg->name instanceof Identifier) {
181+
foreach ($parameters as $parameter) {
182+
if ($parameter->getName() === $arg->name->toString()) {
183+
return $parameter;
184+
}
185+
}
186+
187+
return null;
188+
}
189+
190+
$parameter = $parameters[$position] ?? null;
191+
if ($parameter instanceof ParameterReflection) {
192+
return $parameter;
193+
}
194+
195+
$lastParameter = end($parameters);
196+
if ($lastParameter instanceof ParameterReflection && $lastParameter->isVariadic()) {
197+
return $lastParameter;
198+
}
199+
200+
return null;
201+
}
202+
}

0 commit comments

Comments
 (0)