1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Refactor existing method to reduce redundant code.

This commit is contained in:
AndrolGenhald 2022-01-22 17:05:01 -06:00
parent 7d256975e4
commit 0fe2f6f951
3 changed files with 29 additions and 62 deletions

View File

@ -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
*/

View File

@ -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.');
}
}

View File

@ -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
);