mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Fix #615 - numeric-string is always still a string
This commit is contained in:
parent
c8fe9e89f3
commit
5ff6f27178
@ -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;
|
||||
|
@ -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<mixed,' . $inner_type . '>';
|
||||
}
|
||||
|
||||
return $inner_type;
|
||||
},
|
||||
$type
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Type\Union
|
||||
*/
|
||||
|
@ -739,6 +739,20 @@ class TypeReconciliationTest extends TestCase
|
||||
print_field($array);
|
||||
}',
|
||||
],
|
||||
'numericOrStringPropertySet' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param string|null $b
|
||||
* @psalm-suppress DocblockTypeContradiction
|
||||
*/
|
||||
function foo($b = null) : void {
|
||||
if (is_numeric($b) || is_string($b)) {
|
||||
takesNullableString($b);
|
||||
}
|
||||
}
|
||||
|
||||
function takesNullableString(?string $s) : void {}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user