-
-
Notifications
You must be signed in to change notification settings - Fork 364
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
[Downgrade PHP 7.0] Add DowngradeClosureCallRector #1304
Merged
samsonasik
merged 4 commits into
rectorphp:main
from
villfa:feat/downgrade-closure-call
Nov 25, 2021
Merged
Changes from 3 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
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
75 changes: 75 additions & 0 deletions
75
packages/NodeTypeResolver/TypeAnalyzer/MethodTypeAnalyzer.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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\TypeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\TypeWithClassName; | ||
use Rector\NodeTypeResolver\NodeTypeResolver; | ||
|
||
final class MethodTypeAnalyzer | ||
{ | ||
public function __construct( | ||
private NodeTypeResolver $nodeTypeResolver | ||
) { | ||
} | ||
|
||
/** | ||
* @param class-string $expectedClass | ||
* @param non-empty-string $expectedMethod | ||
*/ | ||
public function isCallTo(MethodCall $methodCall, string $expectedClass, string $expectedMethod): bool | ||
{ | ||
if (! $this->isMethodName($methodCall, $expectedMethod)) { | ||
return false; | ||
} | ||
|
||
return $this->isInstanceOf($methodCall->var, $expectedClass); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $expectedName | ||
*/ | ||
private function isMethodName(MethodCall $methodCall, string $expectedName): bool | ||
{ | ||
if ($methodCall->name instanceof Identifier) { | ||
$comparison = strcasecmp($methodCall->name->toString(), $expectedName); | ||
if ($comparison === 0) { | ||
return true; | ||
} | ||
} | ||
|
||
$type = $this->nodeTypeResolver->getType($methodCall->name); | ||
|
||
if ($type instanceof ConstantStringType) { | ||
$comparison = strcasecmp($type->getValue(), $expectedName); | ||
if ($comparison === 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param class-string $expectedClass | ||
*/ | ||
private function isInstanceOf(Expr $expr, string $expectedClass): bool | ||
{ | ||
$type = $this->nodeTypeResolver->getType($expr); | ||
if (! $type instanceof TypeWithClassName) { | ||
return false; | ||
} | ||
|
||
$comparison = strcasecmp($expectedClass, $type->getClassName()); | ||
if ($comparison === 0) { | ||
return true; | ||
} | ||
|
||
return $type->getAncestorWithClassName($expectedClass) !== null; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...radePhp70/Rector/MethodCall/DowngradeClosureCallRector/DowngradeClosureCallRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradeClosureCallRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...gradePhp70/Rector/MethodCall/DowngradeClosureCallRector/Fixture/closure_call_expr.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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector\Fixture; | ||
|
||
class Foo | ||
{ | ||
public $value = 'foo'; | ||
} | ||
|
||
$closure = function () { | ||
echo $this->value, PHP_EOL; | ||
}; | ||
$args = []; | ||
|
||
$closure->call(new Foo(), ...$args); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector\Fixture; | ||
|
||
class Foo | ||
{ | ||
public $value = 'foo'; | ||
} | ||
|
||
$closure = function () { | ||
echo $this->value, PHP_EOL; | ||
}; | ||
$args = []; | ||
|
||
call_user_func($closure->bindTo(...array_fill(0, 2, new Foo())), ...$args); | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
...ePhp70/Rector/MethodCall/DowngradeClosureCallRector/Fixture/closure_call_variable.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,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector\Fixture; | ||
|
||
class Foo | ||
{ | ||
public $value = 'foo'; | ||
} | ||
|
||
$foo = new Foo(); | ||
$closure = function () { | ||
echo $this->value, PHP_EOL; | ||
}; | ||
$args = []; | ||
|
||
$closure->call($foo, ...$args); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector\Fixture; | ||
|
||
class Foo | ||
{ | ||
public $value = 'foo'; | ||
} | ||
|
||
$foo = new Foo(); | ||
$closure = function () { | ||
echo $this->value, PHP_EOL; | ||
}; | ||
$args = []; | ||
|
||
call_user_func($closure->bindTo($foo, $foo), ...$args); | ||
|
||
?> |
16 changes: 16 additions & 0 deletions
16
...ngradePhp70/Rector/MethodCall/DowngradeClosureCallRector/Fixture/skip_not_closure.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector\Fixture; | ||
|
||
class Foo | ||
{ | ||
public function call($obj) | ||
{ | ||
var_dump($obj); | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
$foo->call($bar); | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...ts/DowngradePhp70/Rector/MethodCall/DowngradeClosureCallRector/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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradeClosureCallRector::class); | ||
}; |
97 changes: 97 additions & 0 deletions
97
rules/DowngradePhp70/Rector/MethodCall/DowngradeClosureCallRector.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,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp70\Rector\MethodCall; | ||
|
||
use Closure; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\NodeTypeResolver\TypeAnalyzer\MethodTypeAnalyzer; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://wiki.php.net/rfc/closure_apply | ||
* | ||
* @see \Rector\Tests\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector\DowngradeClosureCallRectorTest | ||
*/ | ||
final class DowngradeClosureCallRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private MethodTypeAnalyzer $methodTypeAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Replace Closure::call() by Closure::bindTo()', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$closure->call($newObj, ...$args); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
call_user_func($closure->bindTo($newObj, $newObj), ...$args); | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?FuncCall | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
$methodCall = $this->createBindToCall($node); | ||
$args = [new Arg($methodCall), ...array_slice($node->args, 1)]; | ||
|
||
return new FuncCall(new Name('call_user_func'), $args); | ||
} | ||
|
||
private function shouldSkip(MethodCall $methodCall): bool | ||
{ | ||
if ($methodCall->args === []) { | ||
return true; | ||
} | ||
|
||
return ! $this->methodTypeAnalyzer->isCallTo($methodCall, Closure::class, 'call'); | ||
} | ||
|
||
private function createBindToCall(MethodCall $methodCall): MethodCall | ||
{ | ||
$newObj = $methodCall->args[0]; | ||
if ($newObj->value instanceof Variable) { | ||
$args = [$newObj, $newObj]; | ||
} else { | ||
// we don't want the expression to be executed twice so we use array_fill() as a trick | ||
$args = [new Arg(new LNumber(0)), new Arg(new LNumber(2)), $newObj]; | ||
$funcCall = new FuncCall(new Name('array_fill'), $args); | ||
$args = [new Arg($funcCall, false, true)]; | ||
} | ||
|
||
return new MethodCall($methodCall->var, 'bindTo', $args); | ||
} | ||
} |
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 may be needed to be moved to separate private method to be reusable so can change in one place when there is logic change for comparison.
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.
Done