Skip to content

Commit b8511fc

Browse files
authored
TypeCombinator: Simplify ConstantStringType handling (#4666)
1 parent 2a9f636 commit b8511fc

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ parameters:
17101710
-
17111711
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
17121712
identifier: phpstanApi.instanceofType
1713-
count: 5
1713+
count: 3
17141714
path: src/Type/TypeCombinator.php
17151715

17161716
-

src/Type/TypeCombinator.php

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -494,50 +494,56 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array
494494

495495
if (
496496
$a instanceof ConstantStringType
497-
&& $a->getValue() === ''
498-
&& ($b->describe(VerbosityLevel::value()) === 'non-empty-string'
499-
|| $b->describe(VerbosityLevel::value()) === 'non-falsy-string')
500497
) {
501-
return [null, self::intersect(
502-
new StringType(),
503-
...self::getAccessoryCaseStringTypes($b),
504-
)];
505-
}
506-
507-
if (
508-
$b instanceof ConstantStringType
509-
&& $b->getValue() === ''
510-
&& ($a->describe(VerbosityLevel::value()) === 'non-empty-string'
511-
|| $a->describe(VerbosityLevel::value()) === 'non-falsy-string')
512-
) {
513-
return [self::intersect(
514-
new StringType(),
515-
...self::getAccessoryCaseStringTypes($a),
516-
), null];
517-
}
518-
519-
if (
520-
$a instanceof ConstantStringType
521-
&& $a->getValue() === '0'
522-
&& $b->describe(VerbosityLevel::value()) === 'non-falsy-string'
523-
) {
524-
return [null, self::intersect(
525-
new StringType(),
526-
new AccessoryNonEmptyStringType(),
527-
...self::getAccessoryCaseStringTypes($b),
528-
)];
498+
$description = $b->describe(VerbosityLevel::value());
499+
if (
500+
$a->getValue() === ''
501+
&& ($description === 'non-empty-string'
502+
|| $description === 'non-falsy-string')
503+
) {
504+
return [null, self::intersect(
505+
new StringType(),
506+
...self::getAccessoryCaseStringTypes($b),
507+
)];
508+
}
509+
510+
if (
511+
$a->getValue() === '0'
512+
&& $description === 'non-falsy-string'
513+
) {
514+
return [null, self::intersect(
515+
new StringType(),
516+
new AccessoryNonEmptyStringType(),
517+
...self::getAccessoryCaseStringTypes($b),
518+
)];
519+
}
529520
}
530521

531522
if (
532523
$b instanceof ConstantStringType
533-
&& $b->getValue() === '0'
534-
&& $a->describe(VerbosityLevel::value()) === 'non-falsy-string'
535524
) {
536-
return [self::intersect(
537-
new StringType(),
538-
new AccessoryNonEmptyStringType(),
539-
...self::getAccessoryCaseStringTypes($a),
540-
), null];
525+
$description = $a->describe(VerbosityLevel::value());
526+
if (
527+
$b->getValue() === ''
528+
&& ($description === 'non-empty-string'
529+
|| $description === 'non-falsy-string')
530+
) {
531+
return [self::intersect(
532+
new StringType(),
533+
...self::getAccessoryCaseStringTypes($a),
534+
), null];
535+
}
536+
537+
if (
538+
$b->getValue() === '0'
539+
&& $description === 'non-falsy-string'
540+
) {
541+
return [self::intersect(
542+
new StringType(),
543+
new AccessoryNonEmptyStringType(),
544+
...self::getAccessoryCaseStringTypes($a),
545+
), null];
546+
}
541547
}
542548

543549
return null;

0 commit comments

Comments
 (0)