Skip to content
Permalink
Browse files

Include non-empty-string type in more places

  • Loading branch information
muglug committed Dec 30, 2019
1 parent 023c4bc commit c79ba0b09e0a2322f404a903199140c6fec0868a
@@ -1134,9 +1134,7 @@ private static function transformTemplates(
$new_bases = [];

foreach ($mapped_type->getTypes() as $mapped_atomic_type) {
if ($mapped_atomic_type instanceof Type\Atomic\TTemplateParam
&& $mapped_atomic_type->defining_class
) {
if ($mapped_atomic_type instanceof Type\Atomic\TTemplateParam) {
$new_bases[] = $mapped_atomic_type->defining_class;
}
}
@@ -1040,25 +1040,23 @@ private static function getExtendedType(
continue;
}

if ($extended_atomic_type->defining_class) {
$candidate_type = self::getExtendedType(
$extended_atomic_type->param_name,
$extended_atomic_type->defining_class,
$calling_class,
$template_type_extends,
$class_template_types,
$calling_type_params
);
$candidate_type = self::getExtendedType(
$extended_atomic_type->param_name,
$extended_atomic_type->defining_class,
$calling_class,
$template_type_extends,
$class_template_types,
$calling_type_params
);

if ($candidate_type) {
if (!$return_type) {
$return_type = $candidate_type;
} else {
$return_type = Type::combineUnionTypes(
$return_type,
$candidate_type
);
}
if ($candidate_type) {
if (!$return_type) {
$return_type = $candidate_type;
} else {
$return_type = Type::combineUnionTypes(
$return_type,
$candidate_type
);
}
}
}
@@ -1218,11 +1218,11 @@ function (PhpParser\Node\Arg $arg) {
&& !isset(
$template_result->generic_params
[$template_type->param_name]
[$template_type->defining_class ?: '']
[$template_type->defining_class]
)
) {
$template_result->generic_params[$template_type->param_name] = [
($template_type->defining_class ?: '') => [Type::getEmpty(), 0]
($template_type->defining_class) => [Type::getEmpty(), 0]
];
}
}
@@ -1635,14 +1635,12 @@ public static function getClassTemplateParams(
);
}
}
} elseif ($type_extends_atomic->defining_class
&& isset(
$calling_class_storage
->template_type_extends
[$type_extends_atomic->defining_class]
[$type_extends_atomic->param_name]
)
) {
} elseif (isset(
$calling_class_storage
->template_type_extends
[$type_extends_atomic->defining_class]
[$type_extends_atomic->param_name]
)) {
$mapped_offset = array_search(
$type_extends_atomic->param_name,
array_keys($calling_class_storage
@@ -784,10 +784,10 @@ function (PhpParser\Node\Arg $arg) {

foreach ($extended_type->getTypes() as $t) {
if ($t instanceof Type\Atomic\TTemplateParam
&& isset($found_generic_params[$t->param_name][$t->defining_class ?: ''])
&& isset($found_generic_params[$t->param_name][$t->defining_class])
) {
$found_generic_params[$type_key][$template_fq_class_name] = [
$found_generic_params[$t->param_name][$t->defining_class ?: ''][0]
$found_generic_params[$t->param_name][$t->defining_class][0]
];
} else {
$found_generic_params[$type_key][$template_fq_class_name] = [
@@ -870,10 +870,10 @@ function (PhpParser\Node\Arg $arg) {
if (!isset(
$template_result->generic_params
[$template_type->param_name]
[$template_type->defining_class ?: '']
[$template_type->defining_class]
)) {
$template_result->generic_params[$template_type->param_name] = [
($template_type->defining_class ?: '') => [Type::getEmpty(), 0]
($template_type->defining_class) => [Type::getEmpty(), 0]
];
}
}
@@ -1962,7 +1962,6 @@ private static function getTemplateTypesForFunction(

foreach ($type->getTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam
&& $atomic_type->defining_class
&& isset(
$calling_class_storage
->template_type_extends
@@ -672,10 +672,7 @@ public static function isAtomicContainedBy(
&& \substr($input_type_part->defining_class, 0, 3) !== 'fn-'
&& \substr($container_type_part->defining_class, 0, 3) !== 'fn-')
) {
if ($container_type_part->defining_class
&& $input_type_part->defining_class
&& \substr($input_type_part->defining_class, 0, 3) !== 'fn-'
) {
if (\substr($input_type_part->defining_class, 0, 3) !== 'fn-') {
$input_class_storage = $codebase->classlike_storage_provider->get(
$input_type_part->defining_class
);
@@ -427,9 +427,7 @@ private static function extendType(
$extended_types = [];

foreach ($type->getTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam
&& $atomic_type->defining_class
) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
$referenced_type
= $storage->template_type_extends[$atomic_type->defining_class][$atomic_type->param_name]
?? null;
@@ -381,7 +381,7 @@ private static function handleTemplateParamStandin(
) : array {
$template_type = $template_result->template_types
[$atomic_type->param_name]
[$atomic_type->defining_class ?: '']
[$atomic_type->defining_class]
[0];

if ($template_type->getId() === $key) {
@@ -431,7 +431,7 @@ private static function handleTemplateParamStandin(
$atomic_types[] = clone $key_type_atomic;
}

$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class ?: ''][0]
$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class][0]
= clone $key_type;
}
}
@@ -475,11 +475,11 @@ private static function handleTemplateParamStandin(
$generic_param->setFromDocblock();

if (isset(
$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class ?: ''][0]
$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class][0]
)) {
$existing_depth = $template_result->generic_params
[$atomic_type->param_name]
[$atomic_type->defining_class ?: '']
[$atomic_type->defining_class]
[1]
?? -1;

@@ -491,15 +491,15 @@ private static function handleTemplateParamStandin(
$generic_param = \Psalm\Type::combineUnionTypes(
$template_result->generic_params
[$atomic_type->param_name]
[$atomic_type->defining_class ?: '']
[$atomic_type->defining_class]
[0],
$generic_param,
$codebase
);
}
}

$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class ?: ''] = [
$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class] = [
$generic_param,
$depth,
];
@@ -516,7 +516,7 @@ private static function handleTemplateParamStandin(
$replacement_type
)
) {
$template_result->template_types[$atomic_type->param_name][$atomic_type->defining_class ?: ''][0]
$template_result->template_types[$atomic_type->param_name][$atomic_type->defining_class][0]
= clone $input_type;
}
}
@@ -84,9 +84,9 @@ public function replaceIntersectionTemplateTypesWithArgTypes(array $template_typ

foreach ($this->extra_types as $extra_type) {
if ($extra_type instanceof TTemplateParam
&& isset($template_types[$extra_type->param_name][$extra_type->defining_class ?: ''])
&& isset($template_types[$extra_type->param_name][$extra_type->defining_class])
) {
$template_type = clone $template_types[$extra_type->param_name][$extra_type->defining_class ?: ''][0];
$template_type = clone $template_types[$extra_type->param_name][$extra_type->defining_class][0];

foreach ($template_type->getTypes() as $template_type_part) {
if ($template_type_part instanceof TNamedObject) {
@@ -24,14 +24,14 @@ class TTemplateParam extends \Psalm\Type\Atomic
public $as;

/**
* @var string
* @var non-empty-string
*/
public $defining_class;

/**
* @param string $param_name
* @param non-empty-string $defining_class
*/
public function __construct($param_name, Union $extends, string $defining_class)
public function __construct(string $param_name, Union $extends, string $defining_class)
{
$this->param_name = $param_name;
$this->as = $extends;
@@ -52,7 +52,7 @@ public function getKey()
return $this->param_name . '&' . implode('&', $this->extra_types);
}

return $this->param_name . ($this->defining_class ? ':' . $this->defining_class : '');
return $this->param_name . ':' . $this->defining_class;
}

/**
@@ -71,7 +71,7 @@ public function getId()
}

return $this->param_name
. ($this->defining_class ? ':' . $this->defining_class : '')
. ':' . $this->defining_class
. ' as ' . $this->as->getId();
}

@@ -167,7 +167,6 @@ public function check(
}

if ($prevent_template_covariance
&& $this->defining_class
&& \substr($this->defining_class, 0, 3) !== 'fn-'
) {
$codebase = $source->getCodebase();
@@ -1081,7 +1081,7 @@ public function substitute(Union $old_type, Union $new_type = null)
}

/**
* @param array<string, array<string, array{Type\Union, 1?:int}>> $template_types
* @param array<string, array<non-empty-string, array{Type\Union, 1?:int}>> $template_types
*
* @return void
*/
@@ -1099,8 +1099,8 @@ public function replaceTemplateTypesWithArgTypes(array $template_types, Codebase
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
$template_type = null;

if (isset($template_types[$atomic_type->param_name][$atomic_type->defining_class ?: ''])) {
$template_type = $template_types[$atomic_type->param_name][$atomic_type->defining_class ?: ''][0];
if (isset($template_types[$atomic_type->param_name][$atomic_type->defining_class])) {
$template_type = $template_types[$atomic_type->param_name][$atomic_type->defining_class][0];

if (!$atomic_type->as->isMixed() && $template_type->isMixed()) {
$template_type = clone $atomic_type->as;
@@ -1128,10 +1128,10 @@ public function replaceTemplateTypesWithArgTypes(array $template_types, Codebase
}
}
}
} elseif ($codebase && $atomic_type->defining_class) {
} elseif ($codebase) {
foreach ($template_types as $template_type_map) {
foreach ($template_type_map as $template_class => $_) {
if (!$template_class) {
if (substr($template_class, 0, 3) === 'fn-') {
continue;
}

0 comments on commit c79ba0b

Please sign in to comment.
You can’t perform that action at this time.