Skip to content

Commit

Permalink
introduce TypeCombinator to 'non-falsy-string'
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab authored and staabm committed Jul 25, 2022
1 parent 30728d4 commit 7fd0884
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
Expand Down Expand Up @@ -402,19 +403,43 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array
if (
$a instanceof ConstantStringType
&& $a->getValue() === ''
&& $b->describe(VerbosityLevel::value()) === 'non-empty-string'
&& ($b->describe(VerbosityLevel::value()) === 'non-empty-string'
|| $b->describe(VerbosityLevel::value()) === 'non-falsy-string')
) {
return [null, new StringType()];
}

if (
$b instanceof ConstantStringType
&& $b->getValue() === ''
&& $a->describe(VerbosityLevel::value()) === 'non-empty-string'
&& ($a->describe(VerbosityLevel::value()) === 'non-empty-string'
|| $a->describe(VerbosityLevel::value()) === 'non-falsy-string')
) {
return [new StringType(), null];
}

if (
$a instanceof ConstantStringType
&& $a->getValue() === '0'
&& $b->describe(VerbosityLevel::value()) === 'non-falsy-string'
) {
return [null, new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
])];
}

if (
$b instanceof ConstantStringType
&& $b->getValue() === '0'
&& $a->describe(VerbosityLevel::value()) === 'non-falsy-string'
) {
return [new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]), null];
}

return null;
}

Expand Down

0 comments on commit 7fd0884

Please sign in to comment.