From 4941b9e5d26e521d55fc397b1569275ef0ba9180 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Mon, 14 Jun 2021 16:02:59 -0400 Subject: [PATCH] Rename class and make it type-hintable --- .../Expression/BinaryOp/ConcatAnalyzer.php | 4 ++-- .../Expression/EncapsulatedStringAnalyzer.php | 2 +- .../Type/Comparator/ScalarTypeComparator.php | 10 +++++----- src/Psalm/Internal/Type/TypeTokenizer.php | 1 + src/Psalm/Type/Atomic.php | 3 +++ .../TNonEmptyNonspecificLiteralString.php | 19 +++++++++++++++++++ .../TNonspecificNonEmptyLiteralString.php | 10 ---------- 7 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 src/Psalm/Type/Atomic/TNonEmptyNonspecificLiteralString.php delete mode 100644 src/Psalm/Type/Atomic/TNonspecificNonEmptyLiteralString.php diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php index fedc57584..b38c19e95 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php @@ -173,7 +173,7 @@ class ConcatAnalyzer if ($literal_concat) { $result_type = new Type\Union($result_type_parts); } else { - $result_type = new Type\Union([new Type\Atomic\TNonspecificNonEmptyLiteralString]); + $result_type = new Type\Union([new Type\Atomic\TNonEmptyNonspecificLiteralString]); } return; @@ -238,7 +238,7 @@ class ConcatAnalyzer if ($left_is_non_empty || $right_is_non_empty) { if ($left_type->allLiterals() && $right_type->allLiterals()) { - $result_type = new Type\Union([new Type\Atomic\TNonspecificNonEmptyLiteralString]); + $result_type = new Type\Union([new Type\Atomic\TNonEmptyNonspecificLiteralString]); } else { $result_type = Type::getNonEmptyString(); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/EncapsulatedStringAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/EncapsulatedStringAnalyzer.php index 0d7a1930f..53ad18947 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/EncapsulatedStringAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/EncapsulatedStringAnalyzer.php @@ -81,7 +81,7 @@ class EncapsulatedStringAnalyzer if ($non_empty) { if ($all_literals) { - $new_type = new Type\Union([new Type\Atomic\TNonspecificNonEmptyLiteralString()]); + $new_type = new Type\Union([new Type\Atomic\TNonEmptyNonspecificLiteralString()]); } else { $new_type = new Type\Union([new Type\Atomic\TNonEmptyString()]); } diff --git a/src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php b/src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php index ca4a33da0..7261dae50 100644 --- a/src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php +++ b/src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php @@ -25,10 +25,10 @@ use Psalm\Type\Atomic\TLiteralInt; use Psalm\Type\Atomic\TLiteralString; use Psalm\Type\Atomic\TLowercaseString; use Psalm\Type\Atomic\TNamedObject; +use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString; use Psalm\Type\Atomic\TNonEmptyString; use Psalm\Type\Atomic\TNonFalsyString; use Psalm\Type\Atomic\TNonspecificLiteralString; -use Psalm\Type\Atomic\TNonspecificNonEmptyLiteralString; use Psalm\Type\Atomic\TNumeric; use Psalm\Type\Atomic\TNumericString; use Psalm\Type\Atomic\TPositiveInt; @@ -88,7 +88,7 @@ class ScalarTypeComparator if ($container_type_part instanceof TNonspecificLiteralString && ($input_type_part instanceof TLiteralString || $input_type_part instanceof TNonspecificLiteralString - || $input_type_part instanceof TNonspecificNonEmptyLiteralString) + || $input_type_part instanceof TNonEmptyNonspecificLiteralString) ) { return true; } @@ -299,7 +299,7 @@ class ScalarTypeComparator } if ((get_class($container_type_part) === TNonEmptyString::class - || get_class($container_type_part) === TNonspecificNonEmptyLiteralString::class) + || get_class($container_type_part) === TNonEmptyNonspecificLiteralString::class) && $input_type_part instanceof TNonFalsyString ) { return true; @@ -313,7 +313,7 @@ class ScalarTypeComparator if ($container_type_part instanceof TNonFalsyString && ($input_type_part instanceof TNonEmptyString - || $input_type_part instanceof TNonspecificNonEmptyLiteralString) + || $input_type_part instanceof TNonEmptyNonspecificLiteralString) ) { if ($atomic_comparison_result) { $atomic_comparison_result->type_coerced = true; @@ -437,7 +437,7 @@ class ScalarTypeComparator if ($container_type_part instanceof TTraitString && (get_class($input_type_part) === TString::class || $input_type_part instanceof TNonEmptyString - || $input_type_part instanceof TNonspecificNonEmptyLiteralString) + || $input_type_part instanceof TNonEmptyNonspecificLiteralString) ) { if ($atomic_comparison_result) { $atomic_comparison_result->type_coerced = true; diff --git a/src/Psalm/Internal/Type/TypeTokenizer.php b/src/Psalm/Internal/Type/TypeTokenizer.php index b9849977e..cea1bf687 100644 --- a/src/Psalm/Internal/Type/TypeTokenizer.php +++ b/src/Psalm/Internal/Type/TypeTokenizer.php @@ -49,6 +49,7 @@ class TypeTokenizer 'mysql-escaped-string' => true, // deprecated 'html-escaped-string' => true, // deprecated 'literal-string' => true, + 'non-empty-literal-string' => true, 'lowercase-string' => true, 'non-empty-lowercase-string' => true, 'positive-int' => true, diff --git a/src/Psalm/Type/Atomic.php b/src/Psalm/Type/Atomic.php index 21dbe2545..b8186f5ac 100644 --- a/src/Psalm/Type/Atomic.php +++ b/src/Psalm/Type/Atomic.php @@ -263,6 +263,9 @@ abstract class Atomic implements TypeNode case 'literal-string': return new Type\Atomic\TNonspecificLiteralString(); + case 'non-empty-literal-string': + return new Type\Atomic\TNonEmptyNonspecificLiteralString(); + case 'false-y': return new TAssertionFalsy(); diff --git a/src/Psalm/Type/Atomic/TNonEmptyNonspecificLiteralString.php b/src/Psalm/Type/Atomic/TNonEmptyNonspecificLiteralString.php new file mode 100644 index 000000000..4b6f5657a --- /dev/null +++ b/src/Psalm/Type/Atomic/TNonEmptyNonspecificLiteralString.php @@ -0,0 +1,19 @@ +