-
-
Notifications
You must be signed in to change notification settings - Fork 362
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
[CodeQuality] FuncLikeToFirstClassCallableRector #6667
Open
peterfox
wants to merge
7
commits into
rectorphp:main
Choose a base branch
from
peterfox:feature/funclike-to-firstclasscallable
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.
+375
−7
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92e4a49
Working rule
peterfox e552c88
fix scenario
peterfox b3dc82a
Added to Code Quality Set
peterfox 00c003f
Rule applied to codebase
peterfox c161aa0
small test addition
peterfox d3f4fdc
another quick test
peterfox 4880006
Fixes edge case
peterfox 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
48 changes: 48 additions & 0 deletions
48
...odeQuality/Rector/FunctionLike/FuncLikeToFirstClassCallableRector/Fixture/fixture.php.inc
This file contains 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,48 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
function ($foo) | ||
{ | ||
return FooBar::foo($foo); | ||
}; | ||
|
||
fn ($foo) => FooBar::foo($foo); | ||
|
||
$bar = null; | ||
|
||
fn ($foo) => $bar->foo($foo); | ||
|
||
function ($foo) use ($bar) | ||
{ | ||
return $bar->foo($foo); | ||
}; | ||
|
||
function ($foo, $bar, $ray) | ||
{ | ||
return FooBar::foo($foo, $bar); | ||
}; | ||
|
||
fn ($foo, $bar, $ray) => FooBar::foo($foo); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
FooBar::foo(...); | ||
|
||
FooBar::foo(...); | ||
|
||
$bar = null; | ||
|
||
$bar->foo(...); | ||
|
||
$bar->foo(...); | ||
|
||
FooBar::foo(...); | ||
|
||
FooBar::foo(...); | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
...nctionLike/FuncLikeToFirstClassCallableRector/Fixture/skip_call_dependency_on_arg.php.inc
This file contains 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,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
function ($foo) | ||
{ | ||
return $foo->foo(); | ||
}; | ||
|
||
function ($foo) | ||
{ | ||
return (new $foo)->foo(); | ||
}; | ||
|
||
fn ($foo) => $foo->foo(); | ||
|
||
function ($foo) | ||
{ | ||
return $foo::foo(); | ||
}; | ||
|
||
function ($foo) | ||
{ | ||
return ($foo . '\\Foo')::foo(); | ||
}; | ||
|
||
?> |
17 changes: 17 additions & 0 deletions
17
...tionLike/FuncLikeToFirstClassCallableRector/Fixture/skip_functions_with_no_return.php.inc
This file contains 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,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
function ($foo) | ||
{ | ||
FooBar::foo($foo); | ||
}; | ||
|
||
$bar = null; | ||
|
||
function ($foo) use ($bar) | ||
{ | ||
$bar->foo($foo); | ||
}; | ||
|
||
?> |
21 changes: 21 additions & 0 deletions
21
...uncLikeToFirstClassCallableRector/Fixture/skip_functions_with_too_many_statements.php.inc
This file contains 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 | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
function ($foo) | ||
{ | ||
$foo = 1; | ||
return FooBar::foo($foo); | ||
}; | ||
|
||
fn ($foo) => FooBar::foo($foo) && true; | ||
|
||
$bar = null; | ||
|
||
function ($foo) use ($bar) | ||
{ | ||
$foo = 1; | ||
return $bar->foo($foo); | ||
}; | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...ctionLike/FuncLikeToFirstClassCallableRector/Fixture/skip_functions_without_calls.php.inc
This file contains 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,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
function ($foo) | ||
{ | ||
return $foo; | ||
}; | ||
|
||
fn ($foo) => $foo; | ||
|
||
?> |
21 changes: 21 additions & 0 deletions
21
...onLike/FuncLikeToFirstClassCallableRector/Fixture/skip_mismatched_args_and_params.php.inc
This file contains 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 | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
function ($foo, $bar) | ||
{ | ||
return FooBar::foo($bar, $foo); | ||
}; | ||
|
||
fn ($foo, $bar) => FooBar::foo($bar, $foo); | ||
|
||
$bar = null; | ||
|
||
fn ($foo, $bar) => $bar->foo($bar, $foo); | ||
|
||
function ($foo, $barFoo) use ($bar) | ||
{ | ||
return $bar->foo($barFoo, $foo); | ||
}; | ||
|
||
?> |
22 changes: 22 additions & 0 deletions
22
...nctionLike/FuncLikeToFirstClassCallableRector/Fixture/supports_varadic_and_unpack.php.inc
This file contains 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,22 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
function (...$foo) | ||
{ | ||
return FooBar::foo(...$foo); | ||
}; | ||
|
||
fn (...$foo) => FooBar::foo(...$foo); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\Fixture; | ||
|
||
FooBar::foo(...); | ||
|
||
FooBar::foo(...); | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...unctionLike/FuncLikeToFirstClassCallableRector/FuncLikeToFirstClassCallableRectorTest.php
This file contains 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\FunctionLike\FuncLikeToFirstClassCallableRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class FuncLikeToFirstClassCallableRectorTest 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'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...Quality/Rector/FunctionLike/FuncLikeToFirstClassCallableRector/config/configured_rule.php
This file contains 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\FunctionLike\FuncLikeToFirstClassCallableRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return RectorConfig::configure() | ||
->withRules([FuncLikeToFirstClassCallableRector::class]); |
166 changes: 166 additions & 0 deletions
166
rules/CodeQuality/Rector/FunctionLike/FuncLikeToFirstClassCallableRector.php
This file contains 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,166 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\FunctionLike; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\ArrowFunction; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PhpParser\Node\VariadicPlaceholder; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector\FuncLikeToFirstClassCallableRectorTest | ||
*/ | ||
final class FuncLikeToFirstClassCallableRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'converts function like to first class callable', | ||
[New CodeSample( | ||
<<<'CODE_SAMPLE' | ||
function ($parameter) { return Call::to($parameter); } | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
Call::to(...); | ||
CODE_SAMPLE | ||
, | ||
)] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ArrowFunction::class, Closure::class]; | ||
} | ||
|
||
/** | ||
* @param ArrowFunction|Closure $node | ||
*/ | ||
public function refactor(Node $node): null|StaticCall|MethodCall | ||
{ | ||
$extractedMethodCall = $this->extractMethodCallFromFuncLike($node); | ||
|
||
if (! $extractedMethodCall instanceof MethodCall && ! $extractedMethodCall instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if ($extractedMethodCall instanceof MethodCall) { | ||
return new MethodCall($extractedMethodCall->var, $extractedMethodCall->name, [new VariadicPlaceholder()]); | ||
} | ||
|
||
return new StaticCall($extractedMethodCall->class, $extractedMethodCall->name, [new VariadicPlaceholder()]); | ||
} | ||
|
||
private function extractMethodCallFromFuncLike(Closure|ArrowFunction $node): MethodCall|StaticCall|null | ||
{ | ||
if ($node instanceof ArrowFunction) { | ||
if ( | ||
($node->expr instanceof MethodCall || $node->expr instanceof StaticCall) && | ||
! $node->expr->isFirstClassCallable() && | ||
$this->sameParamsForArgs($node->getParams(), $node->expr->getArgs()) && | ||
$this->isNonDependantMethod($node->expr, $node->getParams()) | ||
) { | ||
return $node->expr; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
if (count($node->stmts) != 1 || ! $node->getStmts()[0] instanceof Return_) { | ||
return null; | ||
} | ||
|
||
$callLike = $node->getStmts()[0] | ||
->expr; | ||
|
||
if (! $callLike instanceof MethodCall && ! $callLike instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if ( | ||
! $callLike->isFirstClassCallable() && | ||
$this->sameParamsForArgs($node->getParams(), $callLike->getArgs()) && | ||
$this->isNonDependantMethod($callLike, $node->getParams())) { | ||
return $callLike; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param Node\Param[] $params | ||
* @param Node\Arg[] $args | ||
*/ | ||
private function sameParamsForArgs(array $params, array $args): bool | ||
{ | ||
Assert::allIsInstanceOf($args, Arg::class); | ||
Assert::allIsInstanceOf($params, Param::class); | ||
|
||
if (count($args) > count($params)) { | ||
return false; | ||
} | ||
|
||
if (count($args) === 1 && $args[0]->unpack) { | ||
return $params[0]->variadic; | ||
} | ||
|
||
foreach ($args as $key => $arg) { | ||
if (! $this->nodeComparator->areNodesEqual($arg->value, $params[$key]->var)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Makes sure the parameter isn't used to make the call e.g. in the var or class | ||
* | ||
* @param Param[] $params | ||
*/ | ||
private function isNonDependantMethod(StaticCall|MethodCall $expr, array $params): bool | ||
{ | ||
Assert::allIsInstanceOf($params, Param::class); | ||
|
||
$found = false; | ||
|
||
foreach ($params as $param) { | ||
if ($expr instanceof MethodCall) { | ||
$this->traverseNodesWithCallable($expr->var, function (Node $node) use ($param, &$found): null { | ||
if ($this->nodeComparator->areNodesEqual($node, $param->var)) { | ||
$found = true; | ||
} | ||
|
||
return null; | ||
}); | ||
} | ||
if ($expr instanceof StaticCall) { | ||
$this->traverseNodesWithCallable($expr->class, function (Node $node) use ($param, &$found): null { | ||
if ($this->nodeComparator->areNodesEqual($node, $param->var)) { | ||
$found = true; | ||
} | ||
|
||
return null; | ||
}); | ||
} | ||
|
||
if ($found) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector; | ||
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector; | ||
use Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector; | ||
use Rector\CodeQuality\Rector\FunctionLike\FuncLikeToFirstClassCallableRector; | ||
use Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector; | ||
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector; | ||
use Rector\CodeQuality\Rector\Identical\SimplifyArraySearchRector; | ||
|
@@ -175,6 +176,7 @@ final class CodeQualityLevel | |
CompleteMissingIfElseBracketRector::class, | ||
RemoveUselessIsObjectCheckRector::class, | ||
StaticToSelfStaticMethodCallOnFinalClassRector::class, | ||
FuncLikeToFirstClassCallableRector::class, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same like it should not part of any set, as imo, it is by preference. |
||
]; | ||
|
||
/** | ||
|
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.
is named argument need to be skipped as well ?