From 8505ca2a23904375338a84ff84eb0189e8119ea5 Mon Sep 17 00:00:00 2001 From: Brown Date: Thu, 3 Sep 2020 15:28:09 -0400 Subject: [PATCH] Allow passing mutable object into immutable class to store reference --- .../InstancePropertyAssignmentAnalyzer.php | 20 ----- tests/ImmutableAnnotationTest.php | 84 +++++++------------ 2 files changed, 30 insertions(+), 74 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php index f3715ab43..409b88392 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php @@ -1117,26 +1117,6 @@ class InstancePropertyAssignmentAnalyzer ) { $codebase->analyzer->addMutableClass($declaring_class_storage->name); } - } elseif ($assignment_value_type - && ($declaring_class_storage->mutation_free - || $codebase->alter_code) - ) { - $visitor = new \Psalm\Internal\TypeVisitor\ImmutablePropertyAssignmentVisitor( - $statements_analyzer, - $stmt - ); - - $visitor->traverse($assignment_value_type); - - if (!$declaring_class_storage->mutation_free - && $statements_analyzer->getSource() - instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer - && $statements_analyzer->getSource()->track_mutations - && $visitor->has_mutation - ) { - $statements_analyzer->getSource()->inferred_has_mutation = true; - $statements_analyzer->getSource()->inferred_impure = true; - } } } } diff --git a/tests/ImmutableAnnotationTest.php b/tests/ImmutableAnnotationTest.php index 63beac9f3..22870c026 100644 --- a/tests/ImmutableAnnotationTest.php +++ b/tests/ImmutableAnnotationTest.php @@ -494,6 +494,36 @@ class ImmutableAnnotationTest extends TestCase } }', ], + 'allowPassingMutableIntoImmutable' => [ + 'item = $item; + } + + public function get(): int { + return $this->item->get(); + } + } + + class Item { + private int $i = 0; + + public function mutate(): void { + $this->i++; + } + + /** @psalm-mutation-free */ + public function get(): int { + return $this->i; + } + }', + ], ]; } @@ -631,37 +661,6 @@ class ImmutableAnnotationTest extends TestCase }', 'error_message' => 'MissingImmutableAnnotation', ], - 'preventPassingMutableIntoImmutable' => [ - 'item = $item; - } - - public function get(): int { - return $this->item->get(); - } - } - - class Item { - private int $i = 0; - - public function mutate(): void { - $this->i++; - } - - /** @psalm-mutation-free */ - public function get(): int { - return $this->i; - } - }', - 'error_message' => 'ImpurePropertyAssignment', - ], 'preventNonImmutableTraitInImmutableClass' => [ ' 'MutableDependency' ], - 'preventAssigningArrayToImmutableProperty' => [ - 'items = $items; - } - }', - 'error_message' => 'ImpurePropertyAssignment', - ], 'mutationInPropertyAssignment' => [ '