Skip to content

Commit

Permalink
AddParamTypeBasedOnPHPUnitDataProviderRector: Check all data providers (
Browse files Browse the repository at this point in the history
  • Loading branch information
jlherren authored Sep 29, 2023
1 parent 547af4e commit 801d367
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ public function hasByName(string $name): bool
return (bool) $this->getTagsByName($name);
}

/**
* @api
*/
public function getByName(string $name): ?Node
{
return $this->getTagsByName($name)[0] ?? null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultipleProviders extends TestCase
{
public function dataProvider1(): iterable {
yield [1];
yield [null];
}

public function dataProvider2(): iterable {
yield ['foo'];
}

/**
* @dataProvider dataProvider1
* @dataProvider dataProvider2
*/
public function testGetFromId($data): void {
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -127,12 +128,12 @@ public function refactor(Node $node): ?Node
continue;
}

$dataProviderPhpDocTagNode = $this->resolveDataProviderPhpDocTagNode($classMethod);
if (! $dataProviderPhpDocTagNode instanceof PhpDocTagNode) {
$dataProviderPhpDocTagNodes = $this->resolveDataProviderPhpDocTagNode($classMethod);
if ($dataProviderPhpDocTagNodes === []) {
return null;
}

$hasClassMethodChanged = $this->refactorClassMethod($classMethod, $node, $dataProviderPhpDocTagNode);
$hasClassMethodChanged = $this->refactorClassMethod($classMethod, $node, $dataProviderPhpDocTagNodes);
if ($hasClassMethodChanged) {
$hasChanged = true;
}
Expand Down Expand Up @@ -279,20 +280,26 @@ private function resolveParamOnPositionTypes(Array_ $array, int $parameterPositi
return $paramOnPositionTypes;
}

private function resolveDataProviderPhpDocTagNode(ClassMethod $classMethod): ?PhpDocTagNode
/**
* @return array<PhpDocTagNode>
*/
private function resolveDataProviderPhpDocTagNode(ClassMethod $classMethod): array
{
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (! $classMethodPhpDocInfo instanceof PhpDocInfo) {
return null;
return [];
}

return $classMethodPhpDocInfo->getByName('@dataProvider');
return $classMethodPhpDocInfo->getTagsByName('@dataProvider');
}

/**
* @param array<PhpDocTagNode> $dataProviderPhpDocTagNodes
*/
private function refactorClassMethod(
ClassMethod $classMethod,
Class_ $class,
PhpDocTagNode $dataProviderPhpDocTagNode
array $dataProviderPhpDocTagNodes
): bool {
$hasChanged = false;

Expand All @@ -301,7 +308,12 @@ private function refactorClassMethod(
continue;
}

$paramTypeDeclaration = $this->inferParam($class, $param, $dataProviderPhpDocTagNode);
$paramTypes = [];
foreach ($dataProviderPhpDocTagNodes as $phpDocTagNode) {
$paramTypes[] = $this->inferParam($class, $param, $phpDocTagNode);
}
$paramTypeDeclaration = TypeCombinator::union(...$paramTypes);

if ($paramTypeDeclaration instanceof MixedType) {
continue;
}
Expand Down

0 comments on commit 801d367

Please sign in to comment.