-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix condition literal-string is non-empty-string #10927
Changes from all commits
c0a9d04
f1ac418
279cfb4
e6d2c8e
d715e33
91cfbff
69ecec6
78e526c
a184f15
91ea61e
ee675c7
0e4fc37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,10 @@ public function __construct(Union $key, Union $value, bool $is_list, ?int $count | |
|
||
/** | ||
* @return ( | ||
* $type is TArrayKey ? self : ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As soon as I fixed |
||
* $type is TArray ? self : null | ||
* $type is TKeyedArray ? self : ( | ||
* $type is TNonEmptyArray ? self : ( | ||
* $type is TArray ? self : null | ||
* ) | ||
* ) | ||
* ) | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -451,6 +451,12 @@ private static function replaceConditional( | |
null, | ||
false, | ||
false, | ||
) && null === Type::intersectUnionTypes( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic was When doing
I think this logic is wrong and does not work well with string type like |
||
new Union([$candidate_atomic_type]), | ||
$conditional_type, | ||
$codebase, | ||
false, | ||
false, | ||
)) { | ||
$matching_else_types[] = $candidate_atomic_type; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,8 +33,10 @@ | |
use Psalm\Type\Atomic\TNamedObject; | ||
use Psalm\Type\Atomic\TNever; | ||
use Psalm\Type\Atomic\TNonEmptyLowercaseString; | ||
use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString; | ||
use Psalm\Type\Atomic\TNonEmptyString; | ||
use Psalm\Type\Atomic\TNonFalsyString; | ||
use Psalm\Type\Atomic\TNonspecificLiteralString; | ||
use Psalm\Type\Atomic\TNull; | ||
use Psalm\Type\Atomic\TNumeric; | ||
use Psalm\Type\Atomic\TNumericString; | ||
|
@@ -787,8 +789,26 @@ public static function intersectUnionTypes( | |
|
||
//if a type is contained by the other, the intersection is the narrowest type | ||
if (!$intersection_performed) { | ||
$type_1_in_2 = UnionTypeComparator::isContainedBy($codebase, $type_1, $type_2); | ||
$type_2_in_1 = UnionTypeComparator::isContainedBy($codebase, $type_2, $type_1); | ||
$type_1_in_2 = UnionTypeComparator::isContainedBy( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I tried to use Seems like those parameters were not passed to every calls. |
||
$codebase, | ||
$type_1, | ||
$type_2, | ||
false, | ||
false, | ||
null, | ||
$allow_interface_equality, | ||
$allow_float_int_equality, | ||
); | ||
$type_2_in_1 = UnionTypeComparator::isContainedBy( | ||
$codebase, | ||
$type_2, | ||
$type_1, | ||
false, | ||
false, | ||
null, | ||
$allow_interface_equality, | ||
$allow_float_int_equality, | ||
); | ||
if ($type_1_in_2) { | ||
$intersection_performed = true; | ||
$combined_type = $type_1->getBuilder(); | ||
|
@@ -909,6 +929,13 @@ private static function intersectAtomicTypes( | |
$intersection_atomic = $type_1_atomic; | ||
$wider_type = $type_2_atomic; | ||
$intersection_performed = true; | ||
} elseif (($type_1_atomic instanceof TNonspecificLiteralString | ||
&& $type_2_atomic instanceof TNonEmptyString) | ||
|| ($type_1_atomic instanceof TNonEmptyString | ||
&& $type_2_atomic instanceof TNonspecificLiteralString) | ||
) { | ||
$intersection_atomic = new TNonEmptyNonspecificLiteralString(); | ||
$intersection_performed = true; | ||
} | ||
|
||
if ($intersection_atomic | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -891,7 +891,7 @@ function getSomethingElse() | |
interface ContainerInterface | ||
{ | ||
/** | ||
* @template TRequestedInstance extends InstanceType | ||
* @template TRequestedInstance of InstanceType | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
is not a correct syntax, As soon as I fixed the |
||
* @param class-string<TRequestedInstance>|string $name | ||
* @return ($name is class-string ? TRequestedInstance : InstanceType) | ||
*/ | ||
|
@@ -979,6 +979,62 @@ function getSomething(string $string) | |
], | ||
'ignored_issues' => [], | ||
], | ||
'literalStringIsNotNonEmpty' => [ | ||
'code' => '<?php | ||
/** | ||
* @param literal-string $string | ||
* @psalm-return ($string is non-empty-string ? string : int) | ||
*/ | ||
function getSomething(string $string) | ||
{ | ||
if (!$string) { | ||
return 1; | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
/** @var literal-string $literalString */ | ||
$literalString; | ||
$something = getSomething($literalString); | ||
/** @var non-empty-literal-string $nonEmptyliteralString */ | ||
$nonEmptyliteralString; | ||
$something2 = getSomething($nonEmptyliteralString); | ||
', | ||
'assertions' => [ | ||
'$something' => 'int|string', | ||
'$something2' => 'string', | ||
], | ||
'ignored_issues' => [], | ||
], | ||
'literalStringIsNotNonEmptyWithUnion' => [ | ||
'code' => '<?php | ||
/** | ||
* @param literal-string|int $string | ||
* @psalm-return ($string is non-empty-string|int ? string : int) | ||
*/ | ||
function getSomething($string) | ||
{ | ||
if (!$string) { | ||
return 1; | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
/** @var literal-string $literalString */ | ||
$literalString; | ||
$something = getSomething($literalString); | ||
/** @var non-empty-literal-string $nonEmptyliteralString */ | ||
$nonEmptyliteralString; | ||
$something2 = getSomething($nonEmptyliteralString); | ||
', | ||
'assertions' => [ | ||
'$something' => 'int|string', | ||
'$something2' => 'string', | ||
], | ||
'ignored_issues' => [], | ||
], | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This avoid
This is technically true because the conditional return type is
and the check
InitializeResult is Promise
cannot be resolved sinceInitializeResult&Promise
would be a possible type.