-
Notifications
You must be signed in to change notification settings - Fork 2k
Add new Custom Rector Rule to Pass true to 3rd parameter of in_array when no value provided #3799
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
samsonasik
merged 4 commits into
codeigniter4:develop
from
samsonasik:add-rector-rule-to-pass-third-parameter-true-to-in-array
Oct 23, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a6c162f
Add new Custom Rector Rule to Pass true to 3rd parameter of in_array …
samsonasik bca6a54
Refactor to allow register more functions to specific position to be …
samsonasik 82e234e
get position
samsonasik 077bbb5
ignore new unrelated phpstan error due new release of phpstan
samsonasik 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
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
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
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
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
80 changes: 80 additions & 0 deletions
80
utils/Rector/PassStrictParameterToFunctionParameterRector.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,80 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Utils\Rector; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\ConstFetch; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Name; | ||
| use Rector\Core\Rector\AbstractRector; | ||
| use Rector\Core\RectorDefinition\CodeSample; | ||
| use Rector\Core\RectorDefinition\RectorDefinition; | ||
|
|
||
| /** | ||
| * Pass strict to function parameter on specific position argument when no value provided | ||
| */ | ||
| final class PassStrictParameterToFunctionParameterRector extends AbstractRector | ||
| { | ||
| private const FUNCTION_WITH_ARG_POSITION = [ | ||
| // position start from 0 | ||
| 'in_array' => 2, | ||
| ]; | ||
|
|
||
| public function getDefinition(): RectorDefinition | ||
| { | ||
| return new RectorDefinition('Pass strict to function parameter on specific position argument when no value provided', [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| in_array('a', $array); | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| in_array('a', $array, true); | ||
| CODE_SAMPLE | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [FuncCall::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param FuncCall $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| $name = $node->name; | ||
| if (! method_exists($name, 'toString')) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| $functions = array_keys(self::FUNCTION_WITH_ARG_POSITION); | ||
| $currentFunctionName = $name->toString(); | ||
|
|
||
| if (! in_array($currentFunctionName, $functions, true)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| $position = self::FUNCTION_WITH_ARG_POSITION[$currentFunctionName]; | ||
|
|
||
| if (isset($node->args[$position])) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| $name = new Name('true'); | ||
| $node->args[$position] = new Arg(new ConstFetch($name)); | ||
|
|
||
| return $node; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.