Skip to content

Commit

Permalink
Merge pull request #7844 from orklah/non-empty-string
Browse files Browse the repository at this point in the history
fix refining lowercase string and non-empty-string together
  • Loading branch information
orklah committed Apr 5, 2022
2 parents cab6f33 + b72f056 commit d176361
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Psalm/Internal/Type/AssertionReconciler.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/AssertAnnotationTest.php
Expand Up @@ -2038,6 +2038,23 @@ public function assertSame($expected, $actual): void
}
',
],
'assertNonEmptyStringWithLowercaseString' => [
'code' => '<?php
/** @psalm-assert non-empty-string $input */
function assertLowerCase(string $input): void { throw new \Exception($input . " irrelevant"); }
/**
* @param lowercase-string $input
* @return non-empty-lowercase-string
*/
function makeLowerNonEmpty(string $input): string
{
assertLowerCase($input);
return $input;
}',
],
];
}

Expand Down

0 comments on commit d176361

Please sign in to comment.