-
-
Notifications
You must be signed in to change notification settings - Fork 424
Static closure rule ignores arguments with $this binding #7721
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
peterfox
wants to merge
14
commits into
rectorphp:main
Choose a base branch
from
peterfox:feature/static-closure-improved
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
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3cefafa
working
peterfox ae4853f
fixes
peterfox 6394422
fixes
peterfox f8d993f
Fix PHPStan issues
peterfox a1789e8
wip
peterfox 91e3be0
fixes
peterfox b11216c
final class
peterfox 6b6ae00
fixes
peterfox 7a05a93
Additional param check
peterfox 8c6be32
Merge remote-tracking branch 'origin/main' into feature/static-closur…
peterfox 64e1b4a
Merge remote-tracking branch 'origin/main' into feature/static-closur…
peterfox fc270ae
Merge remote-tracking branch 'origin/main' into feature/static-closur…
peterfox bec4f46
feedback changes
peterfox 7ccd97c
fix
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
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
41 changes: 41 additions & 0 deletions
41
...gStyle/Rector/Closure/StaticClosureRector/Fixture/fixture_with_named_this_binding.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,41 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
|
||
| use Closure; | ||
| use Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\BindObject; | ||
|
|
||
| class FixtureWithNamedThisBinding | ||
| { | ||
| public function first() | ||
| { | ||
| return BindObject::call(closure: function () { | ||
| return 'bar'; | ||
| }, closureTwo: function () { | ||
| return 'foo'; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
|
||
| use Closure; | ||
| use Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\BindObject; | ||
|
|
||
| class FixtureWithNamedThisBinding | ||
| { | ||
| public function first() | ||
| { | ||
| return BindObject::call(closure: function () { | ||
| return 'bar'; | ||
| }, closureTwo: static function () { | ||
| return 'foo'; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| ?> |
22 changes: 22 additions & 0 deletions
22
...ingStyle/Rector/Closure/StaticClosureRector/Fixture/skip_use_with_context_binding.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,22 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\BindObject; | ||
| use function Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\bind_on_object; | ||
|
|
||
| BindObject::call(closure: function () { | ||
| echo 'static call'; | ||
| }); | ||
|
|
||
| (new BindObject())->callOnObject(function () { | ||
| echo 'method call'; | ||
| }); | ||
|
|
||
| bind_on_object(function () { | ||
| echo 'closure func call 0'; | ||
| }, null, function () { | ||
| echo 'closure func call 0'; | ||
| }); | ||
|
|
||
| ?> |
22 changes: 22 additions & 0 deletions
22
rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Source/BindObject.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,22 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source; | ||
|
|
||
| class BindObject | ||
| { | ||
| /** | ||
| * @param-closure-this \Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\BindObject $closure | ||
| */ | ||
| public static function call(\Closure $closure, \Closure|null $closureTwo = null) | ||
| { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @param-closure-this \Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\BindObject $closure | ||
| */ | ||
| public function callOnObject(\Closure $closure) | ||
| { | ||
|
|
||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Source/functions.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,14 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source; | ||
|
|
||
| use Closure; | ||
|
|
||
| /** | ||
| * @param-closure-this \Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\BindObject $closure | ||
| * @param-closure-this \Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Source\BindObject $closureFinally | ||
| */ | ||
| function bind_on_object(Closure|null $closure = null, Closure|null $anotherClosure = null, Closure|null $closureFinally = null) | ||
| { | ||
|
|
||
| } | ||
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
91 changes: 91 additions & 0 deletions
91
src/NodeAnalyzer/CallLikeExpectsThisBoundClosureArgsAnalyzer.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,91 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\NodeAnalyzer; | ||
|
|
||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\CallLike; | ||
| use PhpParser\Node\Expr\Closure; | ||
| use PHPStan\Reflection\ExtendedParameterReflection; | ||
| use Rector\NodeTypeResolver\Node\AttributeKey; | ||
| use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; | ||
| use Rector\Reflection\ReflectionResolver; | ||
|
|
||
| final readonly class CallLikeExpectsThisBoundClosureArgsAnalyzer | ||
| { | ||
| public function __construct( | ||
| private ReflectionResolver $reflectionResolver | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return Arg[] | ||
| */ | ||
| public function getArgsUsingThisBoundClosure(CallLike $callLike): array | ||
| { | ||
| if ($callLike->isFirstClassCallable() || $callLike->getArgs() === []) { | ||
| return []; | ||
| } | ||
|
|
||
| $callArgs = $callLike->getArgs(); | ||
| $hasClosureArg = (bool) array_filter($callArgs, fn (Arg $arg): bool => $arg->value instanceof Closure); | ||
|
|
||
| if (! $hasClosureArg) { | ||
| return []; | ||
| } | ||
|
|
||
| $argsUsingThisBoundClosure = []; | ||
|
|
||
| $reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike); | ||
|
|
||
| if ($reflection === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $scope = $callLike->getAttribute(AttributeKey::SCOPE); | ||
|
|
||
| if ($scope === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $callLike, $scope); | ||
| $parameters = $parametersAcceptor->getParameters(); | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| foreach ($callArgs as $index => $arg) { | ||
| if (! $arg->value instanceof Closure) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($arg->name?->name !== null) { | ||
| foreach ($parameters as $parameter) { | ||
| if (! $parameter instanceof ExtendedParameterReflection) { | ||
| continue; | ||
| } | ||
|
|
||
| $hasObjectBinding = (bool) $parameter->getClosureThisType(); | ||
| if ($hasObjectBinding && $arg->name->name === $parameter->getName()) { | ||
| $argsUsingThisBoundClosure[] = $arg; | ||
| } | ||
| } | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (! is_string($arg->name?->name)) { | ||
| $parameter = $parameters[$index] ?? null; | ||
|
|
||
| if (! $parameter instanceof ExtendedParameterReflection) { | ||
| continue; | ||
| } | ||
|
|
||
| $hasObjectBinding = (bool) $parameter->getClosureThisType(); | ||
| if ($hasObjectBinding) { | ||
| $argsUsingThisBoundClosure[] = $arg; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $argsUsingThisBoundClosure; | ||
| } | ||
| } | ||
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
52 changes: 52 additions & 0 deletions
52
src/PhpParser/NodeVisitor/CallLikeThisBoundClosureArgsNodeVisitor.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,52 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\PhpParser\NodeVisitor; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Closure; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PhpParser\NodeVisitorAbstract; | ||
| use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface; | ||
| use Rector\NodeAnalyzer\CallLikeExpectsThisBoundClosureArgsAnalyzer; | ||
| use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
|
||
| final class CallLikeThisBoundClosureArgsNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface | ||
| { | ||
| public function __construct( | ||
| private readonly CallLikeExpectsThisBoundClosureArgsAnalyzer $callLikeExpectsThisBindedClosureArgsAnalyzer | ||
| ) { | ||
| } | ||
|
|
||
| public function enterNode(Node $node): ?Node | ||
| { | ||
| if ( | ||
| ! $node instanceof MethodCall | ||
| && ! $node instanceof StaticCall | ||
| && ! $node instanceof FuncCall | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($node->isFirstClassCallable()) { | ||
| return null; | ||
| } | ||
|
|
||
| $args = $this->callLikeExpectsThisBindedClosureArgsAnalyzer->getArgsUsingThisBoundClosure($node); | ||
|
|
||
| if ($args === []) { | ||
| return null; | ||
| } | ||
|
|
||
| foreach ($args as $arg) { | ||
| if ($arg->value instanceof Closure && ! $arg->hasAttribute(AttributeKey::IS_CLOSURE_USES_THIS)) { | ||
| $arg->value->setAttribute(AttributeKey::IS_CLOSURE_USES_THIS, true); | ||
| } | ||
| } | ||
|
|
||
| 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.