From b72f0564bc9b05b8896d584b490c98fcb0729ccc Mon Sep 17 00:00:00 2001 From: orklah Date: Tue, 5 Apr 2022 21:05:23 +0200 Subject: [PATCH] fix refining lowercase string and non-empty-string together --- src/Psalm/Internal/Type/AssertionReconciler.php | 10 ++++++++++ tests/AssertAnnotationTest.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Psalm/Internal/Type/AssertionReconciler.php b/src/Psalm/Internal/Type/AssertionReconciler.php index 7c9c53adccd..2f95918b7f7 100644 --- a/src/Psalm/Internal/Type/AssertionReconciler.php +++ b/src/Psalm/Internal/Type/AssertionReconciler.php @@ -45,8 +45,11 @@ use Psalm\Type\Atomic\TLiteralFloat; use Psalm\Type\Atomic\TLiteralInt; use Psalm\Type\Atomic\TLiteralString; +use Psalm\Type\Atomic\TLowercaseString; use Psalm\Type\Atomic\TMixed; use Psalm\Type\Atomic\TNamedObject; +use Psalm\Type\Atomic\TNonEmptyLowercaseString; +use Psalm\Type\Atomic\TNonEmptyString; use Psalm\Type\Atomic\TNull; use Psalm\Type\Atomic\TNumeric; use Psalm\Type\Atomic\TObject; @@ -793,6 +796,13 @@ private static function filterAtomicWithAnother( $matching_atomic_type = $new_range; } + // Lowercase-string and non-empty-string are compatible but none is contained into the other completely + if (($type_2_atomic instanceof TLowercaseString && $type_1_atomic instanceof TNonEmptyString) || + ($type_2_atomic instanceof TNonEmptyString && $type_1_atomic instanceof TLowercaseString) + ) { + $matching_atomic_type = new TNonEmptyLowercaseString(); + } + if (!$atomic_comparison_results->type_coerced && $atomic_comparison_results->scalar_type_match_found) { $any_scalar_type_match_found = true; } diff --git a/tests/AssertAnnotationTest.php b/tests/AssertAnnotationTest.php index b0f672ef163..e32297b4f3d 100644 --- a/tests/AssertAnnotationTest.php +++ b/tests/AssertAnnotationTest.php @@ -2038,6 +2038,23 @@ public function assertSame($expected, $actual): void } ', ], + 'assertNonEmptyStringWithLowercaseString' => [ + 'code' => '