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

Compare list generic params #9487

Merged
merged 2 commits into from Mar 11, 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
7 changes: 4 additions & 3 deletions src/Psalm/Internal/Analyzer/NamespaceAnalyzer.php
Expand Up @@ -15,6 +15,7 @@
use function assert;
use function count;
use function implode;
use function is_string;
use function preg_replace;
use function strpos;
use function strtolower;
Expand Down Expand Up @@ -244,7 +245,7 @@ public static function getIdentifierParts(string $identifier): array
while (($pos = strpos($identifier, "\\")) !== false) {
if ($pos > 0) {
$part = substr($identifier, 0, $pos);
assert($part !== "");
assert(is_string($part) && $part !== "");
$parts[] = $part;
}
$parts[] = "\\";
Expand All @@ -253,13 +254,13 @@ public static function getIdentifierParts(string $identifier): array
if (($pos = strpos($identifier, "::")) !== false) {
if ($pos > 0) {
$part = substr($identifier, 0, $pos);
assert($part !== "");
assert(is_string($part) && $part !== "");
$parts[] = $part;
}
$parts[] = "::";
$identifier = substr($identifier, $pos + 2);
}
if ($identifier !== "") {
if ($identifier !== "" && $identifier !== false) {
$parts[] = $identifier;
}

Expand Down
78 changes: 78 additions & 0 deletions src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php
Expand Up @@ -127,6 +127,84 @@ public static function isContainedBy(
}
return false;
}

// check remaining $input_properties against container's fallback_params
if ($container_type_part instanceof TKeyedArray
&& $container_type_part->fallback_params !== null
) {
[$key_type, $value_type] = $container_type_part->fallback_params;
// treat fallback params as possibly undefined
// otherwise comparison below would fail for list{0?:int} <=> list{...<int<0,max>, int>}
// as the latter `int` is not marked as possibly_undefined
$value_type = $value_type->setPossiblyUndefined(true);

foreach ($input_properties as $key => $input_property_type) {
$key_type_comparison = new TypeComparisonResult();
if (!UnionTypeComparator::isContainedBy(
$codebase,
is_string($key) ? Type::getString($key) : Type::getInt(false, $key),
$key_type,
false,
false,
$key_type_comparison,
$allow_interface_equality,
)) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced
= $key_type_comparison->type_coerced === true
&& $atomic_comparison_result->type_coerced !== false;
}
$all_types_contain = false;
}

$property_type_comparison = new TypeComparisonResult();
if (!UnionTypeComparator::isContainedBy(
$codebase,
$input_property_type,
$value_type,
false,
false,
$property_type_comparison,
$allow_interface_equality,
)) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced
= $property_type_comparison->type_coerced === true
&& $atomic_comparison_result->type_coerced !== false;
}
$all_types_contain = false;
}
}
}

// finally, check input type fallback params against container type fallback params
if ($input_type_part instanceof TKeyedArray
&& $container_type_part instanceof TKeyedArray
&& $input_type_part->fallback_params !== null
&& $container_type_part->fallback_params !== null
) {
foreach ($input_type_part->fallback_params as $i => $input_param) {
$container_param = $container_type_part->fallback_params[$i];
$param_comparison = new TypeComparisonResult();
if (!UnionTypeComparator::isContainedBy(
$codebase,
$input_param,
$container_param,
false,
false,
$param_comparison,
$allow_interface_equality,
)) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced
= $param_comparison->type_coerced === true
&& $atomic_comparison_result->type_coerced !== false;
}
$all_types_contain = false;
}
}
}

return $all_types_contain;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ReturnTypeTest.php
Expand Up @@ -1721,6 +1721,18 @@ public static function instance() {
}',
'error_message' => 'InvalidClass',
],
'listItems' => [
'code' => <<<'PHP'
<?php

/** @return list<int> */
function f(): array
{
return[ 1, new stdClass, "zzz"];
}
PHP,
'error_message' => 'InvalidReturnStatement',
],
];
}
}
36 changes: 34 additions & 2 deletions tests/TypeComparatorTest.php
Expand Up @@ -94,7 +94,7 @@ public function getAllBasicTypes(): array
}

/**
* @dataProvider getAllowedChildTypes
* @dataProvider getSuccessfulComparisons
*/
public function testTypeAcceptsType(string $parent_type_string, string $child_type_string): void
{
Expand All @@ -107,13 +107,32 @@ public function testTypeAcceptsType(string $parent_type_string, string $child_ty
$child_type,
$parent_type,
),
'Type ' . $parent_type_string . ' should contain ' . $child_type_string,
);
}

/**
* @dataProvider getUnsuccessfulComparisons
*/
public function testTypeDoesNotAcceptType(string $parent_type_string, string $child_type_string): void
{
$parent_type = Type::parseString($parent_type_string);
$child_type = Type::parseString($child_type_string);

$this->assertFalse(
UnionTypeComparator::isContainedBy(
$this->project_analyzer->getCodebase(),
$child_type,
$parent_type,
),
'Type ' . $parent_type_string . ' should not contain ' . $child_type_string,
);
}

/**
* @return array<array{string, string}>
*/
public function getAllowedChildTypes(): array
public function getSuccessfulComparisons(): array
{
return [
'iterableAcceptsArray' => [
Expand Down Expand Up @@ -142,4 +161,17 @@ public function getAllowedChildTypes(): array
],
];
}

/** @return iterable<string, list{string,string}> */
public function getUnsuccessfulComparisons(): iterable
{
yield 'genericListDoesNotAcceptListTupleWithMismatchedTypes' => [
'list<int>',
'list{int, string}',
];
yield 'genericListDoesNotAcceptArrayTupleWithMismatchedTypes' => [
'list<int>',
'array{int, string}',
];
}
}