1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 09:19:40 +01:00
psalm/src/Psalm/Type.php

503 lines
15 KiB
PHP
Raw Normal View History

2016-06-14 07:23:57 +02:00
<?php
2016-07-26 00:37:44 +02:00
namespace Psalm;
2016-06-14 07:23:57 +02:00
2016-07-26 00:37:44 +02:00
use Psalm\Type\Atomic;
use Psalm\Type\Generic;
use Psalm\Type\Union;
use Psalm\Type\ParseTree;
2016-06-14 07:23:57 +02:00
abstract class Type
{
/**
* Parses a string type representation
* @param string $type_string
2016-06-28 20:28:45 +02:00
* @return Union
2016-06-14 07:23:57 +02:00
*/
2016-06-28 20:28:45 +02:00
public static function parseString($type_string)
2016-06-14 07:23:57 +02:00
{
2016-06-27 04:02:23 +02:00
if (strpos($type_string, '[') !== false) {
$type_string = self::convertSquareBrackets($type_string);
2016-06-27 04:02:23 +02:00
}
$type_string = str_replace('?', 'null|', $type_string);
$type_tokens = self::tokenize($type_string);
2016-06-14 07:23:57 +02:00
if (count($type_tokens) === 1) {
$type_tokens[0] = self::fixScalarTerms($type_tokens[0]);
2016-06-24 00:45:46 +02:00
2016-09-10 00:36:35 +02:00
if ($type_tokens[0] === 'array') {
return Type::getArray();
}
2016-06-28 20:28:45 +02:00
return new Union([new Atomic($type_tokens[0])]);
2016-06-14 07:23:57 +02:00
}
2016-09-01 06:07:29 +02:00
$parse_tree = ParseTree::createFromTokens($type_tokens);
2016-06-14 07:23:57 +02:00
2016-06-15 01:22:29 +02:00
$parsed_type = self::getTypeFromTree($parse_tree);
2016-06-28 20:28:45 +02:00
if (!($parsed_type instanceof Union)) {
2016-06-15 01:22:29 +02:00
$parsed_type = new Union([$parsed_type]);
}
return $parsed_type;
2016-06-14 07:23:57 +02:00
}
public static function fixScalarTerms($type_string)
{
2016-08-14 18:07:19 +02:00
if (in_array(
strtolower($type_string),
['numeric', 'int', 'float', 'string', 'bool', 'true', 'false', 'null', 'array', 'object', 'mixed', 'resource']
)) {
return strtolower($type_string);
}
elseif ($type_string === 'boolean') {
return 'bool';
}
elseif ($type_string === 'integer') {
return 'int';
}
2016-08-14 18:07:19 +02:00
elseif ($type_string === 'double' || $type_string === 'real') {
return 'float';
}
return $type_string;
}
2016-06-14 07:23:57 +02:00
private static function getTypeFromTree(ParseTree $parse_tree)
{
if ($parse_tree->value === ParseTree::GENERIC) {
$generic_type = array_shift($parse_tree->children);
$generic_params = array_map(
function (ParseTree $child_tree) {
$tree_type = self::getTypeFromTree($child_tree);
return $tree_type instanceof Union ? $tree_type : new Union([$tree_type]);
2016-06-14 07:23:57 +02:00
},
$parse_tree->children
);
2016-09-09 22:21:49 +02:00
$generic_type_value = self::fixScalarTerms($generic_type->value);
if ($generic_type_value === 'array' && count($generic_params) === 1) {
2016-09-15 22:42:50 +02:00
array_unshift($generic_params, Type::getMixed());
2016-09-09 22:21:49 +02:00
}
2016-06-14 07:23:57 +02:00
if (!$generic_params) {
throw new \InvalidArgumentException('No generic params provided for type');
}
2016-09-09 22:21:49 +02:00
return new Generic($generic_type_value, $generic_params);
2016-06-14 07:23:57 +02:00
}
if ($parse_tree->value === ParseTree::UNION) {
$union_types = array_map(
function (ParseTree $child_tree) {
return self::getTypeFromTree($child_tree);
},
$parse_tree->children
);
return new Union($union_types);
}
2016-09-09 22:21:49 +02:00
$atomic_type = self::fixScalarTerms($parse_tree->value);
if ($atomic_type === 'array') {
return self::getArray()->types['array'];
}
return new Atomic($atomic_type);
}
/**
2016-09-09 22:21:49 +02:00
* @return array<int, string>
*/
public static function tokenize($return_type)
{
$return_type_tokens = [''];
$was_char = false;
2016-09-09 22:21:49 +02:00
$return_type = str_replace(' ', '', $return_type);
foreach (str_split($return_type) as $char) {
if ($was_char) {
$return_type_tokens[] = '';
}
2016-09-09 22:21:49 +02:00
if ($char === '<' || $char === '>' || $char === '|' || $char === '?' || $char === ',') {
if ($return_type_tokens[count($return_type_tokens) - 1] === '') {
$return_type_tokens[count($return_type_tokens) - 1] = $char;
}
else {
$return_type_tokens[] = $char;
}
$was_char = true;
}
else {
$return_type_tokens[count($return_type_tokens) - 1] .= $char;
$was_char = false;
}
}
return $return_type_tokens;
}
public static function convertSquareBrackets($type)
{
return preg_replace_callback(
2016-08-11 01:21:03 +02:00
'/([a-zA-Z\<\>\\\\_]+)((\[\])+)/',
function ($matches) {
$inner_type = $matches[1];
$dimensionality = strlen($matches[2]) / 2;
for ($i = 0; $i < $dimensionality; $i++) {
2016-09-09 22:21:49 +02:00
$inner_type = 'array<int, ' . $inner_type . '>';
}
return $inner_type;
},
$type
);
2016-06-14 07:23:57 +02:00
}
2016-06-15 01:22:29 +02:00
2016-09-09 22:21:49 +02:00
public static function getInt()
2016-06-15 01:22:29 +02:00
{
$type = new Atomic('int');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-15 01:22:29 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getString()
2016-06-15 01:22:29 +02:00
{
$type = new Atomic('string');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-15 01:22:29 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getNull()
2016-06-16 02:16:40 +02:00
{
$type = new Atomic('null');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-16 02:16:40 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getMixed()
2016-06-15 01:22:29 +02:00
{
$type = new Atomic('mixed');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-15 01:22:29 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getBool()
2016-06-15 01:22:29 +02:00
{
$type = new Atomic('bool');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-15 01:22:29 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getFloat()
2016-06-15 01:22:29 +02:00
{
$type = new Atomic('float');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-15 01:22:29 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getObject()
2016-06-15 01:22:29 +02:00
{
$type = new Atomic('object');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-15 01:22:29 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getArray()
2016-06-15 01:22:29 +02:00
{
2016-09-09 22:21:49 +02:00
$type = new Generic(
'array',
[
2016-09-15 22:42:50 +02:00
Type::getMixed(),
2016-09-09 22:21:49 +02:00
Type::getMixed()
]
);
2016-06-15 01:22:29 +02:00
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-15 01:22:29 +02:00
}
2016-09-12 06:02:26 +02:00
public static function getEmptyArray()
{
return new Type\Union([
new Type\Generic(
'array',
[
new Type\Union([new Type\Atomic('empty')]),
new Type\Union([new Type\Atomic('empty')])
]
)
]);
}
2016-09-09 22:21:49 +02:00
public static function getVoid()
2016-06-16 02:16:40 +02:00
{
$type = new Atomic('void');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-16 02:16:40 +02:00
}
2016-09-09 22:21:49 +02:00
public static function getFalse()
2016-06-16 02:16:40 +02:00
{
$type = new Atomic('false');
2016-09-09 22:21:49 +02:00
return new Union([$type]);
2016-06-16 02:16:40 +02:00
}
2016-06-15 01:22:29 +02:00
public function isMixed()
{
if ($this instanceof Atomic) {
return $this->value === 'mixed';
}
if ($this instanceof Union) {
return isset($this->types['mixed']);
2016-06-15 01:22:29 +02:00
}
}
2016-06-16 02:16:40 +02:00
public function isNull()
{
if ($this instanceof Atomic) {
return $this->value === 'null';
}
if ($this instanceof Union) {
return count($this->types) === 1 && isset($this->types['null']);
2016-06-16 02:16:40 +02:00
}
}
2016-06-16 07:19:52 +02:00
public function isVoid()
{
if ($this instanceof Atomic) {
return $this->value === 'void';
}
if ($this instanceof Union) {
return isset($this->types['void']);
2016-06-16 07:19:52 +02:00
}
}
public function isEmpty()
{
if ($this instanceof Atomic) {
return $this->value === 'empty';
}
2016-07-25 00:02:03 +02:00
if ($this instanceof Union) {
return isset($this->types['empty']);
}
}
/**
* @param array<Union> $redefined_vars
* @param Context $context
* @return void
*/
public static function redefineGenericUnionTypes(array $redefined_vars, Context $context)
{
foreach ($redefined_vars as $var_name => $redefined_union_type) {
foreach ($redefined_union_type->types as $redefined_atomic_type) {
foreach ($context->vars_in_scope[$var_name]->types as $context_type) {
if ($context_type instanceof Type\Generic &&
$redefined_atomic_type instanceof Type\Generic &&
$context_type->value === $redefined_atomic_type->value
) {
2016-09-09 22:21:49 +02:00
// index of last param
$i = count($context_type->type_params) - 1;
if ($context_type->type_params[$i]->isEmpty()) {
$context_type->type_params[$i] = $redefined_atomic_type->type_params[$i];
}
else {
2016-09-09 22:21:49 +02:00
$context_type->type_params[$i] = Type::combineUnionTypes(
$redefined_atomic_type->type_params[$i],
$context_type->type_params[$i]
);
}
2016-09-09 22:21:49 +02:00
if ($i) {
if ($context_type->type_params[0]->isEmpty()) {
$context_type->type_params[0] = $redefined_atomic_type->type_params[0];
}
else {
$context_type->type_params[0] = Type::combineUnionTypes(
$redefined_atomic_type->type_params[0],
$context_type->type_params[0]
);
}
}
}
}
}
}
}
2016-06-16 02:16:40 +02:00
/**
* Combines two union types into one
* @param Union $type_1
* @param Union $type_2
* @param bool $combine_to_mixed if true, combine differing types A and B to mixed, not A|B
2016-06-16 02:16:40 +02:00
* @return Union
*/
public static function combineUnionTypes(Union $type_1, Union $type_2, $combine_to_mixed = false)
2016-06-15 01:22:29 +02:00
{
return self::combineTypes(array_merge(array_values($type_1->types), array_values($type_2->types)), $combine_to_mixed);
2016-06-16 02:16:40 +02:00
}
2016-06-15 01:22:29 +02:00
2016-06-16 02:16:40 +02:00
/**
* Combines types together
* so int + string = int|string
* so array<int> + array<string> = array<int|string>
* and array<int> + string = array<int>|string
* and array<empty> + array<empty> = array<empty>
* and array<string> + array<empty> = array<string>
* and array + array<string> = array<mixed>
*
* @param array<Atomic> $types
* @param bool $combine_to_mixed if true, combine differing types A and B to mixed, not A|B
2016-06-16 02:16:40 +02:00
* @return Union
*/
public static function combineTypes(array $types, $combine_to_mixed = false)
2016-06-16 02:16:40 +02:00
{
if (in_array(null, $types)) {
return Type::getMixed();
}
if (count($types) === 1) {
if ($types[0]->value === 'false') {
$types[0]->value = 'bool';
2016-06-15 01:22:29 +02:00
}
2016-06-16 02:16:40 +02:00
return new Union([$types[0]]);
}
if (!$types) {
throw new \InvalidArgumentException('You must pass at least one type to combineTypes');
}
2016-09-09 22:21:49 +02:00
$key_types = [];
2016-06-16 02:16:40 +02:00
$value_types = [];
foreach ($types as $type) {
if ($type instanceof Union) {
throw new \InvalidArgumentException('Union type not expected here');
}
// if we see the magic empty value and there's more than one type, ignore it
if ($type->value === 'empty') {
continue;
}
if ($type->value === 'mixed') {
return Type::getMixed();
}
if ($type->value === 'void') {
$type->value = 'null';
}
// deal with false|bool => bool
if ($type->value === 'false' && isset($value_types['bool'])) {
continue;
}
elseif ($type->value === 'bool' && isset($value_types['false'])) {
unset($value_types['false']);
}
if (!isset($value_types[$type->value])) {
$value_types[$type->value] = [];
}
2016-09-09 22:21:49 +02:00
if ($type instanceof Generic) {
$value_type_param_index = count($type->type_params) - 1;
$value_types[$type->value][(string) $type->type_params[$value_type_param_index]] = $type->type_params[$value_type_param_index];
if ($value_type_param_index) {
$key_types[$type->value][(string) $type->type_params[0]] = $type->type_params[0];
}
}
else {
2016-09-10 00:36:35 +02:00
if ($type->value === 'array') {
throw new \InvalidArgumentException('Cannot have a non-generic array');
}
2016-09-09 22:21:49 +02:00
$value_types[$type->value][(string) $type] = null;
}
2016-06-16 02:16:40 +02:00
}
2016-06-16 07:19:52 +02:00
if (count($value_types) === 1) {
if (isset($value_types['false'])) {
return self::getBool();
}
}
2016-06-16 02:16:40 +02:00
$new_types = [];
2016-09-09 22:21:49 +02:00
foreach ($value_types as $generic_type => $value_type) {
2016-09-10 00:36:35 +02:00
$key_type = isset($key_types[$generic_type]) ? $key_types[$generic_type] : [];
$expanded_key_types = [];
foreach ($key_type as $expandable_key_type) {
$expanded_key_types = array_merge($expanded_key_types, array_values($expandable_key_type->types));
}
2016-09-09 22:21:49 +02:00
2016-06-16 02:16:40 +02:00
if (count($value_type) === 1) {
$value_type_param = array_values($value_type)[0];
2016-09-09 22:21:49 +02:00
$generic_type_params = [$value_type_param];
// if we're continuing, also add the correspoinding key type param if it exists
2016-09-10 00:36:35 +02:00
if ($expanded_key_types) {
array_unshift($generic_type_params, self::combineTypes($expanded_key_types, $combine_to_mixed));
2016-09-09 22:21:49 +02:00
}
$new_types[] = $value_type_param ? new Generic($generic_type, $generic_type_params) : new Atomic($generic_type);
2016-06-16 02:16:40 +02:00
continue;
}
$expanded_value_types = [];
$has_null = false;
2016-09-09 22:21:49 +02:00
foreach ($value_type as $expandable_value_type) {
if ($expandable_value_type) {
$has_null = $has_null || $expandable_value_type->isNullable();
$expanded_value_types = array_merge($expanded_value_types, array_values($expandable_value_type->types));
}
else {
$expanded_value_types = [Type::getMixed()->types['mixed']];
}
2016-09-09 22:21:49 +02:00
}
// if $combine_to_mixed is true, we want to combine multiple non-null types to a single mixed type.
if ($combine_to_mixed) {
if (count($expanded_value_types) > ($has_null ? 2 : 1)) {
$expanded_value_types = [Type::getMixed()->types['mixed']];
}
}
$generic_type_params = [self::combineTypes($expanded_value_types, $combine_to_mixed)];
2016-06-16 02:16:40 +02:00
2016-09-09 22:21:49 +02:00
if ($expanded_key_types) {
array_unshift($generic_type_params, self::combineTypes($expanded_key_types, $combine_to_mixed));
2016-06-15 01:22:29 +02:00
}
2016-06-16 02:16:40 +02:00
// we have a generic type with
2016-09-09 22:21:49 +02:00
$new_types[] = new Generic($generic_type, $generic_type_params);
2016-06-15 01:22:29 +02:00
}
2016-06-16 02:16:40 +02:00
$new_types = array_values($new_types);
return new Union($new_types);
2016-06-15 01:22:29 +02:00
}
2016-06-14 07:23:57 +02:00
}