From 3a96b9097d6fbc29085210f2d85dd5d95755ef31 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 27 Jul 2021 18:05:33 +0700 Subject: [PATCH] [DeadCode] Skip fluent return $this on RemoveEmptyMethodCallRector (#521) --- .../Fixture/skip_fluent_return_this.php.inc | 37 +++++++++++++++++++ .../RemoveEmptyMethodCallRector.php | 17 ++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/skip_fluent_return_this.php.inc diff --git a/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/skip_fluent_return_this.php.inc b/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/skip_fluent_return_this.php.inc new file mode 100644 index 00000000000..4a9a34c673f --- /dev/null +++ b/rules-tests/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture/skip_fluent_return_this.php.inc @@ -0,0 +1,37 @@ +prepare()->run(); + } +} + +class Extended extends SkipFluentReturnThis +{ + public function run() + { + echo 'hello'; + } +} + +(new SkipFluentReturnThis())->call(Extended::class); + +?> diff --git a/rules/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector.php b/rules/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector.php index 0d83b97d4d1..10f985bc493 100644 --- a/rules/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector.php +++ b/rules/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector.php @@ -76,7 +76,8 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = $node->var->getAttribute(AttributeKey::SCOPE); + $scope = $this->getScope($node); + if (! $scope instanceof Scope) { return null; } @@ -114,6 +115,20 @@ public function refactor(Node $node): ?Node return $node; } + private function getScope(MethodCall $methodCall): ?Scope + { + if ($methodCall->var instanceof MethodCall) { + return null; + } + + $scope = $methodCall->var->getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + return null; + } + + return $scope; + } + private function shouldSkipClassMethod(Class_ | Trait_ | Interface_ $classLike, MethodCall $methodCall): bool { if (! $classLike instanceof Class_) {