From 0cfe565d0afbcd31eadcc281b9017b5692911661 Mon Sep 17 00:00:00 2001 From: Brown Date: Mon, 9 Mar 2020 18:59:56 -0400 Subject: [PATCH] Fix #2952 - always be cloning --- .../Statements/ExpressionAnalyzer.php | 2 +- tests/ClassStringTest.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php index f40db7110a8..76683a342d3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php @@ -1305,7 +1305,7 @@ private static function fleshOutAtomicType( if ($return_type instanceof Type\Atomic\TClassString && $return_type->as_type ) { - $new_as_type = $return_type->as_type; + $new_as_type = clone $return_type->as_type; self::fleshOutAtomicType( $codebase, diff --git a/tests/ClassStringTest.php b/tests/ClassStringTest.php index 754079117ac..08c3e4378e3 100644 --- a/tests/ClassStringTest.php +++ b/tests/ClassStringTest.php @@ -695,6 +695,39 @@ public static function r() : string } }' ], + 'traitClassStringClone' => [ + ' */ + public static function getFactoryClass() + { + return static::class; + } + } + + class A + { + use Factory; + + public static function factory(): self + { + $class = static::getFactoryClass(); + return new $class; + } + } + + class B + { + use Factory; + + public static function factory(): self + { + $class = static::getFactoryClass(); + return new $class; + } + }' + ], ]; }