From b14ba760e990a8979c0772b42c367cba52a961a6 Mon Sep 17 00:00:00 2001 From: addshore Date: Mon, 1 Mar 2021 00:46:19 +0000 Subject: [PATCH] [TypeDeclaration] Fixes for array dataProviders with multiple types (#5659) More changes to PHPUnitDataProviderParamTypeInferer which I think brings this to completeness in terms of what we want to expect. --- .../PHPUnitDataProviderParamTypeInferer.php | 41 +++++-------------- .../Fixture/dataprovider_array.php.inc | 4 +- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/rules/type-declaration/src/TypeInferer/ParamTypeInferer/PHPUnitDataProviderParamTypeInferer.php b/rules/type-declaration/src/TypeInferer/ParamTypeInferer/PHPUnitDataProviderParamTypeInferer.php index ae00fa41a75a..18a1fd2c2d3e 100644 --- a/rules/type-declaration/src/TypeInferer/ParamTypeInferer/PHPUnitDataProviderParamTypeInferer.php +++ b/rules/type-declaration/src/TypeInferer/ParamTypeInferer/PHPUnitDataProviderParamTypeInferer.php @@ -23,6 +23,7 @@ use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\TypeDeclaration\Contract\TypeInferer\ParamTypeInfererInterface; +use PhpParser\Node\Expr\ArrayItem; final class PHPUnitDataProviderParamTypeInferer implements ParamTypeInfererInterface { @@ -112,23 +113,21 @@ private function resolveReturnStaticArrayTypeByParameterPosition(array $returns, { $paramOnPositionTypes = []; - foreach ($returns as $classMethodReturn) { - if (! $classMethodReturn->expr instanceof Array_) { - continue; - } - - $type = $this->getTypeFromClassMethodReturn($classMethodReturn->expr); + if(! $returns[0]->expr instanceof Array_ ) { + throw new ShouldNotHappenException(); + } - if (! $type instanceof ConstantArrayType) { - return $type; + foreach ($returns[0]->expr->items as $singleDataProvidedSet) { + if(! $singleDataProvidedSet instanceof ArrayItem || ! $singleDataProvidedSet->value instanceof Array_) { + throw new ShouldNotHappenException(); } - foreach ($type->getValueTypes() as $position => $valueType) { - if ($position !== $parameterPosition) { + foreach($singleDataProvidedSet->value->items as $position => $singleDataProvidedSetItem ) { + if( $position !== $parameterPosition || ! $singleDataProvidedSetItem instanceof ArrayItem ) { continue; } - $paramOnPositionTypes[] = $valueType; + $paramOnPositionTypes[] = $this->nodeTypeResolver->resolve($singleDataProvidedSetItem->value); } } @@ -139,26 +138,6 @@ private function resolveReturnStaticArrayTypeByParameterPosition(array $returns, return $this->typeFactory->createMixedPassedOrUnionType($paramOnPositionTypes); } - private function getTypeFromClassMethodReturn(Array_ $classMethodReturnArrayNode): Type - { - $arrayTypes = $this->nodeTypeResolver->resolve($classMethodReturnArrayNode); - - // impossible to resolve - if (! $arrayTypes instanceof ConstantArrayType) { - return new MixedType(); - } - - // nest to 1 item - $arrayTypes = $arrayTypes->getValueTypes()[0]; - - // impossible to resolve - if (! $arrayTypes instanceof ConstantArrayType) { - return new MixedType(); - } - - return $arrayTypes; - } - /** * @param Yield_[] $yields */ diff --git a/rules/type-declaration/tests/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/dataprovider_array.php.inc b/rules/type-declaration/tests/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/dataprovider_array.php.inc index 790c7f8b248e..3c41b515f1e7 100644 --- a/rules/type-declaration/tests/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/dataprovider_array.php.inc +++ b/rules/type-declaration/tests/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/dataprovider_array.php.inc @@ -21,7 +21,7 @@ final class DemoArrayTest /** * @dataProvider provideThings */ - public function testGetFromId( $one, $two, $three, $four ): void {} + public function testGetFromId( $one, $two, $three, $four, $five ): void {} } -----