diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index d05a9b9d5c3..cc5de1f8b03 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -277,7 +277,7 @@ public static function getString(?string $value = null): Union return new Union([$value === null ? new TString() : self::getAtomicStringFromLiteral($value)]); } - /** @return TLiteralString|TNonEmptyString */ + /** @return TLiteralString|TNonEmptyString|TNonFalsyString */ public static function getAtomicStringFromLiteral(string $value, bool $from_docblock = false): TString { $config = Config::getInstance(); @@ -289,8 +289,10 @@ public static function getAtomicStringFromLiteral(string $value, bool $from_docb if (!$type) { if ($value === '' || strlen($value) < $config->max_string_length) { $type = new TLiteralString($value, $from_docblock); - } else { + } elseif ($value === '0') { $type = new TNonEmptyString($from_docblock); + } else { + $type = new TNonFalsyString($from_docblock); } } diff --git a/tests/TypeCombinationTest.php b/tests/TypeCombinationTest.php index b891e84dcc2..f041453d9d5 100644 --- a/tests/TypeCombinationTest.php +++ b/tests/TypeCombinationTest.php @@ -120,6 +120,13 @@ function takesLiteralString($arg) {} takesLiteralString($c); }', ], + 'tooLongLiteralShouldBeNonFalsyString' => [ + 'code' => ' [ + '$x===' => 'non-falsy-string', + ] + ], ]; }