From fadb3a76e7be6a7f0f74c7624311da736f1359aa Mon Sep 17 00:00:00 2001 From: 2e3s <2e3s19@gmail.com> Date: Sat, 12 Dec 2020 10:26:14 -0500 Subject: [PATCH] Memoize private inferred mutation-free methods (#4832) --- .../Call/Method/MethodCallPurityAnalyzer.php | 4 +++- tests/MethodCallTest.php | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php index c61c8a5d8..a3b030ee8 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php @@ -2,6 +2,7 @@ namespace Psalm\Internal\Analyzer\Statements\Expression\Call\Method; use PhpParser; +use Psalm\Internal\Analyzer\ClassLikeAnalyzer; use Psalm\Internal\Analyzer\StatementsAnalyzer; use Psalm\Codebase; use Psalm\CodeLocation; @@ -79,7 +80,8 @@ class MethodCallPurityAnalyzer ) { if ($method_storage->mutation_free && (!$method_storage->mutation_free_inferred - || $method_storage->final) + || $method_storage->final + || $method_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE) && ($method_storage->immutable || $config->remember_property_assignments_after_call) ) { if ($context->inside_conditional diff --git a/tests/MethodCallTest.php b/tests/MethodCallTest.php index d23ff9685..dc176c2b4 100644 --- a/tests/MethodCallTest.php +++ b/tests/MethodCallTest.php @@ -857,6 +857,27 @@ class MethodCallTest extends TestCase printInt($obj->getInt()); }', ], + 'privateInferredMutationFreeMethodCallMemoize' => [ + 'property; + } + + public function test(int $int): void { + if ($this->getProperty() !== null) { + $this->getProperty()->test(); + } + } + }', + ], 'inferredFinalMethod' => [ '