diff --git a/src/Psalm/Checker/TypeChecker.php b/src/Psalm/Checker/TypeChecker.php index 149c0dbdb..86a5bbbca 100644 --- a/src/Psalm/Checker/TypeChecker.php +++ b/src/Psalm/Checker/TypeChecker.php @@ -18,6 +18,7 @@ use Psalm\Type\Atomic\TMixed; use Psalm\Type\Atomic\TNamedObject; use Psalm\Type\Atomic\TNull; use Psalm\Type\Atomic\TNumeric; +use Psalm\Type\Atomic\TNumericString; use Psalm\Type\Atomic\TObject; use Psalm\Type\Atomic\TResource; use Psalm\Type\Atomic\TScalar; @@ -508,6 +509,16 @@ class TypeChecker return true; } + if ($container_type_part instanceof TString && $input_type_part instanceof TNumericString) { + return true; + } + + if ($container_type_part instanceof TNumericString && $input_type_part instanceof TString) { + $type_coerced = true; + + return false; + } + if ($container_type_part instanceof TClassString && $input_type_part instanceof TString) { if (\Psalm\Config::getInstance()->allow_coercion_from_string_to_class_const) { $type_coerced = true; diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index 2dc11cd6d..c55312f14 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -45,10 +45,6 @@ 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); @@ -337,14 +333,10 @@ 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; } @@ -405,35 +397,6 @@ 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 */ diff --git a/tests/TypeReconciliationTest.php b/tests/TypeReconciliationTest.php index 8637dfb13..010cdb14b 100644 --- a/tests/TypeReconciliationTest.php +++ b/tests/TypeReconciliationTest.php @@ -739,6 +739,20 @@ class TypeReconciliationTest extends TestCase print_field($array); }', ], + 'numericOrStringPropertySet' => [ + '