From 7fd08842406c896fa2b875fb8c791e9e3005bc23 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 25 Jul 2022 12:53:01 +0200 Subject: [PATCH] introduce TypeCombinator to 'non-falsy-string' --- src/Type/TypeCombinator.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index 8244bf601f2..386e86e6cb3 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -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; @@ -402,7 +403,8 @@ 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()]; } @@ -410,11 +412,34 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array 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; }