Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iterable template replacement #9660

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@
<file src="src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php">
<ImpureMethodCall>
<code>getClassTemplateTypes</code>
<code>has</code>
</ImpureMethodCall>
</file>
<file src="src/Psalm/Internal/Type/TypeCombiner.php">
Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Internal/Provider/ClassLikeStorageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ public function get(string $fq_classlike_name): ClassLikeStorage
return self::$storage[$fq_classlike_name_lc];
}

/**
* @psalm-mutation-free
*/
public function has(string $fq_classlike_name): bool
{
$fq_classlike_name_lc = strtolower($fq_classlike_name);

/** @psalm-suppress ImpureStaticProperty Used only for caching */
return isset(self::$storage[$fq_classlike_name_lc]);
}

Expand Down
23 changes: 13 additions & 10 deletions src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use Psalm\Type\Atomic\TTemplatePropertiesOf;
use Psalm\Type\Atomic\TTemplateValueOf;
use Psalm\Type\Union;
use Throwable;

use function array_fill;
use function array_keys;
Expand Down Expand Up @@ -1233,13 +1232,13 @@ public static function getMappedGenericTypeParams(
$input_type_params = [];
}

try {
$input_class_storage = $codebase->classlike_storage_provider->get($input_type_part->value);
$container_class_storage = $codebase->classlike_storage_provider->get($container_type_part->value);
$container_type_params_covariant = $container_class_storage->template_covariants;
} catch (Throwable $e) {
$input_class_storage = null;
}
$input_class_storage = $codebase->classlike_storage_provider->has($input_type_part->value)
? $codebase->classlike_storage_provider->get($input_type_part->value)
: null;

$container_type_params_covariant = $codebase->classlike_storage_provider->has($container_type_part->value)
? $codebase->classlike_storage_provider->get($container_type_part->value)->template_covariants
: null;

if ($input_type_part->value !== $container_type_part->value
&& $input_class_storage
Expand All @@ -1266,8 +1265,12 @@ public static function getMappedGenericTypeParams(

$template_extends = $input_class_storage->template_extended_params;

if (isset($template_extends[$container_type_part->value])) {
$params = $template_extends[$container_type_part->value];
$container_type_part_value = $container_type_part->value === 'iterable'
? 'Traversable'
: $container_type_part->value;

if (isset($template_extends[$container_type_part_value])) {
$params = $template_extends[$container_type_part_value];

$new_input_params = [];

Expand Down
34 changes: 34 additions & 0 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ function testGenericList(array $list): array { return $list; }
'$genericList===' => 'list{1, 2, 3}',
],
],
'inferIterableFromTraversable' => [
'code' => '<?php
/**
* @return SplFixedArray<string>
*/
function getStrings(): SplFixedArray
{
return SplFixedArray::fromArray(["fst", "snd", "thr"]);
}
/**
* @return SplFixedArray<int>
*/
function getIntegers(): SplFixedArray
{
return SplFixedArray::fromArray([1, 2, 3]);
}
/**
* @template K
* @template A
* @template B
* @param iterable<K, A> $lhs
* @param iterable<K, B> $rhs
* @return iterable<K, A|B>
*/
function mergeIterable(iterable $lhs, iterable $rhs): iterable
{
foreach ($lhs as $k => $v) { yield $k => $v; }
foreach ($rhs as $k => $v) { yield $k => $v; }
}
$iterable = mergeIterable(getStrings(), getIntegers());',
'assertions' => [
'$iterable===' => 'iterable<int, int|string>',
],
],
'countShapedArrays' => [
'code' => '<?php
/** @var array{a?: int} */
Expand Down