diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index c55312f14..2dc11cd6d 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -45,6 +45,10 @@ abstract class Type // remove all unacceptable characters $type_string = preg_replace('/[^A-Za-z0-9\-_\\\\&|\? \<\>\{\}:,\]\[\(\)\$]/', '', trim($type_string)); + if (strpos($type_string, '[') !== false) { + $type_string = self::convertSquareBrackets($type_string); + } + $type_string = preg_replace('/\?(?=[a-zA-Z])/', 'null|', $type_string); $type_tokens = self::tokenize($type_string); @@ -333,10 +337,14 @@ abstract class Type Aliases $aliases, array $template_types = null ) { + if (strpos($return_type, '[') !== false) { + $return_type = self::convertSquareBrackets($return_type); + } + $return_type_tokens = self::tokenize($return_type); foreach ($return_type_tokens as $i => &$return_type_token) { - if (in_array($return_type_token, ['<', '>', '|', '?', ',', '{', '}', ':', '[', ']'], true)) { + if (in_array($return_type_token, ['<', '>', '|', '?', ',', '{', '}', ':'], true)) { continue; } @@ -397,6 +405,35 @@ abstract class Type return ($namespace ? $namespace . '\\' : '') . $class; } + /** + * @param string $type + * + * @return string + */ + public static function convertSquareBrackets($type) + { + $class_chars = '[a-zA-Z0-9\<\>\\\\_]+'; + + return preg_replace_callback( + '/(' . $class_chars . '|' . '\((' . $class_chars . '(\|' . $class_chars . ')*' . ')\))((\[\])+)/', + /** + * @return string + */ + function (array $matches) { + $inner_type = str_replace(['(', ')'], '', (string)$matches[1]); + + $dimensionality = strlen((string)$matches[4]) / 2; + + for ($i = 0; $i < $dimensionality; ++$i) { + $inner_type = 'array'; + } + + return $inner_type; + }, + $type + ); + } + /** * @return Type\Union */