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

Handle new list representation #130

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/Hooks/TestCaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,27 +387,24 @@ static function (
}
};

/** @var Type\Atomic\TArray|Type\Atomic\TKeyedArray|Type\Atomic\TList $dataset_type */
/**
* @psalm-suppress DeprecatedClass
* @var Type\Atomic\TArray|Type\Atomic\TKeyedArray|Type\Atomic\TList $dataset_type
*/
$dataset_type = self::getAtomics($provider_return_type->type_params[1])['array'];

if ($dataset_type instanceof Type\Atomic\TArray) {
if ($potential_argument_type = self::getItemType($dataset_type)) {
// check that all of the required (?) params accept value type
$potential_argument_type = $dataset_type->type_params[1];
foreach ($method_storage->params as $param_offset => $param) {
if (!$param->type) {
continue;
}
$checkParam($potential_argument_type, $param->type, $param->is_optional, $param_offset);
}
} elseif ($dataset_type instanceof Type\Atomic\TList) {
$potential_argument_type = $dataset_type->type_param;
foreach ($method_storage->params as $param_offset => $param) {
if (!$param->type) {
continue;
}
$checkParam($potential_argument_type, $param->type, $param->is_optional, $param_offset);
}
} else { // TKeyedArray
if (!$dataset_type instanceof Type\Atomic\TKeyedArray) {
throw new RuntimeException('expected TKeyedArray here');
}
// iterate over all params checking if corresponding value type is acceptable
// let's hope properties are sorted in array order
$potential_argument_types = array_values($dataset_type->properties);
Expand Down Expand Up @@ -463,6 +460,26 @@ static function (
}
}

private static function getItemType(Type\Atomic $atomic): ?Type\Union
{
if ($atomic instanceof Type\Atomic\TArray) {
return $atomic->type_params[1];
} elseif (
/** @psalm-suppress DeprecatedClass */
$atomic instanceof Type\Atomic\TList
) {
return $atomic->type_param;
} elseif (
// list-like keyed array
$atomic instanceof Type\Atomic\TKeyedArray
&& $atomic->fallback_params !== null
) {
return $atomic->fallback_params[1];
} else {
return null;
}
}

/** @return non-empty-array<string,Type\Atomic> */
private static function getAtomics(Type\Union $union): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/TestCase.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ Feature: TestCase
Given I have the following code
"""
class MyTestCase extends TestCase {
/** @return iterable<string,array{float,1?:string}> */
/** @return iterable<string,array{0:float,1?:string}> */
public function provide() {
yield "data set" => [1., "a"];
}
Expand Down