-
-
Notifications
You must be signed in to change notification settings - Fork 440
feat: create rector to add names to boolean arguments #7944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
calebdw
wants to merge
1
commit into
rectorphp:main
Choose a base branch
from
calebdw:calebdw/push-muvroqtpsztq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...ity/Rector/CallLike/AddNameToBooleanArgumentRector/AddNameToBooleanArgumentRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddNameToBooleanArgumentRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return AddNameToBooleanArgumentRectorTest::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
...-tests/CodeQuality/Rector/CallLike/AddNameToBooleanArgumentRector/Fixture/fixture.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source\Service; | ||
|
|
||
| in_array($value, $array, true); | ||
|
|
||
| function (Service $service, string $value): void | ||
| { | ||
| $service->configure($value, true); | ||
| Service::create($value, true); | ||
| new Service($value, true); | ||
| }; | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source\Service; | ||
|
|
||
| in_array($value, $array, strict: true); | ||
|
|
||
| function (Service $service, string $value): void | ||
| { | ||
| $service->configure($value, strict: true); | ||
| Service::create($value, strict: true); | ||
| new Service($value, strict: true); | ||
| }; | ||
|
|
||
| ?> |
15 changes: 15 additions & 0 deletions
15
...y/Rector/CallLike/AddNameToBooleanArgumentRector/Fixture/name_following_arguments.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture; | ||
|
|
||
| json_decode($json, true, 512, JSON_THROW_ON_ERROR); | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture; | ||
|
|
||
| json_decode($json, associative: true, depth: 512, flags: JSON_THROW_ON_ERROR); | ||
|
|
||
| ?> |
5 changes: 5 additions & 0 deletions
5
...Quality/Rector/CallLike/AddNameToBooleanArgumentRector/Fixture/skip_already_named.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture; | ||
|
|
||
| in_array($value, $array, strict: true); |
5 changes: 5 additions & 0 deletions
5
.../Rector/CallLike/AddNameToBooleanArgumentRector/Fixture/skip_first_class_callable.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture; | ||
|
|
||
| in_array(...); |
10 changes: 10 additions & 0 deletions
10
...ts/CodeQuality/Rector/CallLike/AddNameToBooleanArgumentRector/Fixture/skip_unpack.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source\Service; | ||
|
|
||
| function (array $rest): void | ||
| { | ||
| Service::create('value', true, ...$rest); | ||
| }; |
21 changes: 21 additions & 0 deletions
21
rules-tests/CodeQuality/Rector/CallLike/AddNameToBooleanArgumentRector/Source/Service.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\Source; | ||
|
|
||
| final class Service | ||
| { | ||
| public function __construct(string $value, bool $strict, ?string $fallback = null) | ||
| { | ||
| } | ||
|
|
||
| public function configure(string $value, bool $strict, ?string $fallback = null): void | ||
| { | ||
| } | ||
|
|
||
| public static function create(string $value, bool $strict, ?string $fallback = null): self | ||
| { | ||
| return new self($value, $strict, $fallback); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...sts/CodeQuality/Rector/CallLike/AddNameToBooleanArgumentRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector; | ||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([AddNameToBooleanArgumentRector::class]); |
202 changes: 202 additions & 0 deletions
202
rules/CodeQuality/Rector/CallLike/AddNameToBooleanArgumentRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\CodeQuality\Rector\CallLike; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\CallLike; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Reflection\ParameterReflection; | ||
| use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; | ||
| use Rector\PhpParser\Node\Value\ValueResolver; | ||
| use Rector\PHPStan\ScopeFetcher; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\Reflection\ReflectionResolver; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector\AddNameToBooleanArgumentRectorTest | ||
| */ | ||
| final class AddNameToBooleanArgumentRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly ReflectionResolver $reflectionResolver, | ||
| private readonly ValueResolver $valueResolver, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Add parameter names to boolean arguments.', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| in_array($value, $array, true); | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| in_array($value, $array, strict: true); | ||
| CODE_SAMPLE | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [CallLike::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param CallLike $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if ($this->shouldSkip($node)) { | ||
| return null; | ||
| } | ||
|
|
||
| $reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); | ||
| if (! $reflection instanceof FunctionReflection && ! $reflection instanceof MethodReflection) { | ||
| return null; | ||
| } | ||
|
|
||
| $scope = ScopeFetcher::fetch($node); | ||
| $args = $node->getArgs(); | ||
| $parameters = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $node, $scope) | ||
| ->getParameters(); | ||
|
|
||
| $position = $this->resolveFirstPositionToName($args, $parameters); | ||
| if ($position === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $wasChanged = false; | ||
| for ($i = $position; $i < count($args); ++$i) { | ||
| $arg = $args[$i]; | ||
| if ($arg->name instanceof Identifier) { | ||
| continue; | ||
| } | ||
|
|
||
| $parameterReflection = $this->resolveParameterReflection($arg, $i, $parameters); | ||
| if (! $parameterReflection instanceof ParameterReflection) { | ||
| return null; | ||
| } | ||
|
|
||
| $arg->name = new Identifier($parameterReflection->getName()); | ||
| $wasChanged = true; | ||
| } | ||
|
|
||
| if (! $wasChanged) { | ||
| return null; | ||
| } | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| private function shouldSkip(CallLike $callLike): bool | ||
| { | ||
| if ($callLike->isFirstClassCallable()) { | ||
| return true; | ||
| } | ||
|
|
||
| $args = $callLike->getArgs(); | ||
| if ($args === []) { | ||
| return true; | ||
| } | ||
|
|
||
| foreach ($args as $arg) { | ||
| if ($arg->unpack) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param Arg[] $args | ||
| * @param ParameterReflection[] $parameters | ||
| */ | ||
| private function resolveFirstPositionToName(array $args, array $parameters): ?int | ||
| { | ||
| foreach ($args as $position => $arg) { | ||
| if ($arg->name instanceof Identifier) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! $this->valueResolver->isTrueOrFalse($arg->value)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($this->canNameArgsFromPosition($args, $parameters, $position)) { | ||
| return $position; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @param Arg[] $args | ||
| * @param ParameterReflection[] $parameters | ||
| */ | ||
| private function canNameArgsFromPosition(array $args, array $parameters, int $position): bool | ||
| { | ||
| $count = count($args); | ||
| for ($i = $position; $i < $count; ++$i) { | ||
| $arg = $args[$i]; | ||
| if ($arg->name instanceof Identifier) { | ||
| continue; | ||
| } | ||
|
|
||
| $parameterReflection = $this->resolveParameterReflection($arg, $i, $parameters); | ||
| if (! $parameterReflection instanceof ParameterReflection) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($parameterReflection->isVariadic()) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param ParameterReflection[] $parameters | ||
| */ | ||
| private function resolveParameterReflection(Arg $arg, int $position, array $parameters): ?ParameterReflection | ||
| { | ||
| if ($arg->name instanceof Identifier) { | ||
| foreach ($parameters as $parameter) { | ||
| if ($parameter->getName() === $arg->name->toString()) { | ||
| return $parameter; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| $parameter = $parameters[$position] ?? null; | ||
| if ($parameter instanceof ParameterReflection) { | ||
| return $parameter; | ||
| } | ||
|
|
||
| $lastParameter = end($parameters); | ||
| if ($lastParameter instanceof ParameterReflection && $lastParameter->isVariadic()) { | ||
| return $lastParameter; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should implements
\Rector\VersionBonding\Contract\MinPhpVersionInterfaceand onprovideMinPhpVersion(), returns\Rector\ValueObject\PhpVersion::PHP_80