From 0fe2f6f951b65976bc4c3ea392ac52b8db4f9dbd Mon Sep 17 00:00:00 2001 From: AndrolGenhald Date: Sat, 22 Jan 2022 17:05:01 -0600 Subject: [PATCH] Refactor existing method to reduce redundant code. --- .../Internal/Analyzer/ClassLikeAnalyzer.php | 56 ------------------- .../Codebase/ConstantTypeResolver.php | 33 +++++++++-- src/Psalm/Internal/Codebase/Reflection.php | 2 +- 3 files changed, 29 insertions(+), 62 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php index 66a3dd7ea..339d5079f 100644 --- a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php @@ -22,7 +22,6 @@ use Psalm\Plugin\EventHandler\Event\AfterClassLikeExistenceCheckEvent; use Psalm\StatementsSource; use Psalm\Storage\ClassLikeStorage; use Psalm\Type; -use Psalm\Type\Atomic\TKeyedArray; use Psalm\Type\Union; use UnexpectedValueException; @@ -31,10 +30,6 @@ use function explode; use function gettype; use function implode; use function in_array; -use function is_array; -use function is_float; -use function is_int; -use function is_string; use function preg_match; use function preg_replace; use function strtolower; @@ -517,57 +512,6 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer } } - /** - * Gets the Psalm literal type from a particular value - * - * @param array|scalar|null $value - * @throws InvalidArgumentException - * - */ - public static function getLiteralTypeFromValue($value, bool $sealed_array = true): Type\Union - { - if (is_array($value)) { - if (empty($value)) { - return Type::getEmptyArray(); - } - - $types = []; - /** @var array|scalar|null $val */ - foreach ($value as $key => $val) { - $types[$key] = self::getLiteralTypeFromValue($val, $sealed_array); - } - $type = new TKeyedArray($types); - $type->sealed = $sealed_array; - return new Type\Union([$type]); - } - - if (is_string($value)) { - return Type::getString($value); - } - - if (is_int($value)) { - return Type::getInt(false, $value); - } - - if (is_float($value)) { - return Type::getFloat($value); - } - - if ($value === false) { - return Type::getFalse(); - } - - if ($value === true) { - return Type::getTrue(); - } - - if ($value === null) { - return Type::getNull(); - } - - throw new InvalidArgumentException(); - } - /** * @param string[] $suppressed_issues */ diff --git a/src/Psalm/Internal/Codebase/ConstantTypeResolver.php b/src/Psalm/Internal/Codebase/ConstantTypeResolver.php index 20ebf0699..88c9e03b1 100644 --- a/src/Psalm/Internal/Codebase/ConstantTypeResolver.php +++ b/src/Psalm/Internal/Codebase/ConstantTypeResolver.php @@ -2,6 +2,7 @@ namespace Psalm\Internal\Codebase; +use InvalidArgumentException; use Psalm\Exception\CircularReferenceException; use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer; use Psalm\Internal\Analyzer\StatementsAnalyzer; @@ -40,6 +41,7 @@ use Psalm\Type\Union; use ReflectionProperty; use function ctype_digit; +use function is_array; use function is_float; use function is_int; use function is_string; @@ -334,10 +336,27 @@ class ConstantTypeResolver } /** - * @param string|int|float|bool|null $value + * Note: This takes an array, but any array should only contain other arrays and scalars. + * + * @param array|string|int|float|bool|null $value */ - private static function getLiteralTypeFromScalarValue($value): Atomic + public static function getLiteralTypeFromScalarValue($value, bool $sealed_array = true): Atomic { + if (is_array($value)) { + if (empty($value)) { + return Type::getEmptyArray()->getSingleAtomic(); + } + + $types = []; + /** @var array|scalar|null $val */ + foreach ($value as $key => $val) { + $types[$key] = new Union([self::getLiteralTypeFromScalarValue($val, $sealed_array)]); + } + $type = new TKeyedArray($types); + $type->sealed = $sealed_array; + return $type; + } + if (is_string($value)) { return new TLiteralString($value); } @@ -351,13 +370,17 @@ class ConstantTypeResolver } if ($value === false) { - return new TFalse; + return new TFalse(); } if ($value === true) { - return new TTrue; + return new TTrue(); } - return new TNull; + if ($value === null) { + return new TNull(); + } + + throw new InvalidArgumentException('$value must be a scalar.'); } } diff --git a/src/Psalm/Internal/Codebase/Reflection.php b/src/Psalm/Internal/Codebase/Reflection.php index e58a98f84..45a12766c 100644 --- a/src/Psalm/Internal/Codebase/Reflection.php +++ b/src/Psalm/Internal/Codebase/Reflection.php @@ -171,7 +171,7 @@ class Reflection foreach ($class_constants as $name => $value) { $storage->constants[$name] = new ClassConstantStorage( ClassLikeAnalyzer::getTypeFromValue($value), - ClassLikeAnalyzer::getLiteralTypeFromValue($value), + new Union([ConstantTypeResolver::getLiteralTypeFromScalarValue($value)]), ClassLikeAnalyzer::VISIBILITY_PUBLIC, null );