rector/vendor/rector/rector-phpunit/src/NodeFactory/DataProviderClassMethodFactory.php
Tomas Votruba 785f5e3b06 Updated Rector to commit 74f6b181e82f191c1e471d446a029a06dff16619
74f6b181e8 [DX] Remove upgrade RectorConfig set, as last 2 version use only PHP (#2852)
2022-08-29 21:45:23 +00:00

42 lines
1.5 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\PHPUnit\NodeFactory;
use PhpParser\Builder\Method;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\PHPUnit\ValueObject\DataProviderClassMethodRecipe;
final class DataProviderClassMethodFactory
{
public function createFromRecipe(DataProviderClassMethodRecipe $dataProviderClassMethodRecipe) : ClassMethod
{
$method = new Method($dataProviderClassMethodRecipe->getMethodName());
$method->makePublic();
$classMethod = $method->getNode();
foreach ($dataProviderClassMethodRecipe->getArgs() as $arg) {
$value = $arg->value;
if (!$value instanceof Array_) {
continue;
}
foreach ($value->items as $arrayItem) {
if (!$arrayItem instanceof ArrayItem) {
continue;
}
$returnStatement = new Yield_(new Array_([new ArrayItem($arrayItem->value)]));
$classMethod->stmts[] = new Expression($returnStatement);
}
}
$this->decorateClassMethodWithReturnTypeAndTag($classMethod);
return $classMethod;
}
private function decorateClassMethodWithReturnTypeAndTag(ClassMethod $classMethod) : void
{
$classMethod->returnType = new FullyQualified('Iterator');
}
}