Skip to content

Commit

Permalink
[TypeDeclaration] Fixes for array dataProviders with multiple types (#…
Browse files Browse the repository at this point in the history
…5659)

More changes to PHPUnitDataProviderParamTypeInferer which I think
brings this to completeness in terms of what we want to expect.
  • Loading branch information
addshore authored Mar 1, 2021
1 parent 8c3443b commit b14ba76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
-----
<?php
Expand All @@ -47,5 +47,5 @@ final class DemoArrayTest
/**
* @dataProvider provideThings
*/
public function testGetFromId( int $one, bool $two, string $three, $four ): void {}
public function testGetFromId( int $one, bool $two, string $three, ?int $four, $five ): void {}
}

0 comments on commit b14ba76

Please sign in to comment.