-
Notifications
You must be signed in to change notification settings - Fork 112
[code-quality] Add ParameterBagTypedGetMethodCallRector #834
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f8b2568
[deprecation] remove deprecated ContainerGetToConstructorInjectionRector
TomasVotruba cbe3e05
[deprecation] remove deprecated FOSRestSetList
TomasVotruba 2216b24
[deprecation] remove deprecated JMS and Sensiolabs set lists, use ->w…
TomasVotruba 3cc018d
[code-quality] Add ParameterBagTypedGetMethodCallRector
TomasVotruba 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
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
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
31 changes: 31 additions & 0 deletions
31
...ity/Rector/MethodCall/ParameterBagTypedGetMethodCallRector/Fixture/get_on_boolean.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,31 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| final class GetOnBoolean | ||
| { | ||
| public function run(Request $request) | ||
| { | ||
| $debug = (bool) $request->query->get('debug', false); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| final class GetOnBoolean | ||
| { | ||
| public function run(Request $request) | ||
| { | ||
| $debug = $request->query->getBoolean('debug'); | ||
| } | ||
| } | ||
|
|
||
| ?> |
13 changes: 13 additions & 0 deletions
13
...ty/Rector/MethodCall/ParameterBagTypedGetMethodCallRector/Fixture/skip_non_casted.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,13 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| final class SkipNonCasted | ||
| { | ||
| public function run(Request $request) | ||
| { | ||
| $debug = $request->query->get('debug', false); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...hodCall/ParameterBagTypedGetMethodCallRector/ParameterBagTypedGetMethodCallRectorTest.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\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class ParameterBagTypedGetMethodCallRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...Quality/Rector/MethodCall/ParameterBagTypedGetMethodCallRector/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,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Symfony\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(ParameterBagTypedGetMethodCallRector::class); | ||
| }; |
125 changes: 125 additions & 0 deletions
125
rules/CodeQuality/Rector/MethodCall/ParameterBagTypedGetMethodCallRector.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,125 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Symfony\CodeQuality\Rector\MethodCall; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Cast\Bool_; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Type\ObjectType; | ||
| use Rector\PhpParser\Node\Value\ValueResolver; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\Symfony\Enum\SymfonyClass; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\ParameterBagTypedGetMethodCallRectorTest | ||
| */ | ||
| final class ParameterBagTypedGetMethodCallRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly ValueResolver $valueResolver | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Make use of specific ParameterBag::get*() method with native return type declaration', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| class SomeClass | ||
| { | ||
| public function run(Request $request) | ||
| { | ||
| $debug = (bool) $request->query->get('debug', false); | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
|
|
||
| , | ||
| <<<'CODE_SAMPLE' | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| class SomeClass | ||
| { | ||
| public function run(Request $request) | ||
| { | ||
| $debug = $request->query->getBoolean('debug'); | ||
| } | ||
| } | ||
|
|
||
| CODE_SAMPLE | ||
| ), | ||
|
|
||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [MethodCall::class, Bool_::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param MethodCall|Bool_ $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if ($node instanceof Bool_) { | ||
| if (! $node->expr instanceof MethodCall) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->refactorMethodCall($node->expr); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function refactorMethodCall(MethodCall $methodCall): ?MethodCall | ||
| { | ||
| // default value must be defined | ||
| if (count($methodCall->getArgs()) !== 2) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $this->isName($methodCall->name, 'get')) { | ||
| return null; | ||
| } | ||
|
|
||
| $callerType = $this->getType($methodCall->var); | ||
| if (! $callerType instanceof ObjectType) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $callerType->isInstanceOf(SymfonyClass::PARAMETER_BAG)->yes()) { | ||
| return null; | ||
| } | ||
|
|
||
| // the getBoolean() method must exist | ||
| if (! $callerType->hasMethod('getBoolean')->yes()) { | ||
| return null; | ||
| } | ||
|
|
||
| $defaultArg = $methodCall->getArgs()[1]; | ||
|
|
||
| if ($this->valueResolver->isFalse($defaultArg->value)) { | ||
| unset($methodCall->args[1]); | ||
| $methodCall->name = new Identifier('getBoolean'); | ||
|
|
||
| return $methodCall; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
79 changes: 0 additions & 79 deletions
79
rules/Symfony42/Rector/MethodCall/ContainerGetToConstructorInjectionRector.php
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
first class callable need to be skipped early
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.
#835