From 8b42e6774406c59ac7af5eb1ba4f5e789588d630 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 1 Aug 2020 02:01:52 +0200 Subject: [PATCH] [MagicDisclosure] Skip getters (#3856) --- .../MethodCall/DefluentMethodCallRector.php | 17 +++++++++++++++++ .../Fixture/skip_getter_on_new.php.inc | 13 +++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 rules/magic-disclosure/tests/Rector/MethodCall/DefluentMethodCallRector/Fixture/skip_getter_on_new.php.inc diff --git a/rules/magic-disclosure/src/Rector/MethodCall/DefluentMethodCallRector.php b/rules/magic-disclosure/src/Rector/MethodCall/DefluentMethodCallRector.php index c75e45c32d9..ff888ffc0c3 100644 --- a/rules/magic-disclosure/src/Rector/MethodCall/DefluentMethodCallRector.php +++ b/rules/magic-disclosure/src/Rector/MethodCall/DefluentMethodCallRector.php @@ -94,7 +94,12 @@ PHP return null; } + if ($this->isGetterMethodCall($methodCall)) { + return null; + } + $chainMethodCalls = $this->chainMethodCallNodeAnalyzer->collectAllMethodCallsInChain($methodCall); + $assignAndRootExpr = $this->chainMethodCallRootExtractor->extractFromMethodCalls($chainMethodCalls); if ($assignAndRootExpr === null) { return null; @@ -222,4 +227,16 @@ PHP return $nodesToAdd; } + + private function isGetterMethodCall(MethodCall $methodCall): bool + { + if ($methodCall->var instanceof MethodCall) { + return false; + } + $methodCallStaticType = $this->getStaticType($methodCall); + $methodCallVarStaticType = $this->getStaticType($methodCall->var); + + // getter short call type + return ! $methodCallStaticType->equals($methodCallVarStaticType); + } } diff --git a/rules/magic-disclosure/tests/Rector/MethodCall/DefluentMethodCallRector/Fixture/skip_getter_on_new.php.inc b/rules/magic-disclosure/tests/Rector/MethodCall/DefluentMethodCallRector/Fixture/skip_getter_on_new.php.inc new file mode 100644 index 00000000000..32f7bd164e7 --- /dev/null +++ b/rules/magic-disclosure/tests/Rector/MethodCall/DefluentMethodCallRector/Fixture/skip_getter_on_new.php.inc @@ -0,0 +1,13 @@ +otherFunction(); + } +}