-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathConstantTypeHelper.php
More file actions
69 lines (62 loc) · 1.9 KB
/
ConstantTypeHelper.php
File metadata and controls
69 lines (62 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use UnitEnum;
use function count;
use function function_exists;
use function get_class;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
use function is_nan;
use function is_object;
use function is_string;
/** @api */
class ConstantTypeHelper
{
/**
* @param mixed $value
*/
public static function getTypeFromValue($value): Type
{
if (is_int($value)) {
return new ConstantIntegerType($value);
} elseif (is_float($value)) {
if (is_nan($value)) {
return new MixedType();
}
return new ConstantFloatType($value);
} elseif (is_bool($value)) {
return new ConstantBooleanType($value);
} elseif ($value === null) {
return new NullType();
} elseif (is_string($value)) {
return new ConstantStringType($value);
} elseif (is_array($value)) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
if (count($value) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
$arrayBuilder->degradeToGeneralArray();
}
foreach ($value as $k => $v) {
$arrayBuilder->setOffsetValueType(self::getTypeFromValue($k), self::getTypeFromValue($v));
}
return $arrayBuilder->getArray();
} elseif (is_object($value)) {
$class = get_class($value);
/** phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName */
if (function_exists('enum_exists') && \enum_exists($class)) {
/** @var UnitEnum $value */
return new EnumCaseObjectType($class, $value->name);
}
/** phpcs:enable */
return new ObjectType(get_class($value));
}
return new MixedType();
}
}